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

Skip to content

Commit 7d01aae

Browse files
committed
feature #30654 [HttpClient] Add a ScopingHttpClient (XuruDragon)
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
2 parents 22bd250 + 1ee0a11 commit 7d01aae

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient;
13+
14+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
16+
use Symfony\Contracts\HttpClient\ResponseInterface;
17+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
18+
19+
/**
20+
* Auto-configure the default options based on the requested URL.
21+
*
22+
* @author Anthony Martin <[email protected]>
23+
*
24+
* @experimental in 4.3
25+
*/
26+
class ScopingHttpClient implements HttpClientInterface
27+
{
28+
use HttpClientTrait;
29+
30+
private $client;
31+
private $defaultOptionsByRegexp;
32+
private $defaultRegexp;
33+
34+
public function __construct(HttpClientInterface $client, array $defaultOptionsByRegexp, string $defaultRegexp = null)
35+
{
36+
$this->client = $client;
37+
$this->defaultOptionsByRegexp = $defaultOptionsByRegexp;
38+
$this->defaultRegexp = $defaultRegexp;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function request(string $method, string $url, array $options = []): ResponseInterface
45+
{
46+
$url = self::parseUrl($url, $options['query'] ?? []);
47+
48+
if (\is_string($options['base_uri'] ?? null)) {
49+
$options['base_uri'] = self::parseUrl($options['base_uri']);
50+
}
51+
52+
try {
53+
$url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null));
54+
} catch (InvalidArgumentException $e) {
55+
if (null === $this->defaultRegexp) {
56+
throw $e;
57+
}
58+
59+
[$url, $options] = self::prepareRequest($method, implode('', $url), $options, $this->defaultOptionsByRegexp[$this->defaultRegexp], true);
60+
$url = implode('', $url);
61+
}
62+
63+
foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
64+
if (preg_match("{{$regexp}}A", $url)) {
65+
$options = self::mergeDefaultOptions($options, $defaultOptions, true);
66+
break;
67+
}
68+
}
69+
70+
return $this->client->request($method, $url, $options);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function stream($responses, float $timeout = null): ResponseStreamInterface
77+
{
78+
return $this->client->stream($responses, $timeout);
79+
}
80+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
use Symfony\Component\HttpClient\ScopingHttpClient;
19+
20+
class ScopingHttpClientTest extends TestCase
21+
{
22+
public function testRelativeUrl()
23+
{
24+
$mockClient = new MockHttpClient([]);
25+
$client = new ScopingHttpClient($mockClient, []);
26+
27+
$this->expectException(InvalidArgumentException::class);
28+
$client->request('GET', '/foo');
29+
}
30+
31+
public function testRelativeUrlWithDefaultRegexp()
32+
{
33+
$mockClient = new MockHttpClient(new MockResponse());
34+
$client = new ScopingHttpClient($mockClient, ['.*' => ['base_uri' => 'http://example.com']], '.*');
35+
36+
$this->assertSame('http://example.com/foo', $client->request('GET', '/foo')->getInfo('url'));
37+
}
38+
39+
/**
40+
* @dataProvider provideMatchingUrls
41+
*/
42+
public function testMatchingUrls(string $regexp, string $url, array $options)
43+
{
44+
$mockClient = new MockHttpClient(new MockResponse());
45+
$client = new ScopingHttpClient($mockClient, $options);
46+
47+
$response = $client->request('GET', $url);
48+
$reuestedOptions = $response->getRequestOptions();
49+
50+
$this->assertEquals($reuestedOptions['case'], $options[$regexp]['case']);
51+
}
52+
53+
public function provideMatchingUrls()
54+
{
55+
$defaultOptions = [
56+
'.*/foo-bar' => ['case' => 1],
57+
'.*' => ['case' => 2],
58+
];
59+
60+
yield ['regexp' => '.*/foo-bar', 'url' => 'http://example.com/foo-bar', 'default_options' => $defaultOptions];
61+
yield ['regexp' => '.*', 'url' => 'http://example.com/bar-foo', 'default_options' => $defaultOptions];
62+
yield ['regexp' => '.*', 'url' => 'http://example.com/foobar', 'default_options' => $defaultOptions];
63+
}
64+
65+
public function testMatchingUrlsAndOptions()
66+
{
67+
$defaultOptions = [
68+
'.*/foo-bar' => ['headers' => ['x-app' => 'unit-test-foo-bar']],
69+
'.*' => ['headers' => ['content-type' => 'text/html']],
70+
];
71+
72+
$mockResponses = [
73+
new MockResponse(),
74+
new MockResponse(),
75+
new MockResponse(),
76+
];
77+
78+
$mockClient = new MockHttpClient($mockResponses);
79+
$client = new ScopingHttpClient($mockClient, $defaultOptions);
80+
81+
$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
82+
$requestOptions = $response->getRequestOptions();
83+
$this->assertEquals($requestOptions['json']['url'], 'http://example.com');
84+
$this->assertEquals($requestOptions['headers']['x-app'][0], $defaultOptions['.*/foo-bar']['headers']['x-app']);
85+
86+
$response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]);
87+
$requestOptions = $response->getRequestOptions();
88+
$this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test');
89+
$this->assertEquals($requestOptions['headers']['content-type'][0], 'text/html');
90+
91+
$response = $client->request('GET', 'http://example.com/foobar-foo', ['headers' => ['x-app' => 'unit-test']]);
92+
$requestOptions = $response->getRequestOptions();
93+
$this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test');
94+
$this->assertEquals($requestOptions['headers']['content-type'][0], 'text/html');
95+
}
96+
}

0 commit comments

Comments
 (0)