Skip to content

Commit

Permalink
Merge pull request #41 from smartystreets/eric/enrichment-address-search
Browse files Browse the repository at this point in the history
Enrichment Address Search
  • Loading branch information
RyanLCox1 authored Jan 26, 2025
2 parents 2203d7d + 2b27bf4 commit 67f2c5a
Show file tree
Hide file tree
Showing 43 changed files with 599 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class InternationalAutocompleteClient: NSObject {
request.setValue(value: lookup.maxResults.flatMap { String($0) } ?? "10", HTTPParameterField: "max_results")
request.setValue(value: lookup.locality ?? "", HTTPParameterField: "include_only_locality")
request.setValue(value: lookup.postalCode ?? "", HTTPParameterField: "include_only_postal_code")
for key in lookup.getCustomParamArray().keys {
request.setValue(value: lookup.getCustomParamArray()[key] ?? "", HTTPParameterField: key)
}

return request
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation

static let SSMaxResults = 10

private var customParamArray: [String: String] = [:]
public var result:InternationalAutocompleteResult?
public var country:String?
public var search:String?
Expand Down Expand Up @@ -42,6 +43,14 @@ import Foundation
}
}

public func getCustomParamArray() -> [String: String] {
return self.customParamArray
}

public func addCustomParameter(parameter: String, value: String) {
self.customParamArray.updateValue(value, forKey: parameter)
}

public func setMaxResults(maxResults: Int, error: inout NSError?) {
if maxResults > 0 && maxResults <= 10 {
self.maxResults = maxResults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class InternationalStreetClient: NSObject {
request.setValue(value: lookup.administrativeArea ?? "", HTTPParameterField: "administrative_area")
request.setValue(value: lookup.postalCode ?? "", HTTPParameterField: "postal_code")

for key in lookup.getCustomParamArray().keys {
request.setValue(value: lookup.getCustomParamArray()[key] ?? "", HTTPParameterField: key)
}

return request
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Foundation
// When set to language_mode.Native, the results will always be in the language of the output country.
// When set to language_mode.Latin, the results will always be provided using a Latin character set.

private var customParamArray: [String: String] = [:]
public var result:[InternationalStreetCandidate]?
public var inputId:String?
public var country:String?
Expand Down Expand Up @@ -99,4 +100,12 @@ import Foundation
self.geocode = "false"
}
}

public func getCustomParamArray() -> [String: String] {
return self.customParamArray
}

public func addCustomParameter(parameter: String, value: String) {
self.customParamArray.updateValue(value, forKey: parameter)
}
}
1 change: 1 addition & 0 deletions Sources/SmartyStreets/SmartyErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SmartyErrors {
case NotPositiveIntergerError
case JSONSerializationError
case MaxRetriesExceededError
case NotModifiedInfo = 304
case BadRequestError = 400
case BadCredentialsError = 401
case PaymentRequiredError = 402
Expand Down
4 changes: 4 additions & 0 deletions Sources/SmartyStreets/StatusCodeSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class StatusCodeSender: SmartySender {
switch response?.statusCode {
case 200:
return response
case 304:
let details = [NSLocalizedDescriptionKey:"Not Modified. This data has not been modified since it was last retrieved."]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.NotModifiedInfo.rawValue, userInfo: details)
return response
case 400:
let details = [NSLocalizedDescriptionKey:"Bad Request (Malformed Payload): A GET request lacked a street field or the request body of a POST request contained malformed JSON."]
error = NSError(domain: smartyErrors.SSErrorDomain, code: SmartyErrors.SSErrors.BadRequestError.rawValue, userInfo: details)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class USAutocompleteProClient: NSObject {
request.setValue(value: lookup.preferGeolocation!.name, HTTPParameterField: "prefer_geolocation")
}

for key in lookup.getCustomParamArray().keys {
request.setValue(value: lookup.getCustomParamArray()[key] ?? "", HTTPParameterField: key)
}

return request
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Foundation
let SSMaxResults = 10
let SSPreferRatio = 3

private var customParamArray: [String: String] = [:]
public var result:USAutocompleteProResult?
public var search:String?
public var selected:String?
Expand Down Expand Up @@ -90,6 +91,10 @@ import Foundation
}
}

public func getCustomParamArray() -> [String: String] {
return self.customParamArray
}

public func setMaxResults(maxResults: Int, error: inout NSError?) {
if maxResults > 0 && maxResults <= 10 {
self.maxResults = maxResults
Expand Down Expand Up @@ -122,4 +127,8 @@ import Foundation
public func addPreferZIPCode(zipcode:String) {
self.preferZIPCodes?.append(zipcode)
}

public func addCustomParameter(parameter: String, value: String) {
self.customParamArray.updateValue(value, forKey: parameter)
}
}
109 changes: 108 additions & 1 deletion Sources/SmartyStreets/USEnrichment/LookupTypes/EnrichmentLookup.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import Foundation

public class EnrichmentLookup: Encodable {
private let smarty_key: String
private var smarty_key: String
private let data_set_name: String
private let data_subset_name: String
private var include_array: [String]
private var exclude_array: [String]
private var street: String
private var city: String
private var state: String
private var zipcode: String
private var freeform: String
private var etag: String
private var custom_param_array: [String: String] = [:]

public init(smartyKey: String, datasetName: String, dataSubsetName: String) {
self.smarty_key = smartyKey
self.data_set_name = datasetName
self.data_subset_name = dataSubsetName
self.include_array = [String]()
self.exclude_array = [String]()
self.street = ""
self.city = ""
self.state = ""
self.zipcode = ""
self.freeform = ""
self.etag = ""
}

public init() {
self.smarty_key = ""
self.data_set_name = ""
self.data_subset_name = ""
self.include_array = [String]()
self.exclude_array = [String]()
self.street = ""
self.city = ""
self.state = ""
self.zipcode = ""
self.freeform = ""
self.etag = ""
}

public func getSmartyKey() -> String {
Expand All @@ -22,6 +53,82 @@ public class EnrichmentLookup: Encodable {
public func getDataSubsetName() -> String {
return data_subset_name
}

public func getIncludeAttributes() -> [String] {
return self.include_array
}

public func getExcludeAttributes() -> [String] {
return self.exclude_array
}

public func getStreet() -> String {
return self.street
}

public func getCity() -> String {
return self.city
}

public func getState() -> String {
return self.state
}

public func getZipcode() -> String {
return self.zipcode
}

public func getFreeform() -> String {
return self.freeform
}

public func getEtag() -> String{
return self.etag
}

public func getCustomParamArray() -> [String: String] {
return self.custom_param_array
}

public func setSmartyKey(smarty_key: String) {
self.smarty_key = smarty_key
}

public func addIncludeAttribute(attribute: String) {
self.include_array.append(attribute)
}

public func addExcludeAttribute(attribute: String) {
self.exclude_array.append(attribute)
}

public func setStreet(street: String) {
self.street = street
}

public func setCity(city: String) {
self.city = city
}

public func setState(state: String) {
self.state = state
}

public func setZipcode(zipcode: String) {
self.zipcode = zipcode
}

public func setFreeform(freeform: String) {
self.freeform = freeform
}

public func setEtag(etag: String) {
self.etag = etag
}

public func addCustomParameter(parameter: String, value: String) {
self.custom_param_array.updateValue(value, forKey: parameter)
}

public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
fatalError("You must use a Lookup subclass with an implemented version of deserializeAndSetResults")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class GeoReferenceEnrichmentLookup: EnrichmentLookup {
super.init(smartyKey: smartyKey, datasetName: "geo-reference", dataSubsetName: "")
}

init(lookup: EnrichmentLookup) {
self.results = nil
super.init(smartyKey: lookup.getSmartyKey(), datasetName: "geo-reference", dataSubsetName: "")
for key in lookup.getIncludeAttributes() {
self.addIncludeAttribute(attribute: key)
}
for key in lookup.getExcludeAttributes() {
self.addExcludeAttribute(attribute: key)
}
self.setStreet(street: lookup.getStreet())
self.setCity(city: lookup.getCity())
self.setState(state: lookup.getState())
self.setZipcode(zipcode: lookup.getZipcode())
self.setFreeform(freeform: lookup.getFreeform())
self.setEtag(etag: lookup.getEtag())
for key in lookup.getCustomParamArray().keys {
self.addCustomParameter(parameter: key, value: lookup.getCustomParamArray()[key] ?? "")
}
}

override public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
self.results = serializer.Deserialize(payload: payload, error: &error.pointee) as? [GeoReferenceResult]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class PropertyFinancialEnrichmentLookup: EnrichmentLookup {
super.init(smartyKey: smartyKey, datasetName: "property", dataSubsetName: "financial")
}

init(lookup: EnrichmentLookup) {
self.results = nil
super.init(smartyKey: lookup.getSmartyKey(), datasetName: "property", dataSubsetName: "financial")
for key in lookup.getIncludeAttributes() {
self.addIncludeAttribute(attribute: key)
}
for key in lookup.getExcludeAttributes() {
self.addExcludeAttribute(attribute: key)
}
self.setStreet(street: lookup.getStreet())
self.setCity(city: lookup.getCity())
self.setState(state: lookup.getState())
self.setZipcode(zipcode: lookup.getZipcode())
self.setFreeform(freeform: lookup.getFreeform())
self.setEtag(etag: lookup.getEtag())
for key in lookup.getCustomParamArray().keys {
self.addCustomParameter(parameter: key, value: lookup.getCustomParamArray()[key] ?? "")
}
}

override public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
self.results = serializer.Deserialize(payload: payload, error: &error.pointee) as? [FinancialResult]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class PropertyPrincipalEnrichmentLookup: EnrichmentLookup {
super.init(smartyKey: smartyKey, datasetName: "property", dataSubsetName: "principal")
}

init(lookup: EnrichmentLookup) {
self.results = nil
super.init(smartyKey: lookup.getSmartyKey(), datasetName: "property", dataSubsetName: "principal")
for key in lookup.getIncludeAttributes() {
self.addIncludeAttribute(attribute: key)
}
for key in lookup.getExcludeAttributes() {
self.addExcludeAttribute(attribute: key)
}
self.setStreet(street: lookup.getStreet())
self.setCity(city: lookup.getCity())
self.setState(state: lookup.getState())
self.setZipcode(zipcode: lookup.getZipcode())
self.setFreeform(freeform: lookup.getFreeform())
self.setEtag(etag: lookup.getEtag())
for key in lookup.getCustomParamArray().keys {
self.addCustomParameter(parameter: key, value: lookup.getCustomParamArray()[key] ?? "")
}
}

override public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
self.results = serializer.Deserialize(payload: payload, error: &error.pointee) as? [PrincipalResult]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class SecondaryCountEnrichmentLookup: EnrichmentLookup {
super.init(smartyKey: smartyKey, datasetName: "secondary", dataSubsetName: "count")
}

init(lookup: EnrichmentLookup) {
self.results = nil
super.init(smartyKey: lookup.getSmartyKey(), datasetName: "secondary", dataSubsetName: "count")
for key in lookup.getIncludeAttributes() {
self.addIncludeAttribute(attribute: key)
}
for key in lookup.getExcludeAttributes() {
self.addExcludeAttribute(attribute: key)
}
self.setStreet(street: lookup.getStreet())
self.setCity(city: lookup.getCity())
self.setState(state: lookup.getState())
self.setZipcode(zipcode: lookup.getZipcode())
self.setFreeform(freeform: lookup.getFreeform())
self.setEtag(etag: lookup.getEtag())
for key in lookup.getCustomParamArray().keys {
self.addCustomParameter(parameter: key, value: lookup.getCustomParamArray()[key] ?? "")
}
}

override public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
self.results = serializer.Deserialize(payload: payload, error: &error.pointee) as? [SecondaryCountResult]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class SecondaryEnrichmentLookup: EnrichmentLookup {
super.init(smartyKey: smartyKey, datasetName: "secondary", dataSubsetName: "")
}

init(lookup: EnrichmentLookup) {
self.results = nil
super.init(smartyKey: lookup.getSmartyKey(), datasetName: "secondary", dataSubsetName: "")
for key in lookup.getIncludeAttributes() {
self.addIncludeAttribute(attribute: key)
}
for key in lookup.getExcludeAttributes() {
self.addExcludeAttribute(attribute: key)
}
self.setStreet(street: lookup.getStreet())
self.setCity(city: lookup.getCity())
self.setState(state: lookup.getState())
self.setZipcode(zipcode: lookup.getZipcode())
self.setFreeform(freeform: lookup.getFreeform())
self.setEtag(etag: lookup.getEtag())
for key in lookup.getCustomParamArray().keys {
self.addCustomParameter(parameter: key, value: lookup.getCustomParamArray()[key] ?? "")
}
}

override public func deserializeAndSetResults(serializer: SmartySerializer, payload: Data, error: UnsafeMutablePointer<NSError?>) {
self.results = serializer.Deserialize(payload: payload, error: &error.pointee) as? [SecondaryResult]
}
Expand Down
Loading

0 comments on commit 67f2c5a

Please sign in to comment.