Last active
June 11, 2021 02:58
-
-
Save jamesrochabrun/00095f3e223c61d8b38d019631e222c3 to your computer and use it in GitHub Desktop.
Revisions
-
jamesrochabrun renamed this gist
Jun 11, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,14 +21,14 @@ enum APIError: Error { } } protocol AsyncGenericAPI { var session: URLSession { get } func fetch<T: Decodable>( type: T.Type, with request: URLRequest) async throws -> T } extension AsyncGenericAPI { func fetch<T: Decodable>( type: T.Type, -
jamesrochabrun revised this gist
Jun 10, 2021 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,8 @@ extension GenericAPI { type: T.Type, with request: URLRequest) async throws -> T { // 1 // 2 let (data, response) = try await session.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { throw APIError.requestFailed(description: "unvalid response") } @@ -43,9 +44,11 @@ extension GenericAPI { } do { let decoder = JSONDecoder() // 3 return try decoder.decode(type, from: data) } catch { // 4 throw APIError.jsonConversionFailure(description: error.localizedDescription) } } } -
jamesrochabrun revised this gist
Jun 10, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,6 @@ extension GenericAPI { func fetch<T: Decodable>( type: T.Type, with request: URLRequest) async throws -> T { // 1 let (data, response) = try await session.data(for: request) // 2 guard let httpResponse = response as? HTTPURLResponse else { -
jamesrochabrun created this gist
Jun 10, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ enum APIError: Error { case requestFailed(description: String) case jsonConversionFailure(description: String) case invalidData case responseUnsuccessful(description: String) case jsonParsingFailure case noInternet case failedSerialization var customDescription: String { switch self { case let .requestFailed(description): return "Request Failed error -> \(description)" case .invalidData: return "Invalid Data error)" case let .responseUnsuccessful(description): return "Response Unsuccessful error -> \(description)" case .jsonParsingFailure: return "JSON Parsing Failure error)" case let .jsonConversionFailure(description): return "JSON Conversion Failure -> \(description)" case .noInternet: return "No internet connection" case .failedSerialization: return "serialization print for debug failed." } } } protocol GenericAPI { var session: URLSession { get } func fetch<T: Decodable>( type: T.Type, with request: URLRequest) async throws -> T } extension GenericAPI { func fetch<T: Decodable>( type: T.Type, with request: URLRequest) async throws -> T { // 1 let (data, response) = try await session.data(for: request) // 2 guard let httpResponse = response as? HTTPURLResponse else { throw APIError.requestFailed(description: "unvalid response") } guard httpResponse.statusCode == 200 else { throw APIError.responseUnsuccessful(description: "status code \(httpResponse.statusCode)") } do { let decoder = JSONDecoder() return try decoder.decode(type, from: data) // 3 } catch { throw APIError.jsonConversionFailure(description: error.localizedDescription) // 4 } } }