[HttpClient] Add a ScopingHttpClient #30654
Merged
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:
ScopingHttpClient
might be a better name for what is called aConditionalHttpClient
there,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:
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.