Skip to content

Commit

Permalink
Simplify execute implementation with urlSession async api
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Nov 14, 2023
1 parent 190d35e commit e9cb882
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions Sources/Networking/NetworkingRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,17 @@ public class NetworkingRequest<E: Encodable>: NSObject, URLSessionTaskDelegate {
logger.log(request: urlRequest)
let config = sessionConfiguration ?? URLSessionConfiguration.default
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
return try await withCheckedThrowingContinuation { continuation in
urlSession.dataTask(with: urlRequest) { data, response, error in
guard let data = data, let response = response else {
if let error = error {
continuation.resume(throwing: error)
}
return
}
self.logger.log(response: response, data: data)
if let httpURLResponse = response as? HTTPURLResponse {
if !(200...299 ~= httpURLResponse.statusCode) {
var error = NetworkingError(errorCode: httpURLResponse.statusCode)
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
error.jsonPayload = json
}
continuation.resume(throwing: error)
return
}
}
continuation.resume(returning: data)
}.resume()
let (data, response) = try await urlSession.data(for: urlRequest)
self.logger.log(response: response, data: data)
if let httpURLResponse = response as? HTTPURLResponse, !(200...299 ~= httpURLResponse.statusCode) {
var error = NetworkingError(errorCode: httpURLResponse.statusCode)
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
error.jsonPayload = json
}
throw error
}
return data

}

private func getURLWithParams() -> String {
Expand Down

0 comments on commit e9cb882

Please sign in to comment.