Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@cyve
Copy link
Contributor

@cyvecyve commentedDec 31, 2023
edited
Loading

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT

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

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

Example:

class MyCustomHttpClientimplements HttpClientInterface{use DecoratorTrait;publicfunctionrequest(string$method,string$url,array$options = []):ResponseInterface    {returnnewMyCustomResponse($this->client->request($method,$url,$options));    }}class MyCustomResponseimplements ResponseInterface{useResponse\DecoratorTrait;// overwrite only necessary methods}

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJan 2, 2024
edited
Loading

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.

@cyvecyveforce-pushed thehttp-client-response-decorator branch from7d838a2 toc10f850CompareJanuary 6, 2024 00:11
@cyve
Copy link
ContributorAuthor

cyve commentedJan 6, 2024
edited
Loading

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 internalCachingHttpClient, 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 thegetHeaders() 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 theAsyncContext has areplaceResponse() 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 addingreplaceResponseHeaders() andreplaceResponseInfo() methods toAsyncContext will do the trick ? What do you think ?

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

@cyvecyveforce-pushed thehttp-client-response-decorator branch fromc10f850 to1a41eedCompareJanuary 6, 2024 00:30
@cyvecyve closed thisMar 26, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

3 participants

@cyve@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp