Skip to content

Instantly share code, notes, and snippets.

@jamesrochabrun
Last active June 11, 2021 02:58
Show Gist options
  • Save jamesrochabrun/00095f3e223c61d8b38d019631e222c3 to your computer and use it in GitHub Desktop.
Save jamesrochabrun/00095f3e223c61d8b38d019631e222c3 to your computer and use it in GitHub Desktop.

Revisions

  1. jamesrochabrun renamed this gist Jun 11, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions GenericAsyncAwaitAPI.swift → AsyncGenericAPI.swift
    Original file line number Diff line number Diff line change
    @@ -21,14 +21,14 @@ enum APIError: Error {
    }
    }

    protocol GenericAPI {
    protocol AsyncGenericAPI {
    var session: URLSession { get }
    func fetch<T: Decodable>(
    type: T.Type,
    with request: URLRequest) async throws -> T
    }

    extension GenericAPI {
    extension AsyncGenericAPI {

    func fetch<T: Decodable>(
    type: T.Type,
  2. jamesrochabrun revised this gist Jun 10, 2021. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions GenericAsyncAwaitAPI.swift
    Original 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

    let (data, response) = try await session.data(for: request) // 2
    // 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()
    return try decoder.decode(type, from: data) // 3
    // 3
    return try decoder.decode(type, from: data)
    } catch {
    throw APIError.jsonConversionFailure(description: error.localizedDescription) // 4
    // 4
    throw APIError.jsonConversionFailure(description: error.localizedDescription)
    }
    }
    }
  3. jamesrochabrun revised this gist Jun 10, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion GenericAsyncAwaitAPI.swift
    Original 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 {
  4. jamesrochabrun created this gist Jun 10, 2021.
    52 changes: 52 additions & 0 deletions GenericAsyncAwaitAPI.swift
    Original 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
    }
    }
    }