Skip to content

Commit

Permalink
Add deprecated iac
Browse files Browse the repository at this point in the history
  • Loading branch information
XanSmarty committed Oct 26, 2023
1 parent 830ee9b commit d4132e9
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 31 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 @@ -49,14 +49,8 @@ public class InternationalAutocompleteClient: NSObject {
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.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,19 +7,14 @@ 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 locality:String?
public var postalCode:String?
public var latitude:Double?
public var longitude:Double?

public enum InternationalGeolocateType: String, Codable {
case ip_address
Expand All @@ -31,17 +26,12 @@ import Foundation
case search = "search"
case addressID = "address_id"
case maxResults = "max_results"
case distance = "distance"
case geolocation = "geolocation"
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())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Foundation

public class InternationalAutocompleteDeprecatedSerializer: SmartySerializer {

override public init() {}

override func Serialize(obj: Any?, error: inout NSError!) -> Data! {
let raw:[InternationalAutocompleteDeprecatedLookup]? = obj as? [InternationalAutocompleteDeprecatedLookup]
let smartyErrors = SmartyErrors()
let jsonEncoder = JSONEncoder()
if raw == nil {
let details = [NSLocalizedDescriptionKey: "The object to be serialized is nil"]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.ObjectNilError.rawValue, userInfo: details)
return nil
}

do {
let jsonData = try jsonEncoder.encode(raw) as Data
return jsonData
} catch let jsonError {
let details = [NSLocalizedDescriptionKey: jsonError.localizedDescription]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.ObjectInvalidTypeError.rawValue, userInfo: details)
}


return nil
}

override public func Deserialize(payload: Data?, error: inout NSError!) -> Any! {
let smartyErrors = SmartyErrors()
let jsonDecoder = JSONDecoder()
if payload == nil {
let details = [NSLocalizedDescriptionKey: "The payload is nil."]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.ObjectNilError.rawValue, userInfo: details)
return nil
}

do {
let result:InternationalAutocompleteDeprecatedResult = try jsonDecoder.decode(InternationalAutocompleteDeprecatedResult.self, from: payload!)
return result
} catch let jsonError {
let details = [NSLocalizedDescriptionKey:jsonError.localizedDescription]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.ObjectInvalidTypeError.rawValue, userInfo: details)
return nil
}
}
}
Loading

0 comments on commit d4132e9

Please sign in to comment.