Last active
August 1, 2019 13:58
-
-
Save Andrea-Scuderi/efb5fb59c7d0296911944fcc8e7dc2ac to your computer and use it in GitHub Desktop.
Vapor API client for auth-template: Create User - part 4
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
// Refactoring of the URLRequest with dataTask avoiding Throws using fatalError | |
func buildCreateUserURLRequest(user: User) -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
] | |
let encoder = JSONEncoder() | |
guard let postData = try? encoder.encode(user) else { | |
fatalError("APIError.invalidBody") | |
} | |
guard let url = URL(string: baseURL + "/users" ) else { | |
fatalError("APIError.invalidEndpoint") | |
} | |
var request = URLRequest(url: url, | |
cachePolicy: .useProtocolCachePolicy, | |
timeoutInterval: 10.0) | |
request.httpMethod = "POST" | |
request.allHTTPHeaderFields = headers | |
request.httpBody = postData as Data | |
return request | |
} | |
// Our old API client dataTask refactoring | |
func createUserOLDRefactoring(user: User, session: URLSession = URLSession.shared, completion: @escaping (Data?, URLResponse?, Error?) -> Void) { | |
let request = buildCreateUserURLRequest(user: user) | |
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: completion) | |
dataTask.resume() | |
} | |
// Reuse your refactored old API client to implement a Publisher | |
func createUserPublisher(user: User, session: URLSession = URLSession.shared) -> Future<CreateUserResponse, Error> { | |
let future = Future<CreateUserResponse, Error>.init { (promise) in | |
let completion: (Data?, URLResponse?, Error?) -> () = { (data, response, error) in | |
//Response Validation | |
guard let httpResponse = response as? HTTPURLResponse else { | |
promise(.failure(APIError.invalidResponse)) | |
return | |
} | |
guard (200..<300).contains(httpResponse.statusCode) else { | |
promise(.failure(APIError.statusCode(httpResponse.statusCode))) | |
return | |
} | |
// Decoding data | |
let decoder = JSONDecoder() | |
do { | |
if let data = data { | |
let response = try decoder.decode(CreateUserResponse.self, from: data) | |
promise(.success(response)) | |
} else { | |
promise(.failure(APIError.emptyData)) | |
} | |
} catch (let error) { | |
promise(.failure(error)) | |
} | |
} | |
//Execute the request | |
createUserOLDRefactoring(user: user, session: session, completion: completion) | |
} | |
return future | |
} | |
// Usage of the refactored old API client Publisher | |
let createUser6Publisher = createUserPublisher(user: User(id: 6)) | |
let cancellable3 = createUser6Publisher | |
.sink(receiveCompletion: { (completion) in | |
switch completion { | |
case .failure(let error): | |
print(error) | |
case .finished: | |
print("DONE - createUser6Publisher") | |
} | |
}, receiveValue: { reponse in | |
print(reponse) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment