Created
August 9, 2016 08:30
-
-
Save francisjervis/d3e2cd86e93613906c13bc98851c557e to your computer and use it in GitHub Desktop.
Swift 2 friendly OnFido class
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
class OnFido { | |
// var apiKey: String? | |
class candidate { | |
var firstName: String? | |
var lastName: String? | |
var dateOfBirth: NSDate? | |
var address1: String? | |
var address2: String? | |
var houseNumber: String? | |
var apartmentNumber: String? | |
var city: String? | |
var country: String? | |
var candidateID: String? | |
var candidateAppId: String? | |
init(withCandidateId: String!) { | |
self.candidateID = withCandidateId | |
} | |
init() { | |
} | |
enum apiError: ErrorType { | |
case HTTPError(errorMessage: String) | |
case onFidoError(errorMessage: String) | |
case candidateIdExists(errorMessage: String) | |
} | |
func createCandidate(withApiKey: String!) throws -> (candidate?) { | |
if candidateID != nil { | |
throw apiError.candidateIdExists(errorMessage: "Candidate \(candidateID) already exists") | |
} | |
let apiKey = withApiKey | |
let url = "https://api.onfido.com/v2/applicants" | |
let request = NSMutableURLRequest(URL: NSURL(string: url)!) | |
request.HTTPMethod = "POST" | |
request.addValue("Token token=\(apiKey!)", forHTTPHeaderField: "Authorization") | |
var params: [String:String] = [:] | |
if self.firstName != nil { | |
params["first_name"] = firstName! | |
} | |
if self.lastName != nil { | |
params["last_name"] = lastName! | |
} | |
var bodyData = "" | |
for (key, value) in params { | |
let scapedKey = key.stringByAddingPercentEncodingWithAllowedCharacters( | |
.URLHostAllowedCharacterSet())! | |
let scapedValue = value.stringByAddingPercentEncodingWithAllowedCharacters( | |
.URLHostAllowedCharacterSet())! | |
bodyData += "\(scapedKey)=\(scapedValue)&" | |
} | |
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) | |
let newCandidate = candidate() | |
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response, data, urlError) in | |
do { | |
let JSON = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions(rawValue: 0)) | |
guard let JSONDictionary :NSDictionary = JSON as? NSDictionary | |
else { | |
print("Not a Dictionary") | |
return | |
} | |
guard let idHash = JSONDictionary["id"] else { | |
throw apiError.HTTPError(errorMessage: "bar") | |
return | |
} | |
newCandidate.candidateID = idHash as! String | |
newCandidate.firstName = self.firstName | |
newCandidate.lastName = self.lastName | |
} | |
catch { | |
throw apiError.HTTPError(errorMessage: "bar") | |
return | |
} | |
} | |
return newCandidate | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment