-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from thefuntasty/housekeep/urlsession-adapter-f…
…ile-separation Housekeep: URLSession adapter file separation
- Loading branch information
Showing
7 changed files
with
142 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// URLSessionAPIAdapter.swift | ||
// FTAPIKit | ||
// | ||
// Created by Matěj Kašpar Jirásek on 10/11/2018. | ||
// Copyright © 2018 FUNTASTY Digital s.r.o. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Standard and default implementation of `APIAdapter` protocol using `URLSession`. | ||
public final class URLSessionAPIAdapter: APIAdapter { | ||
|
||
/// Custom error constructor typealias receiving values from data task execution | ||
/// and JSON decoder, if it needs to decode custom error from returned JSON. | ||
public typealias ErrorConstructor = (Data?, URLResponse?, Error?, JSONDecoder) -> Error? | ||
|
||
public weak var delegate: APIAdapterDelegate? | ||
|
||
private let urlSession: URLSession | ||
private let baseUrl: URL | ||
|
||
private let jsonEncoder: JSONEncoder | ||
private let jsonDecoder: JSONDecoder | ||
|
||
private let customErrorConstructor: ErrorConstructor? | ||
|
||
private var runningRequestCount: UInt = 0 { | ||
didSet { | ||
guard let delegate = delegate else { return } | ||
DispatchQueue.main.async { | ||
delegate.apiAdapter(self, didUpdateRunningRequestCount: self.runningRequestCount) | ||
} | ||
} | ||
} | ||
|
||
/// Constructor for `APIAdapter` based on `URLSession`. | ||
/// | ||
/// - Parameters: | ||
/// - baseUrl: Base URI for the server for all API calls this API adapter will be executing. | ||
/// - jsonEncoder: Optional JSON encoder used for serialization of JSON models. | ||
/// - jsonDecoder: Optional JSON decoder used for deserialization of JSON models. | ||
/// - customErrorConstructor: Optional custom error constructor if we want the API adapter to not return | ||
/// the standard `APIError`, but to handle the errors our own way. | ||
/// - urlSession: Optional URL session (otherwise the standard one will be used). Used mainly if we need | ||
/// our own `URLSessionConfiguration` or another way of caching (ephemeral session). | ||
public init(baseUrl: URL, jsonEncoder: JSONEncoder = JSONEncoder(), jsonDecoder: JSONDecoder = JSONDecoder(), customErrorConstructor: ErrorConstructor? = nil, urlSession: URLSession = .shared) { | ||
self.baseUrl = baseUrl | ||
self.jsonDecoder = jsonDecoder | ||
self.jsonEncoder = jsonEncoder | ||
self.customErrorConstructor = customErrorConstructor | ||
self.urlSession = urlSession | ||
} | ||
|
||
public func request<Endpoint: APIResponseEndpoint>(response endpoint: Endpoint, completion: @escaping (APIResult<Endpoint.Response>) -> Void) { | ||
request(data: endpoint) { result in | ||
switch result { | ||
case .value(let data): | ||
do { | ||
let model = try self.jsonDecoder.decode(Endpoint.Response.self, from: data) | ||
completion(.value(model)) | ||
} catch { | ||
completion(.error(error)) | ||
} | ||
case .error(let error): | ||
completion(.error(error)) | ||
} | ||
} | ||
} | ||
|
||
public func request(data endpoint: APIEndpoint, completion: @escaping (APIResult<Data>) -> Void) { | ||
let url = baseUrl.appendingPathComponent(endpoint.path) | ||
var request = URLRequest(url: url) | ||
request.httpMethod = endpoint.method.description | ||
|
||
do { | ||
try request.setRequestType(endpoint.type, parameters: endpoint.parameters, using: jsonEncoder) | ||
} catch { | ||
completion(.error(error)) | ||
return | ||
} | ||
|
||
if let delegate = delegate { | ||
delegate.apiAdapter(self, willRequest: request, to: endpoint) { result in | ||
switch result { | ||
case .value(let request): | ||
self.send(request: request, completion: completion) | ||
case .error(let error): | ||
completion(.error(error)) | ||
} | ||
} | ||
} else { | ||
send(request: request, completion: completion) | ||
} | ||
} | ||
|
||
private func send(request: URLRequest, completion: @escaping (APIResult<Data>) -> Void) { | ||
runningRequestCount += 1 | ||
resumeDataTask(with: request) { result in | ||
self.runningRequestCount -= 1 | ||
completion(result) | ||
} | ||
} | ||
|
||
private func resumeDataTask(with request: URLRequest, completion: @escaping (APIResult<Data>) -> Void) { | ||
let task = urlSession.dataTask(with: request) { [customErrorConstructor, jsonDecoder] data, response, error in | ||
if let constructor = customErrorConstructor, let error = constructor(data, response, error, jsonDecoder) { | ||
completion(.error(error)) | ||
return | ||
} | ||
switch (data, response, error) { | ||
case let (_, response as HTTPURLResponse, _) where response.statusCode == 204: | ||
completion(.value(Data())) | ||
case let (data?, response as HTTPURLResponse, nil) where response.statusCode < 400: | ||
completion(.value(data)) | ||
case let (_, _, error?): | ||
completion(.error(error)) | ||
case let (data, response as HTTPURLResponse, nil): | ||
completion(.error(APIError.errorCode(response.statusCode, data))) | ||
default: | ||
completion(.error(APIError.noResponse)) | ||
} | ||
} | ||
task.resume() | ||
} | ||
} |