Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
typealias DCRequestType = (URLRequest, Error)
//basically makePostRequest should be a function that returns a URLRequest. Straightforward.
// I want the return type to be either a URlRequest or an Error. <URLRequest, Error> something of this kind.
// You see what I'm doing right?
fileprivate func makePostRequest(apiUrl: String, params: [String: String]) -> <URLRequest, Error> {
// build url
let urlString = "\(dcoderURL)\(apiUrl)"
guard let serviceUrl = URL(string: urlString) else { return nil }
// build url request
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
//set http body for url request with incoming params
guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else {
return nil
}
request.httpBody = httpBody
return request
}
}
//The code about would make lines 29-36 obsolete with just one go. The error can be consumed here to actually throw it back to the UI
// func sendRegistrationCode(userEmail:String, username:String, userPassword:String, userUsername:String, completion: @escaping (SendCodeModel?, Error?) -> ()) {
// let urlString = "\(dcoderURL)\(sendRegistrationCodeURL)"
// let params = ["user_email":userEmail,"user_name":username,"user_password":userPassword,"user_username":userUsername]
// guard let serviceUrl = URL(string: urlString) else { return }
// var request = URLRequest(url: serviceUrl)
// request.httpMethod = "POST"
// request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
// guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else {
// return
// }
// request.httpBody = httpBody
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment