ModifyRequestNetworkService

public final class ModifyRequestNetworkService : NetworkService

ModifyRequestNetworkService can be composed with a network service to modify all outgoing requests. One could add auth tokens or API keys for specifics URLs.

Example:

let networkService: NetworkService = //
let modifyRequestNetworkService = ModifyRequestNetworkService(networkService: networkService, requestModifications: [ { request in
   return request.added(HTTPHeaderFields: ["API-Key": "SecretKey"])
}])

Note

Requests can only be modified syncronously.

Seealso

NetworkService
  • Creates an insatcne of ModifyRequestNetworkService.

    Declaration

    Swift

    public init(networkService: NetworkService, requestModifications: [(URLRequest) -> URLRequest])

    Parameters

    networkService

    a networkservice.

    requestModifications

    array of modifications to modify requests.

  • Fetches a resource asynchronously from remote location. Execution of the requests starts immediately. Execution happens on no specific queue. It dependes on the network access which queue is used. Once execution is finished either the completion block or the error block gets called. You decide on which queue these blocks get executed.

    Example:

    let networkService: NetworkService = //
    let resource: Resource<String> = //
    
    networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in
       print(htmlText, response)
    }, onError: { error in
       // Handle errors
    })
    

    Declaration

    Swift

    @discardableResult
    public func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void,
                        onError: @escaping (NetworkError) -> Void) -> NetworkTask

    Parameters

    queue

    The DispatchQueue to execute the completion and error block on.

    resource

    The resource you want to fetch.

    onCompletionWithResponse

    Callback which gets called when fetching and transforming into model succeeds.

    onError

    Callback which gets called when fetching or transforming fails.

    Return Value

    a running network task