[HttpClient] Add a ScopingHttpClient#30654
Merged
fabpot merged 1 commit intosymfony:masterfrom Mar 23, 2019
Merged
Conversation
e0e82ad to
1ee0a11
Compare
fabpot
approved these changes
Mar 23, 2019
chalasr
approved these changes
Mar 23, 2019
Member
|
Thank you @nicolas-grekas. |
fabpot
added a commit
that referenced
this pull request
Mar 23, 2019
This PR was merged into the 4.3-dev branch. Discussion ---------- [HttpClient] Add a ScopingHttpClient | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR is a follow up of #30592 by @XuruDragon, with two main differences: - I think `ScopingHttpClient` might be a better name for what is called a `ConditionalHttpClient` there, - the `FrameworkBundle` part is removed so that it can be submitted separately later on. With a `ScopingHttpClient`, you can add some default options conditionally based on the requested URL and a regexp that it should match. This allows building clients that add e.g. credentials based on the requested scheme/host/path. When the requested URL is a relative one, a default index can be provided - whose corresponding default options (the `base_uri` one especially) will be used to turn it into an absolute URL. Regexps are anchored on their left side. E.g. this defines a client that will send some github token when a request is made to the corresponding API, and will not send those credentials if any other host is requested, while also turning relative URLs to github ones: ```php $client = HttpClient::create(); $githubClient = new ScopingClient($client, [ 'http://api\.github\.com/' => [ 'base_uri' => 'http://api.github.com/', 'headers' => ['Authorization: token '.$githubToken], ], ], 'http://api\.github\.com/'); ``` Of course, it's possible to define several regexps as keys so that one can create a client that is authenticated against several hosts/paths. Commits ------- 1ee0a11 [HttpClient] Add a ScopingHttpClient
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR is a follow up of #30592 by @XuruDragon, with two main differences:
ScopingHttpClientmight be a better name for what is called aConditionalHttpClientthere,FrameworkBundlepart is removed so that it can be submitted separately later on.With a
ScopingHttpClient, you can add some default options conditionally based on the requested URL and a regexp that it should match. This allows building clients that add e.g. credentials based on the requested scheme/host/path.When the requested URL is a relative one, a default index can be provided - whose corresponding default options (the
base_urione especially) will be used to turn it into an absolute URL.Regexps are anchored on their left side.
E.g. this defines a client that will send some github token when a request is made to the corresponding API, and will not send those credentials if any other host is requested, while also turning relative URLs to github ones:
Of course, it's possible to define several regexps as keys so that one can create a client that is authenticated against several hosts/paths.