-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Add UriTemplateHttpClient
#49302
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
nicolas-grekas
merged 1 commit into
symfony:6.3
from
fancyweb:http-client/uri-template-client
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/Symfony/Component/HttpClient/Tests/UriTemplateHttpClientTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Component\HttpClient\UriTemplateHttpClient; | ||
|
||
final class UriTemplateHttpClientTest extends TestCase | ||
{ | ||
public function testExpanderIsCalled() | ||
{ | ||
$client = new UriTemplateHttpClient( | ||
new MockHttpClient(), | ||
function (string $url, array $vars): string { | ||
$this->assertSame('https://foo.tld/{version}/{resource}{?page}', $url); | ||
$this->assertSame([ | ||
'version' => 'v2', | ||
'resource' => 'users', | ||
'page' => 33, | ||
], $vars); | ||
|
||
return 'https://foo.tld/v2/users?page=33'; | ||
}, | ||
[ | ||
'version' => 'v2', | ||
], | ||
); | ||
$this->assertSame('https://foo.tld/v2/users?page=33', $client->request('GET', 'https://foo.tld/{version}/{resource}{?page}', [ | ||
'vars' => [ | ||
'resource' => 'users', | ||
'page' => 33, | ||
], | ||
])->getInfo('url')); | ||
} | ||
|
||
public function testWithOptionsAppendsVarsToDefaultVars() | ||
{ | ||
$client = new UriTemplateHttpClient( | ||
new MockHttpClient(), | ||
function (string $url, array $vars): string { | ||
$this->assertSame('https://foo.tld/{bar}', $url); | ||
$this->assertSame([ | ||
'bar' => 'ccc', | ||
], $vars); | ||
|
||
return 'https://foo.tld/ccc'; | ||
}, | ||
); | ||
$this->assertSame('https://foo.tld/{bar}', $client->request('GET', 'https://foo.tld/{bar}')->getInfo('url')); | ||
|
||
$client = $client->withOptions([ | ||
'vars' => [ | ||
'bar' => 'ccc', | ||
], | ||
]); | ||
$this->assertSame('https://foo.tld/ccc', $client->request('GET', 'https://foo.tld/{bar}')->getInfo('url')); | ||
} | ||
|
||
public function testExpanderIsNotCalledWithEmptyVars() | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
$client = new UriTemplateHttpClient(new MockHttpClient(), $this->fail(...)); | ||
$client->request('GET', 'https://foo.tld/bar', [ | ||
'vars' => [], | ||
]); | ||
} | ||
|
||
public function testExpanderIsNotCalledWithNoVarsAtAll() | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
$client = new UriTemplateHttpClient(new MockHttpClient(), $this->fail(...)); | ||
$client->request('GET', 'https://foo.tld/bar'); | ||
} | ||
|
||
public function testRequestWithNonArrayVarsOption() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "vars" option must be an array.'); | ||
|
||
(new UriTemplateHttpClient(new MockHttpClient()))->request('GET', 'https://foo.tld', [ | ||
'vars' => 'should be an array', | ||
]); | ||
} | ||
|
||
public function testWithOptionsWithNonArrayVarsOption() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "vars" option must be an array.'); | ||
|
||
(new UriTemplateHttpClient(new MockHttpClient()))->withOptions([ | ||
'vars' => new \stdClass(), | ||
]); | ||
} | ||
|
||
public function testVarsOptionIsNotPropagated() | ||
{ | ||
$client = new UriTemplateHttpClient( | ||
new MockHttpClient(function (string $method, string $url, array $options): MockResponse { | ||
$this->assertArrayNotHasKey('vars', $options); | ||
|
||
return new MockResponse(); | ||
}), | ||
static fn (): string => 'ccc', | ||
); | ||
|
||
$client->withOptions([ | ||
'vars' => [ | ||
'foo' => 'bar', | ||
], | ||
])->request('GET', 'https://foo.tld', [ | ||
'vars' => [ | ||
'foo2' => 'bar2', | ||
], | ||
]); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/HttpClient/UriTemplateHttpClient.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient; | ||
|
||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
class UriTemplateHttpClient implements HttpClientInterface, ResetInterface | ||
{ | ||
use DecoratorTrait; | ||
|
||
/** | ||
* @param (\Closure(string $url, array $vars): string)|null $expander | ||
*/ | ||
public function __construct(HttpClientInterface $client = null, private ?\Closure $expander = null, private array $defaultVars = []) | ||
{ | ||
$this->client = $client ?? HttpClient::create(); | ||
} | ||
|
||
public function request(string $method, string $url, array $options = []): ResponseInterface | ||
{ | ||
$vars = $this->defaultVars; | ||
|
||
if (\array_key_exists('vars', $options)) { | ||
if (!\is_array($options['vars'])) { | ||
throw new \InvalidArgumentException('The "vars" option must be an array.'); | ||
} | ||
|
||
$vars = [...$vars, ...$options['vars']]; | ||
unset($options['vars']); | ||
} | ||
|
||
if ($vars) { | ||
$url = ($this->expander ??= $this->createExpanderFromPopularVendors())($url, $vars); | ||
} | ||
|
||
return $this->client->request($method, $url, $options); | ||
} | ||
|
||
public function withOptions(array $options): static | ||
{ | ||
if (!\is_array($options['vars'] ?? [])) { | ||
throw new \InvalidArgumentException('The "vars" option must be an array.'); | ||
} | ||
|
||
$clone = clone $this; | ||
$clone->defaultVars = [...$clone->defaultVars, ...$options['vars'] ?? []]; | ||
unset($options['vars']); | ||
|
||
$clone->client = $this->client->withOptions($options); | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @return \Closure(string $url, array $vars): string | ||
*/ | ||
private function createExpanderFromPopularVendors(): \Closure | ||
{ | ||
if (class_exists(\GuzzleHttp\UriTemplate\UriTemplate::class)) { | ||
return \GuzzleHttp\UriTemplate\UriTemplate::expand(...); | ||
} | ||
|
||
if (class_exists(\League\Uri\UriTemplate::class)) { | ||
return static fn (string $url, array $vars): string => (new \League\Uri\UriTemplate($url))->expand($vars); | ||
} | ||
|
||
if (class_exists(\Rize\UriTemplate::class)) { | ||
return (new \Rize\UriTemplate())->expand(...); | ||
} | ||
|
||
throw new \LogicException('Support for URI template requires a vendor to expand the URI. Run "composer require guzzlehttp/uri-template" or pass your own expander \Closure implementation.'); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.