AbstractAPI.php
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* AbstractAPI.php.
*
* This file is part of the wechat-components.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Core;
use EasyWeChat\Core\Exceptions\HttpException;
use EasyWeChat\Support\Collection;
use EasyWeChat\Support\Log;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Class AbstractAPI.
*/
abstract class AbstractAPI
{
/**
* Http instance.
*
* @var \EasyWeChat\Core\Http
*/
protected $http;
/**
* The request token.
*
* @var \EasyWeChat\Core\AccessToken
*/
protected $accessToken;
const GET = 'get';
const POST = 'post';
const JSON = 'json';
/**
* @var int
*/
protected static $maxRetries = 2;
/**
* Constructor.
*
* @param \EasyWeChat\Core\AccessToken $accessToken
*/
public function __construct(AccessToken $accessToken)
{
$this->setAccessToken($accessToken);
}
/**
* Return the http instance.
*
* @return \EasyWeChat\Core\Http
*/
public function getHttp()
{
if (is_null($this->http)) {
$this->http = new Http();
}
if (0 === count($this->http->getMiddlewares())) {
$this->registerHttpMiddlewares();
}
return $this->http;
}
/**
* Set the http instance.
*
* @param \EasyWeChat\Core\Http $http
*
* @return $this
*/
public function setHttp(Http $http)
{
$this->http = $http;
return $this;
}
/**
* Return the current accessToken.
*
* @return \EasyWeChat\Core\AccessToken
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* Set the request token.
*
* @param \EasyWeChat\Core\AccessToken $accessToken
*
* @return $this
*/
public function setAccessToken(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
return $this;
}
/**
* @param int $retries
*/
public static function maxRetries($retries)
{
self::$maxRetries = abs($retries);
}
/**
* Parse JSON from response and check error.
*
* @param string $method
* @param array $args
*
* @return \EasyWeChat\Support\Collection | null
*
* @throws \EasyWeChat\Core\Exceptions\HttpException
*/
public function parseJSON($method, array $args)
{
$http = $this->getHttp();
$contents = $http->parseJSON(call_user_func_array([$http, $method], $args));
if (empty($contents)) {
return null;
}
$this->checkAndThrow($contents);
return new Collection($contents);
}
/**
* Register Guzzle middlewares.
*/
protected function registerHttpMiddlewares()
{
// log
$this->http->addMiddleware($this->logMiddleware());
// retry
$this->http->addMiddleware($this->retryMiddleware());
// access token
$this->http->addMiddleware($this->accessTokenMiddleware());
}
/**
* Attache access token to request query.
*
* @return \Closure
*/
protected function accessTokenMiddleware()
{
return function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
if (!$this->accessToken) {
return $handler($request, $options);
}
$field = $this->accessToken->getQueryName();
$token = $this->accessToken->getToken();
$request = $request->withUri(Uri::withQueryValue($request->getUri(), $field, $token));
return $handler($request, $options);
};
};
}
/**
* Log the request.
*
* @return \Closure
*/
protected function logMiddleware()
{
return Middleware::tap(function (RequestInterface $request, $options) {
Log::debug("Request: {$request->getMethod()} {$request->getUri()} ".json_encode($options));
Log::debug('Request headers:'.json_encode($request->getHeaders()));
});
}
/**
* Return retry middleware.
*
* @return \Closure
*/
protected function retryMiddleware()
{
return Middleware::retry(function (
$retries,
RequestInterface $request,
ResponseInterface $response = null
) {
// Limit the number of retries to 2
if ($retries <= self::$maxRetries && $response && $body = $response->getBody()) {
// Retry on server errors
if (false !== stripos($body, 'errcode') && (false !== stripos($body, '40001') || false !== stripos($body, '42001'))) {
$field = $this->accessToken->getQueryName();
$token = $this->accessToken->getToken(true);
$request = $request->withUri($newUri = Uri::withQueryValue($request->getUri(), $field, $token));
Log::debug("Retry with Request Token: {$token}");
Log::debug("Retry with Request Uri: {$newUri}");
return true;
}
}
return false;
});
}
/**
* Check the array data errors, and Throw exception when the contents contains error.
*
* @param array $contents
*
* @throws \EasyWeChat\Core\Exceptions\HttpException
*/
protected function checkAndThrow(array $contents)
{
if (isset($contents['errcode']) && 0 !== $contents['errcode']) {
if (empty($contents['errmsg'])) {
$contents['errmsg'] = 'Unknown';
}
throw new HttpException($contents['errmsg'], $contents['errcode']);
}
}
}