Skip to content

Commit

Permalink
Inject custom instance of the JSONDecoder from the WKRequest
Browse files Browse the repository at this point in the history
Allows using a custom instance of the Decoder to be used in each request
  • Loading branch information
afterxleep committed Dec 6, 2021
1 parent 8d46abd commit e1feeb4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions Sources/WireKit/Protocols/WKRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public protocol WKRequest {
var queryParams: WKHTTPParams? { get }
var body: WKHTTPParams? { get }
var headers: WKHTTPHeaders? { get }
var decoder: JSONDecoder? { get }
}

public extension WKRequest {
Expand Down
4 changes: 2 additions & 2 deletions Sources/WireKit/WKAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public struct WKAPIClient {
public func dispatch<Request: WKRequest>(_ request: Request) -> AnyPublisher<Request.ReturnType, WKNetworkRequestError> {
guard let urlRequest = request.asURLRequest(baseURL: baseURL) else {
return Fail(outputType: Request.ReturnType.self, failure: WKNetworkRequestError.badRequest).eraseToAnyPublisher()

}

typealias RequestPublisher = AnyPublisher<Request.ReturnType, WKNetworkRequestError>
let requestPublisher: RequestPublisher = networkDispatcher.dispatch(request: urlRequest)
let requestPublisher: RequestPublisher = networkDispatcher.dispatch(request: urlRequest, decoder: request.decoder)
return requestPublisher.eraseToAnyPublisher()
}
}
9 changes: 6 additions & 3 deletions Sources/WireKit/WKNetworkDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public struct WKNetworkDispatcher {
/// Dispatches an URLRequest and returns a publisher
/// - Parameter request: URLRequest
/// - Returns: A publisher with the provided decoded data or an error
public func dispatch<ReturnType: Codable>(request: URLRequest) -> AnyPublisher<ReturnType, WKNetworkRequestError> {

public func dispatch<ReturnType: Codable>(request: URLRequest, decoder: JSONDecoder?) -> AnyPublisher<ReturnType, WKNetworkRequestError> {

let decoder = decoder ?? JSONDecoder()

return urlSession
.dataTaskPublisher(for: request)
.tryMap({ data, response in
Expand All @@ -45,7 +47,7 @@ public struct WKNetworkDispatcher {
}
return data
})
.decode(type: ReturnType.self, decoder: JSONDecoder())
.decode(type: ReturnType.self, decoder: decoder)
.mapError { error in
handleError(error)
}
Expand Down Expand Up @@ -74,6 +76,7 @@ public struct WKNetworkDispatcher {
/// - Parameter error: URLSession publisher error
/// - Returns: Readable NWKNetworkRequestError
private func handleError(_ error: Error) -> WKNetworkRequestError {
print(error)
switch error {
case is Swift.DecodingError:
return .decodingError
Expand Down
2 changes: 2 additions & 0 deletions docs/wkrequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ All your API requests should go through `WKRequest`. Here's the parameter list.
### Headers
`[String: String]` - Dictionary of headers to be sent. By default Wirekit automatically adds `Content-Type` and `Accept` headers so you don't have to manually addd them here.

### Decoder
`JSONDecoder` - Custom instance of JSONDecoder to be used then decoding this particular request. This is useful to implement custom decoding strategies

0 comments on commit e1feeb4

Please sign in to comment.