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