1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Adapter; |
13
|
|
|
|
14
|
|
|
use Buzz\Browser; |
15
|
|
|
use Buzz\Client\Curl; |
16
|
|
|
use Buzz\Client\FileGetContents; |
17
|
|
|
use Buzz\Message\Response; |
18
|
|
|
use Buzz\Middleware\MiddlewareInterface; |
19
|
|
|
use DigitalOceanV2\Exception\HttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Antoine Corcy <[email protected]> |
23
|
|
|
* @author Graham Campbell <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class BuzzAdapter implements AdapterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Browser |
29
|
|
|
*/ |
30
|
|
|
protected $browser; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $token |
34
|
|
|
* @param Browser|null $browser |
35
|
|
|
* @param MiddlewareInterface|null $middleware |
36
|
|
|
*/ |
37
|
|
|
public function __construct($token, Browser $browser = null, MiddlewareInterface $middleware = null) |
38
|
|
|
{ |
39
|
|
|
$this->browser = $browser ?: new Browser(function_exists('curl_exec') ? new Curl() : new FileGetContents()); |
40
|
|
|
$this->browser->addMiddleware($middleware ?: new BuzzOAuthMiddleware($token)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function get($url) |
47
|
|
|
{ |
48
|
|
|
$response = $this->browser->get($url); |
49
|
|
|
|
50
|
|
|
$this->handleResponse($response); |
51
|
|
|
|
52
|
|
|
return $response->getContent(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function delete($url) |
59
|
|
|
{ |
60
|
|
|
$response = $this->browser->delete($url); |
61
|
|
|
|
62
|
|
|
$this->handleResponse($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function put($url, $content = '') |
69
|
|
|
{ |
70
|
|
|
$headers = []; |
71
|
|
|
|
72
|
|
|
if (is_array($content)) { |
73
|
|
|
$content = json_encode($content); |
74
|
|
|
$headers[] = 'Content-Type: application/json'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$response = $this->browser->put($url, $headers, $content); |
78
|
|
|
|
79
|
|
|
$this->handleResponse($response); |
80
|
|
|
|
81
|
|
|
return $response->getContent(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function post($url, $content = '') |
88
|
|
|
{ |
89
|
|
|
$headers = []; |
90
|
|
|
|
91
|
|
|
if (is_array($content)) { |
92
|
|
|
$content = json_encode($content); |
93
|
|
|
$headers[] = 'Content-Type: application/json'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$response = $this->browser->post($url, $headers, $content); |
97
|
|
|
|
98
|
|
|
$this->handleResponse($response); |
99
|
|
|
|
100
|
|
|
return $response->getContent(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function getLatestResponseHeaders() |
107
|
|
|
{ |
108
|
|
|
if (null === $response = $this->browser->getLastResponse()) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return [ |
113
|
|
|
'reset' => (int) $response->getHeader('RateLimit-Reset'), |
114
|
|
|
'remaining' => (int) $response->getHeader('RateLimit-Remaining'), |
115
|
|
|
'limit' => (int) $response->getHeader('RateLimit-Limit'), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Response $response |
121
|
|
|
* |
122
|
|
|
* @throws HttpException |
123
|
|
|
*/ |
124
|
|
|
protected function handleResponse(Response $response) |
125
|
|
|
{ |
126
|
|
|
if ($response->getStatusCode() === 200) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->handleError($response); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Response $response |
135
|
|
|
* |
136
|
|
|
* @throws HttpException |
137
|
|
|
*/ |
138
|
|
|
protected function handleError(Response $response) |
139
|
|
|
{ |
140
|
|
|
$body = $response->getContent(); |
141
|
|
|
$code = $response->getStatusCode(); |
142
|
|
|
|
143
|
|
|
$content = json_decode($body); |
144
|
|
|
|
145
|
|
|
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|