|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nighten\ApiClient; |
|
4
|
|
|
|
|
5
|
|
|
use Nighten\ApiClient\Request\RequestInterface; |
|
6
|
|
|
use Nighten\ApiClient\Response\DefaultResponseHandler; |
|
7
|
|
|
use Nighten\ApiClient\Response\ResponseHandlerInterface; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Client |
|
12
|
|
|
{ |
|
13
|
|
|
private string $host; |
|
14
|
|
|
|
|
15
|
|
|
private ?float $timeout = null; |
|
16
|
|
|
|
|
17
|
|
|
private ClientInterface $httpClient; |
|
18
|
|
|
|
|
19
|
|
|
private ?ResponseHandlerInterface $responseHandler; |
|
20
|
|
|
|
|
21
|
|
|
private ?LoggerInterface $logger; |
|
22
|
|
|
|
|
23
|
|
|
private ?AuthenticationProviderInterface $authenticationProvider = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Client constructor. |
|
27
|
|
|
* @param string $host |
|
28
|
|
|
* @param ClientInterface|null $httpClient |
|
29
|
|
|
* @param ResponseHandlerInterface|null $responseHandler |
|
30
|
|
|
* @param LoggerInterface|null $logger |
|
31
|
|
|
*/ |
|
32
|
10 |
|
public function __construct( |
|
33
|
|
|
string $host, |
|
34
|
|
|
?ClientInterface $httpClient = null, |
|
35
|
|
|
?ResponseHandlerInterface $responseHandler = null, |
|
36
|
|
|
?LoggerInterface $logger = null |
|
37
|
|
|
) { |
|
38
|
10 |
|
$this->host = preg_replace('#/$#', '', $host); |
|
39
|
10 |
|
$this->httpClient = $httpClient ?: new \GuzzleHttp\Client(); |
|
40
|
10 |
|
$this->responseHandler = $responseHandler; |
|
41
|
10 |
|
$this->logger = $logger; |
|
42
|
10 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param LoggerInterface $logger |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function setLogger(LoggerInterface $logger): void |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->logger = $logger; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function setAuthenticationProvider(AuthenticationProviderInterface $authenticationProvider): void |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->authenticationProvider = $authenticationProvider; |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param float $timeout |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function setTimeout(float $timeout): void |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->timeout = $timeout; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ResponseHandlerInterface $responseHandler |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function setResponseHandler(ResponseHandlerInterface $responseHandler): void |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->responseHandler = $responseHandler; |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param RequestInterface $request |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function request(RequestInterface $request) |
|
79
|
|
|
{ |
|
80
|
9 |
|
$response = $this->httpClient->request( |
|
81
|
9 |
|
$request->getMethod(), |
|
82
|
9 |
|
$this->host . $request->getPath(), |
|
83
|
9 |
|
$this->getOptions($request->getBody()) |
|
84
|
|
|
); |
|
85
|
9 |
|
if ($this->logger instanceof LoggerInterface) { |
|
86
|
1 |
|
$this->logger->info('Try API Request. Method:' . $request->getMethod() . ' Url:' . $this->host . $request->getPath()); |
|
87
|
|
|
} |
|
88
|
9 |
|
return $this->getActualResponseHandler($request)->handle($response); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $rawBody |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
9 |
|
private function getOptions(?string $rawBody = null): array |
|
96
|
|
|
{ |
|
97
|
9 |
|
$options = []; |
|
98
|
9 |
|
if ($rawBody !== null) { |
|
99
|
9 |
|
$options['body'] = $rawBody; |
|
100
|
|
|
} |
|
101
|
9 |
|
if ($this->timeout !== null) { |
|
102
|
1 |
|
$options['timeout'] = $this->timeout; |
|
103
|
|
|
} |
|
104
|
9 |
|
if ($this->authenticationProvider instanceof AuthenticationProviderInterface) { |
|
105
|
1 |
|
$options['headers']['Authorization'] = $this->authenticationProvider->getKey(); |
|
106
|
|
|
} |
|
107
|
9 |
|
return $options; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param RequestInterface $request |
|
112
|
|
|
* @return ResponseHandlerInterface |
|
113
|
|
|
*/ |
|
114
|
9 |
|
private function getActualResponseHandler(RequestInterface $request): ResponseHandlerInterface |
|
115
|
|
|
{ |
|
116
|
9 |
|
if ($this->responseHandler instanceof ResponseHandlerInterface) { |
|
117
|
1 |
|
return $this->responseHandler; |
|
118
|
|
|
} |
|
119
|
8 |
|
if ($request instanceof ResponseHandlerAwareInterface) { |
|
120
|
1 |
|
return $request->getResponseHandler(); |
|
121
|
|
|
} |
|
122
|
7 |
|
return new DefaultResponseHandler(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return ClientInterface |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function getHttpClient(): ClientInterface |
|
129
|
|
|
{ |
|
130
|
1 |
|
return $this->httpClient; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param ClientInterface $httpClient |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function setHttpClient(ClientInterface $httpClient): void |
|
137
|
|
|
{ |
|
138
|
1 |
|
$this->httpClient = $httpClient; |
|
139
|
1 |
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|