Skip to content

Switch HTTPClient response handler from a closure to a protocol#158

Open
guoye-zhang wants to merge 2 commits into
apple:mainfrom
guoye-zhang:response-handler
Open

Switch HTTPClient response handler from a closure to a protocol#158
guoye-zhang wants to merge 2 commits into
apple:mainfrom
guoye-zhang:response-handler

Conversation

@guoye-zhang
Copy link
Copy Markdown
Collaborator

@guoye-zhang guoye-zhang commented May 13, 2026

Opening this to start a discussion. The new signature of perform:

    mutating func perform<ResponseHandler: HTTPClientResponseHandler & ~Copyable, Return: ~Copyable>(
        request: HTTPRequest,
        body: consuming HTTPClientRequestBody<RequestWriter>?,
        options: RequestOptions,
        responseHandler: consuming ResponseHandler
    ) async throws -> Return
    where
        ResponseHandler.ResponseConcludingReader: ~Copyable,
        ResponseHandler.ResponseConcludingReader == ResponseConcludingReader,
        ResponseHandler.Return == Return

Resolves #147

Remaining:

  • Update docc comments
  • Decide what to do with convenience methods
  • Add support for informational responses in URLSession and AHC
  • Update proposal

@guoye-zhang guoye-zhang added the ⚠️ semver/major Breaks existing public API. label May 13, 2026
@_lifetime(&self)
#endif
mutating func perform<Return: ~Copyable>(
mutating func perform<ResponseHandler: HTTPClientResponseHandler & ~Copyable, Return: ~Copyable>(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to get it to work with primary associated types due to SuppressedAssociatedTypesWithDefaults requiring ~Copyable for ResponseHandler.ResponseConcludingReader

ResponseConcludingReader.FinalElement == HTTPFields?
associatedtype Return: ~Copyable

func handleInformational(response: HTTPResponse) async throws
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we expect a user to do in here? Is this just for them to observe it? Can they take any action on an informational response?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the status code. E.g. start sending the request body after 100 continue (the backing implementation can also handle this by delaying calling the closure until 100 continue); prefetching a resource after 103 early hints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ semver/major Breaks existing public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support informational responses in HTTPClient

2 participants