8000 [HttpClient] Add a ConditionalHttpClient · symfony/symfony@eaf6bb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaf6bb7

Browse files
author
Anthony MARTIN
committed
[HttpClient] Add a ConditionalHttpClient
1 parent ac93c9e commit eaf6bb7

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Contracts\HttpClient\HttpClientInterface;
< 10000 /code>
15+
use Symfony\Contracts\HttpClient\ResponseInterface;
16+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
17+
18+
/**
19+
* @author Anthony Martin <anthony.martin@sensiolabs.com>
20+
*
21+
* @experimental in 4.3
22+
*/
23+
class ConditionalHttpClient implements HttpClientInterface
24+
{
25+
private $options;
26+
27+
private $client;
28+
29+
/**
30+
* @param HttpClientInterface $client
31+
* @param array $options should contain an array with regexp as key to filter the url and an array of options as value to be given for the HttpClient request() function
32+
*/
33+
public function __construct(HttpClientInterface $client, array $options)
34+
{
35+
$this->client = $client;
36+
$this->options = $options;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function request(string $method, string $url, array $options = []): ResponseInterface
43+
{
44+
foreach ($this->options as $regexp => $clientOptions) {
45+
if (preg_match($regexp, $url)) {
46+
return $this->client->request($method, $url, array_merge($clientOptions, $options));
47+
}
48+
}
49+
50+
return $this->client->request($method, $url, $options);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function stream($responses, float $timeout = null): ResponseStreamInterface
57+
{
58+
return $this->client->stream($responses, $timeout);
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\HttpClient\ConditionalHttpClient;
15+
use Symfony\Component\HttpClient\CurlHttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase;
18+
19+
/**
20+
* @requires extension curl
21+
*/
22+
class CurlConditionalHttpClientTest extends HttpClientTestCase
23+
{
24+
protected function getHttpClient(): HttpClientInterface
25+
{
26+
return new ConditionalHttpClient(new CurlHttpClient(), [
27+
'#^.*length-broken$#' => ['headers' => [
28+
'ConditionalHttpClient' => 'CurlHttpClient',
29+
'ConditionalHttpClient2' => 'url:length-broken',
30+
]],
31+
'#^.*$#' => ['headers' => ['ConditionalHttpClient' => 'CurlHttpClient']],
32+
]);
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\HttpClient\ConditionalHttpClient;
15+
use Symfony\Component\HttpClient\NativeHttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase;
18+
19+
abstract class NativeConditionalHttpClientTest extends HttpClientTestCase
20+
{
21+
protected function getHttpClient(): HttpClientInterface
22+
{
23+
return new ConditionalHttpClient(new NativeHttpClient(), [
24+
'#^.*length-broken$#' => ['headers' => [
25+
'ConditionalHttpClient' => 'NativeHttpClient',
26+
'ConditionalHttpClient2' => 'url:length-broken',
27+
]],
28+
'#^.*$#' => ['headers' => ['ConditionalHttpClient' => 'NativeHttpClient']],
29+
]);
30+
}
31+
}

0 commit comments

Comments
 (0)
0