Skip to content

Commit

Permalink
feature #48797 [FrameworkBundle] Add extra attribute for HttpClient…
Browse files Browse the repository at this point in the history
… Configuration (voodooism)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[FrameworkBundle] Add `extra` attribute for HttpClient Configuration

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| License       | MIT
| Doc PR        | coming soon...<!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry sh
 - ould follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

I want to configure HttpClient once in configure file and forget about the configuration when doing a request or injection. In my particular case, I want to pass certificates to the client as raw values using special curlopts. Here #48775 proposed the best way to do it - using `extra` attribute in configuration.

It's going to look like so
```php
return static function (FrameworkConfig $frameworkConfig): void {
    $httpClient = $frameworkConfig->httpClient();
    $httpClient->defaultOptions([
        'extra' => ['curl' => ['foo' => 'bar']]
    ]);

    $httpClient->scopedClient('some_client')
        ->baseUri('https://some.uri')
        ->header('Accept', 'application/json')
        ->extra(['curl' => ['foo' => 'bar']]);
}
```

Commits
-------

6c89894 [FrameworkBundle] Add `extra` attribute for HttpClient Configuration
  • Loading branch information
fabpot committed Jan 5, 2023
2 parents 5c09985 + 6c89894 commit ed96673
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.3
---

* Add `extra` option for `http_client.default_options` and `http_client.scoped_client`
* Add `DomCrawlerAssertionsTrait::assertSelectorCount(int $count, string $selector)`
* Allow to avoid `limit` definition in a RateLimiter configuration when using the `no_limit` policy
* Add `--format` option to the `debug:config` command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,11 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e
->variableNode('md5')->end()
->end()
->end()
->arrayNode('extra')
->info('Extra options for specific HTTP client')
->normalizeKeys(false)
->variablePrototype()->end()
->end()
->append($this->addHttpClientRetrySection())
->end()
->end()
Expand Down Expand Up @@ -1868,6 +1873,11 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e
->variableNode('md5')->end()
->end()
->end()
->arrayNode('extra')
->info('Extra options for specific HTTP client')
->normalizeKeys(false)
->variablePrototype()->end()
->end()
->append($this->addHttpClientRetrySection())
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="max-redirects" type="xsd:integer" />
<xsd:attribute name="http-version" type="xsd:string" />
Expand All @@ -645,6 +646,7 @@
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="scope" type="xsd:string" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'pin-sha256' => ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412='],
'md5' => 'sdhtb481248721thbr=',
],
'extra' => ['foo' => ['bar' => 'baz']],
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
'max_host_connections' => 4,
'default_options' => [
'headers' => ['foo' => 'bar'],
'extra' => ['foo' => 'bar'],
],
'scoped_clients' => [
'foo' => [
'base_uri' => 'http://example.com',
'headers' => ['bar' => 'baz'],
'extra' => ['bar' => 'baz'],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<framework:pin-sha256>jsda84hjtyd4821bgfesd215bsfg5412=</framework:pin-sha256>
<framework:md5>sdhtb481248721thbr=</framework:md5>
</framework:peer-fingerprint>
<framework:extra>
<framework:foo>
<framework:bar>baz</framework:bar>
</framework:foo>
</framework:extra>
</framework:default-options>
</framework:http-client>
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
<framework:http-client max-host-connections="4">
<framework:default-options>
<framework:header name="foo">bar</framework:header>
<framework:extra>
<framework:foo>bar</framework:foo>
</framework:extra>
</framework:default-options>
<framework:scoped-client name="foo" base-uri="http://example.com">
<framework:header name="bar">baz</framework:header>
<framework:extra>
<framework:bar>baz</framework:bar>
</framework:extra>
</framework:scoped-client>
</framework:http-client>
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ framework:
peer_fingerprint:
pin-sha256: ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412=']
md5: 'sdhtb481248721thbr='
extra:
foo:
bar: 'baz'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ framework:
max_host_connections: 4
default_options:
headers: {'foo': 'bar'}
extra: {'foo': 'bar'}
scoped_clients:
foo:
base_uri: http://example.com
headers: {'bar': 'baz'}
extra: {'bar': 'baz'}
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,7 @@ public function testHttpClientDefaultOptions()
$defaultOptions = [
'headers' => [],
'resolve' => [],
'extra' => [],
];
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());

Expand All @@ -1872,17 +1873,21 @@ public function testHttpClientOverrideDefaultOptions()
$container = $this->createContainerFromFile('http_client_override_default_options');

$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['extra']);
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));

$expected = [
'headers' => [
'bar' => 'baz',
],
'extra' => [
'bar' => 'baz',
],
'query' => [],
'resolve' => [],
];
$this->assertSame($expected, $container->getDefinition('foo')->getArgument(2));
$this->assertEquals($expected, $container->getDefinition('foo')->getArgument(2));
}

public function testHttpClientRetry()
Expand Down Expand Up @@ -1944,6 +1949,7 @@ public function testHttpClientFullDefaultOptions()
'pin-sha256' => ['14s5erg62v1v8471g2revg48r7==', 'jsda84hjtyd4821bgfesd215bsfg5412='],
'md5' => 'sdhtb481248721thbr=',
], $defaultOptions['peer_fingerprint']);
$this->assertSame(['foo' => ['bar' => 'baz']], $defaultOptions['extra']);
}

public function provideMailer(): array
Expand Down

0 comments on commit ed96673

Please sign in to comment.