Refactor logout() to use template request
This commit is contained in:
parent
0c0f76b31b
commit
50bb2072fa
|
@ -885,51 +885,15 @@ public class WFClient {
|
||||||
request.addValue(tokenToDelete, forHTTPHeaderField: "Authorization")
|
request.addValue(tokenToDelete, forHTTPHeaderField: "Authorization")
|
||||||
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
|
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
|
||||||
|
|
||||||
let dataTask = session.dataTask(with: request) { (data, response, error) in
|
delete(with: request) { result in
|
||||||
// Something went wrong; return the error message.
|
switch result {
|
||||||
if let error = error {
|
case .success(_):
|
||||||
// ⚠️ HACK: There's something that URLSession doesn't like about 204 NO CONTENT response that the API
|
self.user = nil
|
||||||
// server is returning. If we get back a "protocol error", the operation probably succeeded,
|
completion(.success(true))
|
||||||
// but URLSession is being pedantic/cranky and throwing an NSPOSIXErrorDomain error code 100.
|
case .failure(let error):
|
||||||
// Here, we check for that error, make sure the token was invalidated, and only then fire the
|
completion(.failure(error))
|
||||||
// success case in the completion block.
|
|
||||||
let nsError = error as NSError
|
|
||||||
if nsError.code == 100 && nsError.domain == NSPOSIXErrorDomain {
|
|
||||||
// Confirm that the operation succeeded by testing for a 404 on the same token.
|
|
||||||
self.logout(token: tokenToDelete) { result in
|
|
||||||
do {
|
|
||||||
_ = try result.get()
|
|
||||||
completion(.failure(error))
|
|
||||||
} catch WFError.notFound {
|
|
||||||
self.user = nil
|
|
||||||
completion(.success(true))
|
|
||||||
} catch WFError.unauthorized {
|
|
||||||
self.user = nil
|
|
||||||
completion(.success(true))
|
|
||||||
} catch {
|
|
||||||
completion(.failure(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
completion(.failure(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let response = response as? HTTPURLResponse {
|
|
||||||
// We got a response. If it's a 204 NO CONTENT, return true as success;
|
|
||||||
// if not, return a WFError as failure.
|
|
||||||
if response.statusCode != 204 {
|
|
||||||
guard let data = data else { return }
|
|
||||||
guard let error = self.translateWFError(fromServerResponse: data) else { return }
|
|
||||||
completion(.failure(error))
|
|
||||||
} else {
|
|
||||||
self.user = nil
|
|
||||||
completion(.success(true))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dataTask.resume()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves a user's basic data.
|
/// Retrieves a user's basic data.
|
||||||
|
|
Loading…
Reference in New Issue