This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from loveOSS/introduce-sf-http-client
Implemented Symfony 4 HTTP client adapter
- Loading branch information
Showing
14 changed files
with
199 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -10,8 +10,6 @@ php: | |
|
||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
- php: 7.3 | ||
|
||
cache: | ||
directories: | ||
|
This file contains 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 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 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,43 @@ | ||
<?php | ||
|
||
namespace Resiliency\Clients; | ||
|
||
use Resiliency\Contracts\Client; | ||
use Resiliency\Contracts\Place; | ||
use Resiliency\Contracts\Service; | ||
|
||
abstract class ClientHelper implements Client | ||
{ | ||
/** | ||
* @var array the Client main options | ||
*/ | ||
protected $mainOptions; | ||
|
||
public function __construct(array $mainOptions = []) | ||
{ | ||
$this->mainOptions = $mainOptions; | ||
} | ||
|
||
/** | ||
* @param array $options the list of options | ||
* | ||
* @return string the method | ||
*/ | ||
protected function defineMethod(array $options): string | ||
{ | ||
if (isset($this->mainOptions['method'])) { | ||
return (string) $this->mainOptions['method']; | ||
} | ||
|
||
if (isset($options['method'])) { | ||
return (string) $options['method']; | ||
} | ||
|
||
return self::DEFAULT_METHOD; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
abstract public function request(Service $service, Place $place): string; | ||
} |
This file contains 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 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,53 @@ | ||
<?php | ||
|
||
namespace Resiliency\Clients; | ||
|
||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Resiliency\Exceptions\UnavailableService; | ||
use Resiliency\Contracts\Service; | ||
use Resiliency\Contracts\Place; | ||
|
||
/** | ||
* Symfony implementation of client. | ||
* The possibility of extending this client is intended. | ||
* /!\ The HttpClient of Symfony is experimental. | ||
*/ | ||
class SymfonyClient extends ClientHelper | ||
{ | ||
/** | ||
* @var HttpClientInterface the Symfony HTTP client | ||
*/ | ||
private $httpClient; | ||
|
||
public function __construct( | ||
HttpClientInterface $httpClient, | ||
array $mainOptions = [] | ||
) { | ||
$this->httpClient = $httpClient; | ||
parent::__construct($mainOptions); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function request(Service $service, Place $place): string | ||
{ | ||
$options = []; | ||
try { | ||
$method = $this->defineMethod($service->getParameters()); | ||
$options['timeout'] = $place->getTimeout(); | ||
|
||
$clientParameters = array_merge($service->getParameters(), $options); | ||
unset($clientParameters['method']); | ||
|
||
return $this->httpClient->request($method, $service->getURI(), $clientParameters)->getContent(); | ||
} catch (TransportExceptionInterface $exception) { | ||
throw new UnavailableService( | ||
$exception->getMessage(), | ||
(int) $exception->getCode(), | ||
$exception | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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,74 @@ | ||
<?php | ||
|
||
namespace Tests\Resiliency\Clients; | ||
|
||
use Resiliency\Contracts\Place; | ||
use Resiliency\Clients\SymfonyClient; | ||
use Tests\Resiliency\CircuitBreakerTestCase; | ||
use Resiliency\Exceptions\UnavailableService; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
|
||
class SymfonyClientTest extends CircuitBreakerTestCase | ||
{ | ||
public function testRequestWorksAsExpected(): void | ||
{ | ||
$client = new SymfonyClient($this->getClient()); | ||
$service = $this->getService('https://www.google.com', ['method' => 'GET']); | ||
|
||
self::assertNotNull($client->request($service, $this->getPlace())); | ||
} | ||
|
||
public function testWrongRequestThrowsAnException(): void | ||
{ | ||
$this->expectException(UnavailableService::class); | ||
|
||
$client = new SymfonyClient($this->getClient()); | ||
$service = $this->getService('http://not-even-a-valid-domain.xxx'); | ||
|
||
$client->request($service, $this->getPlace()); | ||
} | ||
|
||
public function testTheClientAcceptsHttpMethodOverride(): void | ||
{ | ||
$client = new SymfonyClient($this->getClient(), [ | ||
'method' => 'HEAD', | ||
]); | ||
|
||
$service = $this->getService('https://www.google.com'); | ||
|
||
self::assertSame( | ||
'', | ||
$client->request( | ||
$service, | ||
$this->getPlace() | ||
) | ||
); | ||
} | ||
|
||
private function getClient(): HttpClientInterface | ||
{ | ||
$callback = function ($method, $url, $options) { | ||
if ($url === 'http://not-even-a-valid-domain.xxx/') { | ||
return new MockResponse('', ['error' => 'Unavailable']); | ||
} | ||
|
||
if ($method === 'HEAD') { | ||
return new MockResponse(''); | ||
} | ||
|
||
return new MockResponse('mocked'); | ||
}; | ||
|
||
return new MockHttpClient($callback); | ||
} | ||
|
||
private function getPlace(): Place | ||
{ | ||
$placeMock = $this->createMock(Place::class); | ||
$placeMock->method('getTimeout')->willReturn(2.0); | ||
|
||
return $placeMock; | ||
} | ||
} |
This file contains 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