This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthTestHttpClient.php
68 lines (52 loc) · 1.76 KB
/
OAuthTestHttpClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Class OAuthTestHttpClient
*
* @created 26.07.2019
* @author smiley <smiley@chillerlan.net>
* @copyright 2019 smiley
* @license MIT
*/
namespace chillerlan\OAuthTest\Providers;
use Psr\Http\Client\{ClientExceptionInterface, ClientInterface};
use Psr\Http\Message\{RequestInterface, ResponseInterface};
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
use Exception, Throwable;
use function chillerlan\HTTP\Utils\message_to_string;
use function constant, defined, get_class, usleep;
final class OAuthTestHttpClient implements ClientInterface, LoggerAwareInterface{
use LoggerAwareTrait;
protected ClientInterface $http;
public function __construct(
string $cfgdir,
LoggerInterface $logger = null
){
if(!defined('TEST_CLIENT_FACTORY')){
throw new Exception('TEST_CLIENT_FACTORY in phpunit.xml not set');
}
$clientFactory = constant('TEST_CLIENT_FACTORY');
$this->http = $clientFactory::getClient($cfgdir);
$this->logger = $logger ?? new NullLogger;
}
/**
* @inheritDoc
*/
public function sendRequest(RequestInterface $request):ResponseInterface{
$this->logger->debug("\n----HTTP-REQUEST----\n".message_to_string($request));
usleep(250000);
try{
$response = $this->http->sendRequest($request);
}
catch(Throwable $e){
$this->logger->debug("\n----HTTP-ERROR------\n".message_to_string($request));
$this->logger->error($e->getMessage());
$this->logger->error($e->getTraceAsString());
if(!$e instanceof ClientExceptionInterface){
throw new Exception('unexpected exception, does not implement "ClientExceptionInterface": '.get_class($e));
}
throw $e;
}
$this->logger->debug("\n----HTTP-RESPONSE---\n".message_to_string($response));
return $response;
}
}