Refactor deleteCollection() to use template request

This commit is contained in:
Angelo Stavrow 2021-05-11 16:43:43 -04:00
parent 50bb2072fa
commit 013207a140
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
1 changed files with 6 additions and 40 deletions

View File

@ -175,48 +175,14 @@ public class WFClient {
request.addValue(tokenToVerify, forHTTPHeaderField: "Authorization")
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let dataTask = session.dataTask(with: request) { (data, response, error) in
// Something went wrong; return the error message.
if let error = error {
// HACK: There's something that URLSession doesn't like about 204 NO CONTENT response that the API
// server is returning. If we get back a "protocol error", the operation probably succeeded,
// but URLSession is being pedantic/cranky and throwing an NSPOSIXErrorDomain error code 100.
// Here, we check for that error, make sure the token was invalidated, and only then fire the
// 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.deleteCollection(withAlias: alias) { result in
do {
_ = try result.get()
completion(.failure(error))
} catch WFError.notFound {
delete(with: request) { result in
switch result {
case .success(_):
completion(.success(true))
} catch WFError.unauthorized {
completion(.success(true))
} catch {
case .failure(let error):
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 {
completion(.success(true))
}
}
}
dataTask.resume()
}
// MARK: - Post-related methods