diff --git a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteCandidate.swift b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteCandidate.swift index 692ad89..80cad65 100644 --- a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteCandidate.swift +++ b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteCandidate.swift @@ -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 } } diff --git a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteClient.swift b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteClient.swift index 6d2e81c..0156087 100644 --- a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteClient.swift +++ b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteClient.swift @@ -14,37 +14,37 @@ public class InternationalAutocompleteClient: NSObject { @objc public func sendLookup(lookup: UnsafeMutablePointer, error: UnsafeMutablePointer) -> 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") @@ -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") diff --git a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteLookup.swift b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteLookup.swift index 7866fe0..0fd4855 100644 --- a/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteLookup.swift +++ b/Sources/SmartyStreets/InternationalAutocomplete/InternationalAutocompleteLookup.swift @@ -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" diff --git a/Sources/SmartyStreets/URLPrefixSender.swift b/Sources/SmartyStreets/URLPrefixSender.swift index 396ec47..f91207c 100644 --- a/Sources/SmartyStreets/URLPrefixSender.swift +++ b/Sources/SmartyStreets/URLPrefixSender.swift @@ -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) } } diff --git a/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteClientTests.swift b/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteClientTests.swift index 076c85d..75c4db5 100644 --- a/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteClientTests.swift +++ b/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteClientTests.swift @@ -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 @@ -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) @@ -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"]) diff --git a/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteResultTests.swift b/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteResultTests.swift index e8185b8..87d1558 100644 --- a/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteResultTests.swift +++ b/Tests/SmartyStreetsTests/InternationalAutocompleteTests/InternationalAutocompleteResultTests.swift @@ -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" ] ] ] @@ -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) } } diff --git a/samples/Sources/swiftExamples/USReverseGeoExample.swift b/samples/Sources/swiftExamples/USReverseGeoExample.swift index 8a8b6c8..2fadd8f 100644 --- a/samples/Sources/swiftExamples/USReverseGeoExample.swift +++ b/samples/Sources/swiftExamples/USReverseGeoExample.swift @@ -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