-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
449 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ts/InternationalAutocompleteDeprecated/InternationalAutocompleteDeprecatedCandidate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...reets/InternationalAutocompleteDeprecated/InternationalAutocompleteDeprecatedClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...reets/InternationalAutocompleteDeprecated/InternationalAutocompleteDeprecatedLookup.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...reets/InternationalAutocompleteDeprecated/InternationalAutocompleteDeprecatedResult.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...s/InternationalAutocompleteDeprecated/InternationalAutocompleteDeprecatedSerializer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.