|
10 | 10 |
|
11 | 11 | namespace chillerlan\OAuthTest; |
12 | 12 |
|
13 | | -use chillerlan\HTTP\Psr18\{CurlClient, LoggingClient}; |
14 | | -use chillerlan\Settings\SettingsContainerInterface; |
| 13 | +use Psr\Http\Client\ClientExceptionInterface; |
15 | 14 | use Psr\Http\Client\ClientInterface; |
16 | 15 | use Psr\Http\Message\{RequestInterface, ResponseInterface}; |
17 | | -use Psr\Log\{LoggerAwareInterface, LoggerInterface, NullLogger}; |
| 16 | +use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
| 17 | +use Exception, Throwable; |
18 | 18 |
|
19 | | -use function usleep; |
| 19 | +use function chillerlan\HTTP\Utils\message_to_string; |
| 20 | +use function constant, defined, get_class, usleep; |
20 | 21 |
|
21 | 22 | class OAuthTestHttpClient implements ClientInterface, LoggerAwareInterface{ |
| 23 | + use LoggerAwareTrait; |
22 | 24 |
|
23 | | - protected SettingsContainerInterface $options; |
24 | | - |
25 | | - protected ClientInterface $client; |
| 25 | + protected ClientInterface $http; |
26 | 26 |
|
27 | 27 | public function __construct( |
28 | | - SettingsContainerInterface $options, |
29 | | - ClientInterface $http = null, |
| 28 | + string $cfgdir, |
30 | 29 | LoggerInterface $logger = null |
31 | 30 | ){ |
32 | | - $this->options = $options; |
33 | | - $this->client = new LoggingClient( |
34 | | - $http ?? new CurlClient($this->options), |
35 | | - $logger ?? new NullLogger |
36 | | - ); |
37 | | - } |
38 | 31 |
|
39 | | - /** |
40 | | - * @inheritDoc |
41 | | - */ |
42 | | - public function setLogger(LoggerInterface $logger):void{ |
43 | | - $this->client->setLogger($logger); |
| 32 | + if(!defined('TEST_CLIENT_FACTORY')){ |
| 33 | + throw new Exception('TEST_CLIENT_FACTORY in phpunit.xml not set'); |
| 34 | + } |
| 35 | + |
| 36 | + $clientFactory = constant('TEST_CLIENT_FACTORY'); |
| 37 | + |
| 38 | + $this->http = $clientFactory::getClient($cfgdir); |
| 39 | + $this->logger = $logger ?? new NullLogger; |
44 | 40 | } |
45 | 41 |
|
46 | 42 | /** |
47 | 43 | * @inheritDoc |
48 | 44 | */ |
49 | 45 | public function sendRequest(RequestInterface $request):ResponseInterface{ |
50 | | - usleep($this->options->sleep * 1000000); |
| 46 | + $this->logger->debug("\n----HTTP-REQUEST----\n".message_to_string($request)); |
| 47 | + usleep(250000); |
| 48 | + |
| 49 | + try{ |
| 50 | + $response = $this->http->sendRequest($request); |
| 51 | + } |
| 52 | + catch(Throwable $e){ |
| 53 | + $this->logger->debug("\n----HTTP-ERROR------\n".message_to_string($request)); |
| 54 | + $this->logger->error($e->getMessage()); |
| 55 | + $this->logger->error($e->getTraceAsString()); |
| 56 | + |
| 57 | + if(!$e instanceof ClientExceptionInterface){ |
| 58 | + throw new Exception('unexpected exception, does not implement "ClientExceptionInterface": '.get_class($e)); |
| 59 | + } |
| 60 | + |
| 61 | + throw $e; |
| 62 | + } |
| 63 | + |
| 64 | + $this->logger->debug("\n----HTTP-RESPONSE---\n".message_to_string($response)); |
51 | 65 |
|
52 | | - return $this->client->sendRequest($request); |
| 66 | + return $response; |
53 | 67 | } |
54 | 68 |
|
55 | 69 | } |
0 commit comments