Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] change the way http clients are configured by leveraging ScopingHttpClient #30674

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 1 commit into from
Apr 3, 2019

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Mar 24, 2019

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 allows configuring scoped HTTP clients ("scoped_clients" replaces the previous "clients" options):

framework:
  http_client:
    max_host_connections: 4
    default_options:
      # ...
    scoped_clients:
      github_client:
        base_uri: https://api.github.com
        headers:
          Authorization: token abc123
          # ...

The base URI is turned into a scoping regular expression so that the token will be sent only when the github_client service is requesting the corresponding URLs.
When the base URI is too restrictive, the scope option can be used explicitly to define the regexp that URLs must match before any other options are applied.

All defined scopes are passed to a new scoping_http_client service, that can be used to hit endpoints with authentication pre-configured for several hosts. Its named autowiring alias is HttpClientInterface $scopingClient (this cannot be done with http_client as we want safe defaults, e.g. credentials should not be used implicitly when writing webhooks/crawlers.)

@joelwurtz
Copy link
Contributor

joelwurtz commented Mar 26, 2019

What do you think about using the same concept as the RequestMatcher (like a ClientRequestMatcher) for tiggering this ?

So someone could match on headers (like content-type / accept and even if the header is already present) or even body response ?

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Mar 26, 2019

So someone could match on headers (like content-type / accept and even if the header is already present) or even body response ?

I suppose you mean body request?
Anyway, why not, but I don't know how precisely, and I would wait for someone to have a use case first (and contribute the code ;) )

->cannotBeEmpty()
->end()
->scalarNode('base_uri')
->info('The URI to resolve relative URLs, following rules in RFC 3985, section 2.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the full purpose of this option. Well, it IS the full purpose if scope is not defined :p. But assuming it's not defined, this is both the base URI to use, but also for matching. Gotta find a way to "squeeze" that in. We could add an info on the parent scopes node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info updated

@nicolas-grekas nicolas-grekas force-pushed the fwb-hc-scope branch 5 times, most recently from 5bd1967 to ab34374 Compare March 29, 2019 19:22
@nicolas-grekas
Copy link
Member Author

Status: needs review
Green :)

@nicolas-grekas
Copy link
Member Author

(failure is unrelated: some old commit for contracts us checked out)
Votes pending

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from a feature standpoint - I chatted about this with Nicolas.

<argument type="service" id="http_client" />
<argument type="collection" />
</service>
<service id="Symfony\Contracts\HttpClient\HttpClientInterface $scopingHttpClient" alias="scoping_http_client" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, a scoped HTTP client is the result, but $scopingHttpClientdoes not sound good here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that $scopedHttpClient sounded like it was already "scoped". But really, this gives you a big object that is aware of all of the scopes, and has the ability to scope, based on the URI. The naming came from this thinking, but maybe it's still confusing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Ryan said. The same reasoning is why ScopingHttpClient, the class, is named that way also.

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not a big fan of the names, but I don't have a better alternative.

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 3, 2019

I removed the scoping_http_client service and related autowiring aliases. It can be reintroduced later if needed.

@nicolas-grekas nicolas-grekas merged commit f1a26b9 into symfony:master Apr 3, 2019
nicolas-grekas added a commit that referenced this pull request Apr 3, 2019
…igured by leveraging ScopingHttpClient (nicolas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[FrameworkBundle] change the way http clients are configured by leveraging 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 allows configuring scoped HTTP clients ("scoped_clients" replaces the previous "clients" options):

```yaml
framework:
  http_client:
    max_host_connections: 4
    default_options:
      # ...
    scoped_clients:
      github_client:
        base_uri: https://api.github.com
        headers:
          Authorization: token abc123
          # ...
```

The base URI is turned into a scoping regular expression so that the token will be sent only when the `github_client` service is requesting the corresponding URLs.
When the base URI is too restrictive, the `scope` option can be used explicitly to define the regexp that URLs must match before any other options are applied.

~All defined scopes are passed to a new `scoping_http_client` service, that can be used to hit endpoints with authentication pre-configured for several hosts. Its named autowiring alias is `HttpClientInterface $scopingClient` (this cannot be done with `http_client` as we want safe defaults, e.g. credentials should not be used implicitly when writing webhooks/crawlers.)~

Commits
-------

f1a26b9 [FrameworkBundle] change the way http clients are configured by leveraging ScopingHttpClient
@nicolas-grekas nicolas-grekas deleted the fwb-hc-scope branch April 3, 2019 09:37
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
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.

5 participants