1 | <?php |
||
24 | class GuzzleAdapter implements AdapterInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var ClientInterface |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * @var Response |
||
33 | */ |
||
34 | protected $response; |
||
35 | |||
36 | /** |
||
37 | * @param string $token |
||
38 | * @param ClientInterface|null $client |
||
39 | */ |
||
40 | public function __construct($token, ClientInterface $client = null) |
||
41 | { |
||
42 | $this->client = $client ?: new Client(); |
||
43 | |||
44 | $this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $token)); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function get($url) |
||
51 | { |
||
52 | try { |
||
53 | $this->response = $this->client->get($url)->send(); |
||
54 | } catch (RequestException $e) { |
||
55 | $this->response = $e->getResponse(); |
||
56 | $this->handleError(); |
||
57 | } |
||
58 | |||
59 | return $this->response->getBody(true); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function delete($url) |
||
66 | { |
||
67 | try { |
||
68 | $this->response = $this->client->delete($url)->send(); |
||
69 | } catch (RequestException $e) { |
||
70 | $this->response = $e->getResponse(); |
||
71 | $this->handleError(); |
||
72 | } |
||
73 | |||
74 | return $this->response->getBody(true); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function put($url, $content = '') |
||
81 | { |
||
82 | $request = $this->client->put($url); |
||
83 | |||
84 | if (is_array($content)) { |
||
85 | $request->setBody(json_encode($content), 'application/json'); |
||
86 | } else { |
||
87 | $request->setBody($content); |
||
88 | } |
||
89 | |||
90 | try { |
||
91 | $this->response = $request->send(); |
||
92 | } catch (RequestException $e) { |
||
93 | $this->response = $e->getResponse(); |
||
94 | $this->handleError(); |
||
95 | } |
||
96 | |||
97 | return $this->response->getBody(true); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function post($url, $content = '') |
||
104 | { |
||
105 | $request = $this->client->post($url); |
||
106 | |||
107 | if (is_array($content)) { |
||
108 | $request->setBody(json_encode($content), 'application/json'); |
||
109 | } else { |
||
110 | $request->setBody($content); |
||
111 | } |
||
112 | |||
113 | try { |
||
114 | $this->response = $request->send(); |
||
115 | } catch (RequestException $e) { |
||
116 | $this->response = $e->getResponse(); |
||
117 | $this->handleError(); |
||
118 | } |
||
119 | |||
120 | return $this->response->getBody(true); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getLatestResponseHeaders() |
||
127 | { |
||
128 | if (null === $this->response) { |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | return [ |
||
133 | 'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), |
||
134 | 'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), |
||
135 | 'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'), |
||
136 | ]; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @throws HttpException |
||
141 | */ |
||
142 | protected function handleError() |
||
143 | |||
144 | $body = (string) $this->response->getBody(true); |
||
|
|||
145 | $code = (int) $this->response->getStatusCode(); |
||
152 |