-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add info about HTTP client events. #7769
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -479,6 +479,59 @@ instead. You can force select a transport adapter using a constructor option:: | |||||
|
|
||||||
| $client = new Client(['adapter' => Stream::class]); | ||||||
|
|
||||||
| Events | ||||||
| ====== | ||||||
|
|
||||||
| The HTTP client triggers couple of events before and after sending a request | ||||||
| which allows you to modify either the request or response or do other tasks like | ||||||
| caching, logging etc. | ||||||
|
|
||||||
| HttpClient.beforeSend | ||||||
| --------------------- | ||||||
|
|
||||||
| // Somewhere before calling one of the HTTP client's methods which makes a request | ||||||
| $http->getEventManager()->on( | ||||||
| 'HttpClient.beforeSend', | ||||||
| function ( | ||||||
| \Cake\Http\Client\ClientEvent $event, | ||||||
| \Cake\Http\Client\Request $request, | ||||||
| array $adapterOptions, | ||||||
| int $redirects | ||||||
| ) { | ||||||
| // Modify the request | ||||||
| $event->setRequest(....); | ||||||
| // Modify the adapter options | ||||||
| $event->setAdapterOptions(....); | ||||||
|
|
||||||
| // Skip making the actual request by returning a response. | ||||||
| // You can use $event->setResult($response) to achieve the same. | ||||||
| return new \Cake\Http\Client\Response(body: 'something'); | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| HttpClient.afterSend | ||||||
| --------------------- | ||||||
|
|
||||||
| // Somewhere before calling one of the HTTP client's methods which makes a request | ||||||
| $http->getEventManager()->on( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 'HttpClient.afterSend', | ||||||
| function ( | ||||||
| \Cake\Http\Client\ClientEvent $event, | ||||||
| \Cake\Http\Client\Request $request, | ||||||
| array $adapterOptions, | ||||||
| int $redirects, | ||||||
| bool $requestSent // Indicates whether the request was actually sent | ||||||
| // or response returned from ``beforeSend`` event | ||||||
| ) { | ||||||
| // Get the response | ||||||
| $response = $event->getResponse(); | ||||||
|
|
||||||
| // Return a new/modified response. | ||||||
| // You can use $event->setResult($response) to achieve the same. | ||||||
| return new \Cake\Http\Client\Response(body: 'something'); | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| .. _httpclient-testing: | ||||||
|
|
||||||
| Testing | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All code examples above use the var
$httpfor the client instance, hence I used the same for consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example on 480 uses $client. I can take care of normalizing them all though.