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 31, 2023
1 parent c98bd02 commit bc1af9e
Show file tree
Hide file tree
Showing 20 changed files with 552 additions and 104 deletions.
11 changes: 9 additions & 2 deletions Sources/SmartyStreets/ClientBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import Foundation
var proxy:NSDictionary!
var licenses:[String] = []
var internationalStreetApiURL:String = "https://international-street.api.smarty.com/verify"
var internationalAutocompleteApiURL:String = "https://international-autocomplete.api.smarty.com/lookup"
var internationalAutocompleteApiURL:String = "https://international-autocomplete.api.smarty.com/v2/lookup"
var internationalAutocompleteApiDeprecatedURL:String = "https://international-autocomplete.api.smarty.com/lookup"
var usAutocompleteApiURL:String = "https://us-autocomplete.api.smarty.com/suggest"
var usAutocompleteProApiURL:String = "https://us-autocomplete-pro.api.smarty.com/lookup"
var usExtractApiURL:String = "https://us-extract.api.smarty.com"
Expand Down Expand Up @@ -133,12 +134,18 @@ import Foundation
let serializer = InternationalStreetSerializer()
return InternationalStreetClient(sender:buildSender(), serializer: serializer)
}

public func buildInternationalAutocompleteApiClient() -> InternationalAutocompleteClient {
ensureURLPrefixNotNil(url: self.internationalAutocompleteApiURL)
let serializer = InternationalAutocompleteSerializer()
return InternationalAutocompleteClient(sender:buildSender(), serializer:serializer)
}

public func buildInternationalAutocompleteApiDeprecatedClient() -> InternationalAutocompleteDeprecatedClient {
ensureURLPrefixNotNil(url: self.internationalAutocompleteApiDeprecatedURL)
let serializer = InternationalAutocompleteDeprecatedSerializer()
return InternationalAutocompleteDeprecatedClient(sender:buildSender(), serializer:serializer)
}

public func buildUSAutocompleteApiClient() -> USAutocompleteClient { // Deprecated
ensureURLPrefixNotNil(url: self.usAutocompleteApiURL)
Expand Down
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,50 +14,43 @@ 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")
request.setValue(value: lookup.maxResults.flatMap { String($0) } ?? "10", HTTPParameterField: "max_results")
request.setValue(value: lookup.distance.flatMap { String($0) } ?? "5", HTTPParameterField: "distance")
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")
request.setValue(value: lookup.latitude.flatMap { String($0) } ?? "", HTTPParameterField: "latitude")

return request
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,26 @@ import Foundation
// See "https://smartystreets.com/docs/cloud/international-address-autocomplete-api#pro-http-request-input-fields"

static let SSMaxResults = 10
static let SSDistance = 5

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 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"
case longitude = "longitude"
}

override public init() {
self.maxResults = InternationalAutocompleteLookup.SSMaxResults
self.distance = InternationalAutocompleteLookup.SSDistance
}

public func withSearch(search:String) -> InternationalAutocompleteLookup {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

@objcMembers public class InternationalAutocompleteDeprecatedCandidate: NSObject, Codable {
// A candidate is a possible match for an address that was submitted.
//
// See "https://smartystreets.com/docs/cloud/international-address-autocomplete-api"

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?

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"
}

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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Foundation

public class InternationalAutocompleteDeprecatedClient: NSObject {
// It is recommended to instantiate this class using SSClientBuilder

var sender:SmartySender
public var serializer:SmartySerializer

public init(sender:Any, serializer:SmartySerializer) {
self.sender = sender as! SmartySender
self.serializer = serializer
}

@objc public func sendLookup(lookup: UnsafeMutablePointer<InternationalAutocompleteDeprecatedLookup>, 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:InternationalAutocompleteDeprecatedResult

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

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

lookup.pointee.result = result
return true
} else {
return false
}
}

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

request.setValue(value: lookup.search ?? "", HTTPParameterField: "search")
request.setValue(value: lookup.country ?? "", HTTPParameterField: "country")
request.setValue(value: lookup.maxResults.flatMap { String($0) } ?? "10", HTTPParameterField: "max_results")
request.setValue(value: lookup.distance.flatMap { String($0) } ?? "5", HTTPParameterField: "distance")
if lookup.geolocation != InternationalAutocompleteDeprecatedLookup.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")
request.setValue(value: lookup.latitude.flatMap { String($0) } ?? "", HTTPParameterField: "latitude")

return request
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Foundation

@objcMembers public class InternationalAutocompleteDeprecatedLookup: NSObject, Codable {
// In addition to holding all of the input data for this lookup, this class also will contain the result
// of the lookup after it comes back from the API.
//
// See "https://smartystreets.com/docs/cloud/international-address-autocomplete-api#pro-http-request-input-fields"

static let SSMaxResults = 10
static let SSDistance = 5

public var result:InternationalAutocompleteDeprecatedResult?
public var country:String?
public var search: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 none
}

enum CodingKeys: String, CodingKey {
case country = "country"
case search = "search"
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"
case longitude = "longitude"
}

override public init() {
self.maxResults = InternationalAutocompleteDeprecatedLookup.SSMaxResults
self.distance = InternationalAutocompleteDeprecatedLookup.SSDistance
}

public func withSearch(search:String) -> InternationalAutocompleteDeprecatedLookup {
self.search = search
return self
}

func getResultAtIndex(index:Int) -> InternationalAutocompleteDeprecatedCandidate{
if let result = self.result {
return result.candidates![index]
} else {
return InternationalAutocompleteDeprecatedCandidate(dictionary: NSDictionary())
}
}

public func setMaxResults(maxResults: Int, error: inout NSError?) {
if maxResults > 0 && maxResults <= 10 {
self.maxResults = maxResults
} else {
let details = [NSLocalizedDescriptionKey:"Max suggestions must be a postive integer no larger than 10."]
error = NSError(domain: SmartyErrors().SSErrorDomain, code: SmartyErrors.SSErrors.NotPositiveIntergerError.rawValue, userInfo: details)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

@objcMembers public class InternationalAutocompleteDeprecatedResult: NSObject, Codable {

public var candidates:[InternationalAutocompleteDeprecatedCandidate]?

public init(dictionary: NSDictionary) {
super.init()
if let candidates = dictionary["candidates"] {
self.candidates = convertToCandidateObjects(candidates as! [[String:Any]])
} else {
self.candidates = [InternationalAutocompleteDeprecatedCandidate]()
}
}

func convertToCandidateObjects(_ object:[[String:Any]]) -> [InternationalAutocompleteDeprecatedCandidate] {
var mutable = [InternationalAutocompleteDeprecatedCandidate]()
for suggestion in object {
mutable.append(InternationalAutocompleteDeprecatedCandidate(dictionary: suggestion as NSDictionary))
}
return mutable
}

func getCandidateAtIndex(index: Int) -> InternationalAutocompleteDeprecatedCandidate {
if let candidates = self.candidates {
return candidates[index]
} else {
return InternationalAutocompleteDeprecatedCandidate(dictionary: NSDictionary())
}
}
}
Loading

0 comments on commit bc1af9e

Please sign in to comment.