|
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
|
|
|
print $json; |
|
58
|
|
|
|
|
59
|
|
|
return $this->request('POST', $uri, $json, $options); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @throws Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
public function put($uri, $json, array $options = []) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->request('PUT', $uri, $json, $options); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @throws Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
public function patch($uri, $json, array $options = []) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->request('PATCH', $uri, $json, $options); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
* |
|
85
|
|
|
* @throws Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public function delete($uri, array $options = []) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->request('DELETE', $uri, null, $options); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
* |
|
95
|
|
|
* @throws Exception |
|
96
|
|
|
*/ |
|
97
|
|
|
public function request($method, $uri, $json = null, array $options = []) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!empty($json)) { |
|
100
|
|
|
$options['body'] = $json; |
|
101
|
|
|
$options['headers']['content-type'] = 'application/json'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$authenticationOption = $this->authenticate(); |
|
105
|
|
|
$options = array_merge($options, $authenticationOption); |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
return $this->client->request($method, $uri, $options); |
|
|
|
|
|
|
109
|
|
|
} catch (GuzzleException $e) { |
|
110
|
|
|
$this->propagateResponseExceptions($e); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param GuzzleException $exception |
|
116
|
|
|
* |
|
117
|
|
|
* @throws Exception |
|
118
|
|
|
*/ |
|
119
|
|
|
private function propagateResponseExceptions(GuzzleException $exception) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($exception instanceof ConnectException) { |
|
122
|
|
|
throw new \RuntimeException("Connection issue!"); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($exception instanceof ClientException) { |
|
126
|
|
|
$response = $exception->getResponse(); |
|
127
|
|
|
|
|
128
|
|
|
if (null === $response) { |
|
129
|
|
|
throw new ApiException($exception->getMessage()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($response->getStatusCode() === 404) { |
|
133
|
|
|
throw new ResourceNotFoundException(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($response->getStatusCode() === 400) { |
|
137
|
|
|
throw new BadRequestException(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($exception instanceof ServerException) { |
|
142
|
|
|
$response = $exception->getResponse(); |
|
143
|
|
|
|
|
144
|
|
|
if (null === $response) { |
|
145
|
|
|
throw new ApiException($exception->getMessage()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($response->getStatusCode() === 500) { |
|
149
|
|
|
throw new DolibarrException(); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
throw new ApiException($exception->getMessage()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Do an authenticated request. |
|
158
|
|
|
* |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function authenticate() |
|
162
|
|
|
{ |
|
163
|
|
|
return []; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: