1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalOceanV2\Adapter; |
4
|
|
|
|
5
|
|
|
use DigitalOceanV2\Exception\HttpException; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Event\CompleteEvent; |
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
10
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
11
|
|
|
use GuzzleHttp\Psr7\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Marcos Sigueros <[email protected]> |
15
|
|
|
* @author Chris Fidao <[email protected]> |
16
|
|
|
* @author Graham Campbell <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class GuzzleHttpAdapter implements AdapterInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClientInterface |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Response|ResponseInterface |
27
|
|
|
*/ |
28
|
|
|
protected $response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $token |
32
|
|
|
* @param ClientInterface|null $client |
33
|
|
|
*/ |
34
|
|
|
public function __construct($token, ClientInterface $client = null) |
35
|
|
|
{ |
36
|
|
|
if (version_compare(ClientInterface::VERSION, '6') === 1) { |
37
|
|
|
$this->client = $client ?: new Client(['headers' => ['Authorization' => sprintf('Bearer %s', $token)]]); |
38
|
|
|
} else { |
39
|
|
|
$this->client = $client ?: new Client(); |
40
|
|
|
|
41
|
|
|
$this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $token)); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function get($url) |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$this->response = $this->client->get($url); |
52
|
|
|
} catch (RequestException $e) { |
53
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
54
|
|
|
$this->handleError(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->response->getBody(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function delete($url) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$this->response = $this->client->delete($url); |
67
|
|
|
} catch (RequestException $e) { |
68
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
69
|
|
|
$this->handleError(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->response->getBody(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function put($url, $content = '') |
79
|
|
|
{ |
80
|
|
|
$options = []; |
81
|
|
|
|
82
|
|
|
$options[is_array($content) ? 'json' : 'body'] = $content; |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$this->response = $this->client->put($url, $options); |
86
|
|
|
} catch (RequestException $e) { |
87
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
88
|
|
|
$this->handleError(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->response->getBody(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function post($url, $content = '') |
98
|
|
|
{ |
99
|
|
|
$options = []; |
100
|
|
|
|
101
|
|
|
$options[is_array($content) ? 'json' : 'body'] = $content; |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
$this->response = $this->client->post($url, $options); |
105
|
|
|
} catch (RequestException $e) { |
106
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
107
|
|
|
$this->handleError(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->response->getBody(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function getLatestResponseHeaders() |
117
|
|
|
{ |
118
|
|
|
if (null === $this->response) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return [ |
123
|
|
|
'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), |
124
|
|
|
'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), |
125
|
|
|
'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'), |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @throws HttpException |
131
|
|
|
*/ |
132
|
|
|
protected function handleError() |
133
|
|
|
{ |
134
|
|
|
$body = (string) $this->response->getBody(); |
135
|
|
|
$code = (int) $this->response->getStatusCode(); |
136
|
|
|
|
137
|
|
|
$content = json_decode($body); |
138
|
|
|
|
139
|
|
|
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.