Skip to content

Commit

Permalink
support iac-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
XanSmarty committed Oct 20, 2023
1 parent c98bd02 commit 830ee9b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ import Foundation
public var street:String?
public var locality:String?
public var administrativeArea:String?
public var superAdministrativeArea:String?;
public var subAdministrativeArea:String?;
public var postalCode:String?
public var countryISO3:String?
public var entries:Int?
public var addressText:String?
public var addressID:String?

enum CodingKeys:String, CodingKey {
case street = "street"
case locality = "locality"
case administrativeArea = "administrative_area"
case superAdministrativeArea = "super_administrative_area"
case subAdministrativeArea = "sub_administrative_area"
case postalCode = "postal_code"
case countryISO3 = "country_iso3"
case entries = "entries"
case addressText = "address_text"
case addressID = "address_id"
}

init(dictionary:NSDictionary) {
self.street = dictionary["street"] as? String
self.locality = dictionary["locality"] as? String
self.administrativeArea = dictionary["administrative_area"] as? String
self.superAdministrativeArea = dictionary["super_administrative_area"] as? String
self.subAdministrativeArea = dictionary["sub_administrative_area"] as? String
self.postalCode = dictionary["postal_code"] as? String
self.countryISO3 = dictionary["country_iso3"] as? String
self.entries = dictionary["entries"] as? Int
self.addressText = dictionary["address_text"] as? String
self.addressID = dictionary["address_id"] as? String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ public class InternationalAutocompleteClient: NSObject {
@objc public func sendLookup(lookup: UnsafeMutablePointer<InternationalAutocompleteLookup>, error: UnsafeMutablePointer<NSError?>) -> Bool {
// Sends a Lookup object to the International Autocomplete API and stores the result in the Lookup's result field.

if let prefix = lookup.pointee.search {
if prefix.count == 0 {
let details = [NSLocalizedDescriptionKey:"sendLookup must be passed a Lookup with the prefix field set."]
error.pointee = NSError(domain: SmartyErrors().SSErrorDomain, code: SmartyErrors.SSErrors.FieldNotSetError.rawValue, userInfo: details)
return false
}

let request = buildRequest(lookup:lookup.pointee)
let response = self.sender.sendRequest(request: request, error: &error.pointee)
if error.pointee != nil { return false }

var result:InternationalAutocompleteResult

if let payload = response?.payload {
result = self.serializer.Deserialize(payload: payload, error: &error.pointee) as? InternationalAutocompleteResult ?? InternationalAutocompleteResult(dictionary: NSDictionary())
} else {
result = InternationalAutocompleteResult(dictionary: NSDictionary())
}

// Naming of parameters to allow JSON deserialization
if error.pointee != nil { return false }

lookup.pointee.result = result
return true
} else {
if lookup.pointee.country == nil || (lookup.pointee.search == nil && lookup.pointee.addressID == nil) {
let details = [NSLocalizedDescriptionKey:"sendLookup must be passed a Lookup with the country field set, and either search or addressID set."]
error.pointee = NSError(domain: SmartyErrors().SSErrorDomain, code: SmartyErrors.SSErrors.FieldNotSetError.rawValue, userInfo: details)
return false
}

let request = buildRequest(lookup:lookup.pointee)
let response = self.sender.sendRequest(request: request, error: &error.pointee)
if error.pointee != nil { return false }

var result:InternationalAutocompleteResult

if let payload = response?.payload {
result = self.serializer.Deserialize(payload: payload, error: &error.pointee) as? InternationalAutocompleteResult ?? InternationalAutocompleteResult(dictionary: NSDictionary())
} else {
result = InternationalAutocompleteResult(dictionary: NSDictionary())
}

// Naming of parameters to allow JSON deserialization
if error.pointee != nil { return false }

lookup.pointee.result = result
return true
}

func buildRequest(lookup:InternationalAutocompleteLookup) -> SmartyRequest {
let request = SmartyRequest()

if let unwrappedPrefix = lookup.addressID {
request.urlPrefix = "/" + unwrappedPrefix
}

request.setValue(value: lookup.search ?? "", HTTPParameterField: "search")
request.setValue(value: lookup.country ?? "", HTTPParameterField: "country")
Expand All @@ -53,7 +53,6 @@ public class InternationalAutocompleteClient: NSObject {
if lookup.geolocation != InternationalAutocompleteLookup.InternationalGeolocateType.none {
request.setValue(value: lookup.geolocation?.rawValue ?? "", HTTPParameterField: "geolocation")
}
request.setValue(value: lookup.administrativeArea ?? "", HTTPParameterField: "include_only_administrative_area")
request.setValue(value: lookup.locality ?? "", HTTPParameterField: "include_only_locality")
request.setValue(value: lookup.postalCode ?? "", HTTPParameterField: "include_only_postal_code")
request.setValue(value: lookup.longitude.flatMap { String($0) } ?? "", HTTPParameterField: "longitude")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,27 @@ import Foundation
public var result:InternationalAutocompleteResult?
public var country:String?
public var search:String?
public var addressID:String?
public var maxResults:Int?
public var distance:Int?
public var geolocation:InternationalGeolocateType?
public var administrativeArea:String?
public var locality:String?
public var postalCode:String?
public var latitude:Double?
public var longitude:Double?

public enum InternationalGeolocateType: String, Codable {
case adminarea
case locality
case postalcode
case geocodes
case ip_address
case none
}

enum CodingKeys: String, CodingKey {
case country = "country"
case search = "search"
case addressID = "address_id"
case maxResults = "max_results"
case distance = "distance"
case geolocation = "geolocation"
case administrativeArea = "include_only_administrative_area"
case locality = "include_only_locality"
case postalCode = "include_only_postal_code"
case latitude = "latitude"
Expand Down
7 changes: 6 additions & 1 deletion Sources/SmartyStreets/URLPrefixSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public class URLPrefixSender: SmartySender {
}

override func sendRequest(request: SmartyRequest, error: inout NSError!) -> SmartyResponse! {
request.urlPrefix = self.urlPrefix
if !request.urlPrefix.isEmpty {
request.urlPrefix = self.urlPrefix + request.urlPrefix
} else {
request.urlPrefix = self.urlPrefix
}

return self.inner.sendRequest(request: request, error: &error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ class InternationalAutocompleteClientTests: XCTestCase {
lookup.search = "1"
lookup.country = "2"
lookup.locality = "3"
lookup.administrativeArea = "4"
lookup.maxResults = 5
lookup.distance = 7
lookup.geolocation = InternationalAutocompleteLookup.InternationalGeolocateType.geocodes
lookup.postalCode = "6"
lookup.maxResults = 4
lookup.distance = 6
lookup.geolocation = InternationalAutocompleteLookup.InternationalGeolocateType.ip_address
lookup.postalCode = "5"
lookup.longitude = 112.1
lookup.latitude = -23.4

Expand All @@ -39,11 +38,10 @@ class InternationalAutocompleteClientTests: XCTestCase {
XCTAssertEqual("1", capturingSender.request.parameters["search"])
XCTAssertEqual("2", capturingSender.request.parameters["country"])
XCTAssertEqual("3", capturingSender.request.parameters["include_only_locality"])
XCTAssertEqual("4", capturingSender.request.parameters["include_only_administrative_area"])
XCTAssertEqual("5", capturingSender.request.parameters["max_results"])
XCTAssertEqual("7", capturingSender.request.parameters["distance"])
XCTAssertEqual("geocodes", capturingSender.request.parameters["geolocation"])
XCTAssertEqual("6", capturingSender.request.parameters["include_only_postal_code"])
XCTAssertEqual("4", capturingSender.request.parameters["max_results"])
XCTAssertEqual("6", capturingSender.request.parameters["distance"])
XCTAssertEqual("ip_address", capturingSender.request.parameters["geolocation"])
XCTAssertEqual("5", capturingSender.request.parameters["include_only_postal_code"])
XCTAssertEqual("112.1", capturingSender.request.parameters["longitude"])
XCTAssertEqual("-23.4", capturingSender.request.parameters["latitude"])
XCTAssertNil(self.error)
Expand All @@ -62,7 +60,6 @@ class InternationalAutocompleteClientTests: XCTestCase {
XCTAssertEqual("1", capturingSender.request.parameters["search"])
XCTAssertEqual("2", capturingSender.request.parameters["country"])
XCTAssertEqual(nil, capturingSender.request.parameters["include_only_locality"])
XCTAssertEqual(nil, capturingSender.request.parameters["include_only_administrative_area"])
XCTAssertEqual("10", capturingSender.request.parameters["max_results"])
XCTAssertEqual("5", capturingSender.request.parameters["distance"])
XCTAssertEqual(nil, capturingSender.request.parameters["geolocation"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ class InternationalAutocompleteResultTests: XCTestCase {
"street":"0",
"locality":"1",
"administrative_area":"2",
"super_administrative_area":"3",
"sub_administrative_area":"4",
"postal_code":"5",
"country_iso3":"6"
"postal_code":"3",
"country_iso3":"4"
],
[
"street":"7",
"locality":"8",
"administrative_area":"9",
"super_administrative_area":"super",
"sub_administrative_area":"sub",
"postal_code":"10",
"country_iso3":"11"
"street":"5",
"locality":"6",
"administrative_area":"7",
"postal_code":"8",
"country_iso3":"9"
]
]
]
Expand All @@ -41,17 +37,13 @@ class InternationalAutocompleteResultTests: XCTestCase {
XCTAssertEqual("0", result.getCandidateAtIndex(index: 0).street)
XCTAssertEqual("1", result.getCandidateAtIndex(index: 0).locality)
XCTAssertEqual("2", result.getCandidateAtIndex(index: 0).administrativeArea)
XCTAssertEqual("3", result.getCandidateAtIndex(index: 0).superAdministrativeArea)
XCTAssertEqual("4", result.getCandidateAtIndex(index: 0).subAdministrativeArea)
XCTAssertEqual("5", result.getCandidateAtIndex(index: 0).postalCode)
XCTAssertEqual("6", result.getCandidateAtIndex(index: 0).countryISO3)
XCTAssertEqual("3", result.getCandidateAtIndex(index: 0).postalCode)
XCTAssertEqual("4", result.getCandidateAtIndex(index: 0).countryISO3)

XCTAssertEqual("7", result.getCandidateAtIndex(index: 1).street)
XCTAssertEqual("8", result.getCandidateAtIndex(index: 1).locality)
XCTAssertEqual("9", result.getCandidateAtIndex(index: 1).administrativeArea)
XCTAssertEqual("super", result.getCandidateAtIndex(index: 1).superAdministrativeArea)
XCTAssertEqual("sub", result.getCandidateAtIndex(index: 1).subAdministrativeArea)
XCTAssertEqual("10", result.getCandidateAtIndex(index: 1).postalCode)
XCTAssertEqual("11", result.getCandidateAtIndex(index: 1).countryISO3)
XCTAssertEqual("5", result.getCandidateAtIndex(index: 1).street)
XCTAssertEqual("6", result.getCandidateAtIndex(index: 1).locality)
XCTAssertEqual("7", result.getCandidateAtIndex(index: 1).administrativeArea)
XCTAssertEqual("8", result.getCandidateAtIndex(index: 1).postalCode)
XCTAssertEqual("9", result.getCandidateAtIndex(index: 1).countryISO3)
}
}
2 changes: 1 addition & 1 deletion samples/Sources/swiftExamples/USReverseGeoExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class USReverseGeoExample {
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/us-reverse-geo-api#http-input-fields

var lookup = USReverseGeoLookup(latitude: 40.27644, longitude: -111.65747)
var lookup = USReverseGeoLookup(latitude: 40.27644, longitude: -111.65747, source: "postal")

var error: NSError! = nil

Expand Down

0 comments on commit 830ee9b

Please sign in to comment.