Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 36 additions & 39 deletions Sources/SwiftRestRequests/RestApiCaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,9 @@ open class RestApiCaller : NSObject {
let httpStatus = httpResponse.status

// For requests without deserialization and no error just return the status
if type(of: responseDeserializer) == VoidDeserializer.self || httpStatus == .noContent {
if httpStatus.type == .success {
return (nil, httpResponse.status)
}
if shouldBypassDeserialization(responseDeserializer, status: httpStatus),
httpStatus.type == .success {
return (nil, httpStatus)
}

guard !data.isEmpty else {
Expand All @@ -291,52 +290,50 @@ open class RestApiCaller : NSObject {

// Postcondition: We have a response object or error that needs to be parsed!

let contentType = httpResponse.value(forHTTPHeaderField: HTTPHeaderKeys.ContentType.rawValue)
_ = try validatedMimeType(from: httpResponse)

// Note: some servers return also encoding i.e. Content-Type: application/json; charset=utf-8 take the first part
if httpStatus.type == .success {
let transformedResponse = try decodeSuccessfulResponse(data: data, response: httpResponse, deserializer: responseDeserializer)
return (transformedResponse, httpStatus)
}

throw try buildErrorResponse(data: data, response: httpResponse, status: httpStatus)
}

private func shouldBypassDeserialization<T: Deserializer>(_ deserializer: T, status: HTTPStatusCode) -> Bool {
(deserializer is VoidDeserializer) || status == .noContent
}

private func validatedMimeType(from response: HTTPURLResponse) throws -> MimeType {
let contentType = response.value(forHTTPHeaderField: HTTPHeaderKeys.ContentType.rawValue)
let firstContentMimeType = contentType?.components(separatedBy: ";").first

guard let firstContentMimeType, let _ = MimeType(rawValue: firstContentMimeType) else {
guard let firstContentMimeType,
let mimeType = MimeType(rawValue: firstContentMimeType) else {
throw RestError.invalidMimeType(contentType)
}

// Postcondition: Response or error ContentTyp is supported

if httpStatus.type == .success {

// Postcondition: httpStatus in 200...299
if httpStatus == .ok {
// Postcondition: httpStatus is 200 we need to deserialize
do {
let transformedResponse = try responseDeserializer.deserialize(data)
return (transformedResponse, httpResponse.status)
} catch {
throw RestError.malformedResponse(httpResponse, data, error)
}
} else {
// Postcondition: httpStatus is 201...299
// Note: we skipt data in this case
return (nil, httpStatus)
}

} else {

// Postcondition: httpStatus not 2XX. We have an error and error data
var failedRestCallError: RestError

return mimeType
}

private func decodeSuccessfulResponse<T: Deserializer>(data: Data, response: HTTPURLResponse, deserializer: T) throws -> T.ResponseType? {
if response.status == .ok {
do {
let errorJson = try errorDeserializer?.deserialize(data)
failedRestCallError = RestError.failedRestCall(httpResponse, httpStatus, error: errorJson)
return try deserializer.deserialize(data)
} catch {
throw RestError.malformedResponse(httpResponse, data, error)
throw RestError.malformedResponse(response, data, error)
}

throw failedRestCallError

}
return nil
}

private func buildErrorResponse(data: Data, response: HTTPURLResponse, status: HTTPStatusCode) throws -> RestError {
do {
let errorPayload = try errorDeserializer?.deserialize(data)
return RestError.failedRestCall(response, status, error: errorPayload)
} catch {
throw RestError.malformedResponse(response, data, error)
}
}


// MARK: Public API that can be used from other classes or subclass
Expand Down