Protocols

The following protocols are available globally.

  • HTTPClientResponseDelegate allows an implementation to receive notifications about request processing and to control how response parts are processed. You can implement this protocol if you need fine-grained control over an HTTP request/response, for example, if you want to inspect the response headers before deciding whether to accept a response body, or if you want to stream your request body. Pass an instance of your conforming class to the HTTPClient.execute() method and this package will call each delegate method appropriately as the request takes place./

    Backpressure

    A HTTPClientResponseDelegate can be used to exert backpressure on the server response. This is achieved by way of the futures returned from didReceiveHead and didReceiveBodyPart. The following functions are part of the “backpressure system” in the delegate:

    • didReceiveHead
    • didReceiveBodyPart
    • didFinishRequest
    • didReceiveError

    The first three methods are strictly exclusive, with that exclusivity managed by the futures returned by didReceiveHead and didReceiveBodyPart. What this means is that until the returned future is completed, none of these three methods will be called again. This allows delegates to rate limit the server to a capacity it can manage. didFinishRequest does not return a future, as we are expecting no more data from the server at this time.

    didReceiveError is somewhat special: it signals the end of this regime. didRecieveError is not exclusive: it may be called at any time, even if a returned future is not yet completed. didReceiveError is terminal, meaning that once it has been called none of these four methods will be called again. This can be used as a signal to abandon all outstanding work.

    Note

    This delegate is strongly held by the HTTPTaskHandler for the duration of the Request processing and will be released together with the HTTPTaskHandler when channel is closed. Users of the library are not required to keep a reference to the object that implements this protocol, but may do so if needed.
    See more

    Declaration

    Swift

    public protocol HTTPClientResponseDelegate : AnyObject