1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dolibarr\Client\HttpClient; |
4
|
|
|
|
5
|
|
|
use Dolibarr\Client\Exception\ApiException; |
6
|
|
|
use Dolibarr\Client\Exception\BadRequestException; |
7
|
|
|
use Dolibarr\Client\Exception\DolibarrException; |
8
|
|
|
use Dolibarr\Client\Exception\ResourceNotFoundException; |
9
|
|
|
use Dolibarr\Client\Security\Authentication\DolibarrApiKeyRequester; |
10
|
|
|
use Exception; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\ClientInterface; |
13
|
|
|
use GuzzleHttp\Exception\ClientException; |
14
|
|
|
use GuzzleHttp\Exception\ConnectException; |
15
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
16
|
|
|
use GuzzleHttp\Exception\ServerException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Laurent De Coninck <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class HttpClient implements HttpClientInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ClientInterface |
26
|
|
|
*/ |
27
|
|
|
private $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $options |
31
|
|
|
* @param ClientInterface $client |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
array $options = [], |
35
|
|
|
ClientInterface $client = null |
36
|
|
|
) { |
37
|
|
|
$this->client = $client ?: new Client($options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
* |
43
|
|
|
* @throws Exception |
44
|
|
|
*/ |
45
|
|
|
public function get($uri, array $options = []) |
46
|
|
|
{ |
47
|
|
|
return $this->request('GET', $uri, null, $options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* |
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
public function post($uri, $json, array $options = []) |
56
|
|
|
{ |
57
|
|
|
return $this->request('POST', $uri, $json, $options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function put($uri, $json, array $options = []) |
66
|
|
|
{ |
67
|
|
|
return $this->request('PUT', $uri, $json, $options); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
public function patch($uri, $json, array $options = []) |
76
|
|
|
{ |
77
|
|
|
return $this->request('PATCH', $uri, $json, $options); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
* |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
|
|
public function delete($uri, array $options = []) |
86
|
|
|
{ |
87
|
|
|
return $this->request('DELETE', $uri, null, $options); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @throws Exception |
94
|
|
|
*/ |
95
|
|
|
public function request($method, $uri, $json = null, array $options = []) |
96
|
|
|
{ |
97
|
|
|
if (!empty($json)) { |
98
|
|
|
$options['body'] = $json; |
99
|
|
|
$options['headers']['content-type'] = 'application/json'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$authenticationOption = $this->authenticate(); |
103
|
|
|
$options = array_merge($options, $authenticationOption); |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
return $this->client->request($method, $uri, $options); |
|
|
|
|
107
|
|
|
} catch (GuzzleException $e) { |
108
|
|
|
$this->propagateResponseExceptions($e); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param GuzzleException $exception |
114
|
|
|
* |
115
|
|
|
* @throws Exception |
116
|
|
|
*/ |
117
|
|
|
private function propagateResponseExceptions(GuzzleException $exception) |
118
|
|
|
{ |
119
|
|
|
if ($exception instanceof ConnectException) { |
120
|
|
|
throw new \RuntimeException("Connection issue!"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($exception instanceof ClientException) { |
124
|
|
|
$response = $exception->getResponse(); |
125
|
|
|
|
126
|
|
|
if (null === $response) { |
127
|
|
|
throw new ApiException($exception->getMessage()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($response->getStatusCode() === 404) { |
131
|
|
|
throw new ResourceNotFoundException(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($response->getStatusCode() === 400) { |
135
|
|
|
throw new BadRequestException(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($exception instanceof ServerException) { |
140
|
|
|
$response = $exception->getResponse(); |
141
|
|
|
|
142
|
|
|
if (null === $response) { |
143
|
|
|
throw new ApiException($exception->getMessage()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($response->getStatusCode() === 500) { |
147
|
|
|
throw new DolibarrException(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
throw new ApiException($exception->getMessage()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Do an authenticated request. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function authenticate() |
160
|
|
|
{ |
161
|
|
|
return []; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: