Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpClient] add DecoratorTrait for response #53332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

cyve
Copy link
Contributor

@cyve cyve commented Dec 31, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

When we decorate the HttpClient, we often need to create a dedicated class to wrap the response (ex TraceableHttpClient => TraceableResponse)

Following the idea introduced in #41161, I propose to add a trait to simplify the response decoration.

Example:

class MyCustomHttpClient implements HttpClientInterface
{
    use DecoratorTrait;

    public function request(string $method, string $url, array $options = []): ResponseInterface
    {
        return new MyCustomResponse($this->client->request($method, $url, $options));
    }
}

class MyCustomResponse implements ResponseInterface
{
    use Response\DecoratorTrait;
    // overwrite only necessary methods
}

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 2, 2024

Hello, thanks for proposing. Can you explain your use case?
The AsyncDecoratorTrait + AsyncResponse combo is a way better alternative to me since it also works with $client->stream(), while simple response decorators don't.

@cyve cyve force-pushed the http-client-response-decorator branch from 7d838a2 to c10f850 Compare January 6, 2024 00:11
@cyve
Copy link
Contributor Author

cyve commented Jan 6, 2024

Hi @nicolas-grekas, thanks for your feedback. I took the opportunity to take a deeper look at the AsyncResponse concept. It looks really nice. I was able to do almost everything I wanted to do with response wrappers.

My last use case is a caching client decorator. Same as the internal CachingHttpClient, but with a custom cache solution instead of HttpCache. We need to add extra headers to the cached response (age, ID, etc.). So we created a wrapper for the response and decorate the getHeaders() method.
With the AsyncResponse, I was able to modify the content of the original response (by yielding extra chunks in the passthru), but not the other information (status code, headers and debug infos). Do you know how I can do that ?

I saw that the AsyncContext has a replaceResponse() method. But it seems a bit overkill to recreate an entire response. And the method seem to be designed to return the result of a second HTTP request, not overwrite the first one. Maybe adding replaceResponseHeaders() and replaceResponseInfo() methods to AsyncContext will do the trick ? What do you think ?

I don't know if it worth it, but I added a static stream() method to the DecoratorTrait to make it work with $client->stream().

@cyve cyve force-pushed the http-client-response-decorator branch from c10f850 to 1a41eed Compare January 6, 2024 00:30
@cyve cyve closed this Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.