ServiceHook

public protocol ServiceHook

A service hook represents a component that is called at specific events during the processing of a request. A service hook can be attached to a service to e.g. provide caching behavior.

  • onSchedule(_:_:) Default implementation

    The method is called right before a request is scheduled. It may return the result type of a request to prematurely fulfill it. That means, whenever a non-nil value is returned, the request is not sent over the wire but the returned value will be returned instead. No failures can occur after returning a non-nil value here.

    Note

    When calling schedule(with:) on a request, this method is not guaranteed to be called. If building the URL request fails for some reason (e.g. the body cannot be constructed), no method invocation will occur.

    Attention

    Internally, the request is actually started to be sent simultaneously with this method call. If the response of this function takes too long, the server’s response might get called. As soon as this method returns, however, the request will be cancelled.

    Default Implementation

    By default, nil is returned always, allowing the request to be sent to the server.

    Declaration

    Swift

    func onSchedule<R>(_ request: R, _ urlRequest: URLRequest) -> R.Result? where R : Request

    Parameters

    request

    The request object that was scheduled.

    urlRequest

    The URL request that would be sent to the server. Contains all headers, the exact body, etc.

  • onSuccess(_:_:result:) Default implementation

    This method is called whenever a request was successfully finished, i.e. a valid response has been recorded from the server. This method will not be called when the result was returned by onSchedule(_:_:).

    Default Implementation

    By default, no operation is performed.

    Declaration

    Swift

    func onSuccess<R>(_ request: R, _ urlRequest: URLRequest, result: R.Result) where R : Request

    Parameters

    request

    The request object that was scheduled.

    urlRequest

    The URL request that was sent to the server.

    result

    The decoded result returned by the server.

  • onFailure(_:) Default implementation

    This method is called whenever a failure occurs when scheduling a Request or a StreamRequest. The error might indicate e.g. network outage that should be displayed to the user.

    Default Implementation

    By default, no operation is performed.

    Declaration

    Swift

    func onFailure(_ error: Error)

    Parameters

    error

    The error that caused a request to fail.