Fixed mapping response that isSuccessful gets set to false if response code is not a 2xx

This commit is contained in:
dankito 2020-07-31 01:13:51 +02:00
parent bfabbcf3ba
commit 8695a1c049
1 changed files with 9 additions and 3 deletions

View File

@ -88,16 +88,22 @@ class UrlSessionWebClient : Fints4kIWebClient {
} }
private func mapResponse(_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Fints4kWebClientResponse { private func mapResponse(_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Fints4kWebClientResponse {
let mappedError = error == nil ? nil : KotlinException(message: error?.localizedDescription)
if let httpResponse = response as? HTTPURLResponse { if let httpResponse = response as? HTTPURLResponse {
let statusCode = Int32(httpResponse.statusCode)
let isSuccessful = mappedError == nil
&& statusCode >= 200 && statusCode <= 299
if let data = data { if let data = data {
return Fints4kWebClientResponse(successful: true, responseCode: Int32(httpResponse.statusCode), error: nil, body: String(data: data, encoding: .ascii)) return Fints4kWebClientResponse(successful: isSuccessful, responseCode: statusCode, error: mappedError, body: String(data: data, encoding: .ascii))
} }
else { else {
return Fints4kWebClientResponse(successful: true, responseCode: Int32(httpResponse.statusCode), error: nil, body: nil) return Fints4kWebClientResponse(successful: isSuccessful, responseCode: statusCode, error: mappedError, body: nil)
} }
} }
else { else {
return Fints4kWebClientResponse(successful: false, responseCode: -1, error: KotlinException(message: error?.localizedDescription), body: nil) return Fints4kWebClientResponse(successful: false, responseCode: -1, error: mappedError, body: nil)
} }
} }