1 | <?php |
||
35 | abstract class AbstractAPI |
||
36 | { |
||
37 | /** |
||
38 | * Http instance. |
||
39 | * |
||
40 | * @var \EasyWeChat\Core\Http |
||
41 | */ |
||
42 | protected $http; |
||
43 | |||
44 | /** |
||
45 | * The request token. |
||
46 | * |
||
47 | * @var \EasyWeChat\Core\AccessToken |
||
48 | */ |
||
49 | protected $accessToken; |
||
50 | |||
51 | const GET = 'get'; |
||
52 | const POST = 'post'; |
||
53 | const JSON = 'json'; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param \EasyWeChat\Core\AccessToken $accessToken |
||
59 | */ |
||
60 | 132 | public function __construct(AccessToken $accessToken) |
|
64 | |||
65 | /** |
||
66 | * Return the http instance. |
||
67 | * |
||
68 | * @return \EasyWeChat\Core\Http |
||
69 | */ |
||
70 | 10 | public function getHttp() |
|
82 | |||
83 | /** |
||
84 | * Set the http instance. |
||
85 | * |
||
86 | * @param \EasyWeChat\Core\Http $http |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | 13 | public function setHttp(Http $http) |
|
96 | |||
97 | /** |
||
98 | * Return the current accessToken. |
||
99 | * |
||
100 | * @return \EasyWeChat\Core\AccessToken |
||
101 | */ |
||
102 | 8 | public function getAccessToken() |
|
106 | |||
107 | /** |
||
108 | * Set the request token. |
||
109 | * |
||
110 | * @param \EasyWeChat\Core\AccessToken $accessToken |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | 132 | public function setAccessToken(AccessToken $accessToken) |
|
120 | |||
121 | /** |
||
122 | * Parse JSON from response and check error. |
||
123 | * |
||
124 | * @param string $method |
||
125 | * @param array $args |
||
126 | * |
||
127 | * @return \EasyWeChat\Support\Collection |
||
128 | */ |
||
129 | 7 | public function parseJSON($method, array $args) |
|
139 | |||
140 | /** |
||
141 | * Register Guzzle middlewares. |
||
142 | */ |
||
143 | 9 | protected function registerHttpMiddlewares() |
|
152 | |||
153 | /** |
||
154 | * Attache access token to request query. |
||
155 | * |
||
156 | * @return \Closure |
||
157 | */ |
||
158 | 9 | protected function accessTokenMiddleware() |
|
159 | { |
||
160 | return function (callable $handler) { |
||
161 | return function (RequestInterface $request, array $options) use ($handler) { |
||
162 | 1 | if (!$this->accessToken) { |
|
163 | return $handler($request, $options); |
||
164 | } |
||
165 | |||
166 | 1 | $field = $this->accessToken->getQueryName(); |
|
167 | 1 | $token = $this->accessToken->getToken(); |
|
168 | |||
169 | 1 | $request = $request->withUri(Uri::withQueryValue($request->getUri(), $field, $token)); |
|
170 | |||
171 | 1 | return $handler($request, $options); |
|
172 | 1 | }; |
|
173 | 9 | }; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Log the request. |
||
178 | * |
||
179 | * @return \Closure |
||
180 | */ |
||
181 | 9 | protected function logMiddleware() |
|
182 | { |
||
183 | return Middleware::tap(function (RequestInterface $request, $options) { |
||
184 | 1 | Log::debug("Request: {$request->getMethod()} {$request->getUri()} ".json_encode($options)); |
|
185 | 1 | Log::debug('Request headers:'.json_encode($request->getHeaders())); |
|
186 | 9 | }); |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Return retry middleware. |
||
191 | * |
||
192 | * @return \Closure |
||
193 | */ |
||
194 | protected function retryMiddleware() |
||
195 | { |
||
196 | 9 | return Middleware::retry(function ( |
|
197 | $retries, |
||
198 | RequestInterface $request, |
||
199 | ResponseInterface $response = null |
||
200 | ) { |
||
201 | // Limit the number of retries to 2 |
||
202 | 1 | if ($retries <= 2 && $response && $body = $response->getBody()) { |
|
203 | // Retry on server errors |
||
204 | 1 | if (stripos($body, 'errcode') && (stripos($body, '40001') || stripos($body, '42001'))) { |
|
205 | $field = $this->accessToken->getQueryName(); |
||
206 | $token = $this->accessToken->getToken(true); |
||
207 | |||
208 | $request = $request->withUri($newUri = Uri::withQueryValue($request->getUri(), $field, $token)); |
||
209 | |||
210 | Log::debug("Retry with Request Token: {$token}"); |
||
211 | Log::debug("Retry with Request Uri: {$newUri}"); |
||
212 | |||
213 | return true; |
||
214 | } |
||
215 | 1 | } |
|
216 | |||
217 | 1 | return false; |
|
218 | 9 | }); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Check the array data errors, and Throw exception when the contents contains error. |
||
223 | * |
||
224 | * @param array $contents |
||
225 | * |
||
226 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
227 | */ |
||
228 | 8 | protected function checkAndThrow(array $contents) |
|
238 | } |
||
239 |