-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAPI.php
More file actions
executable file
·196 lines (167 loc) · 4.28 KB
/
AbstractAPI.php
File metadata and controls
executable file
·196 lines (167 loc) · 4.28 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
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* 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 <[email protected]>
*
* 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\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
/**
* Class AbstractAPI.
*/
abstract class AbstractAPI
{
/**
* Http instance.
*
* @var \EasyWeChat\Core\Http
*/
protected $http;
/**
* The reqeust token.
*
* @var \EasyWeChat\Core\AccessToken
*/
protected $accessToken;
const GET = 'get';
const POST = 'post';
const JSON = 'json';
/**
* 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()
{
return $this->http ?: $this->http = new Http();
}
/**
* Set the http instance.
*
* @param \EasyWeChat\Core\Http $http
*
* @return $this
*/
public function setHttp(Http $http)
{
$this->http = $http;
$this->attachAccessToken();
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;
$this->attachAccessToken();
return $this;
}
/**
* Return API url, and add some parameters.
*
* @param string $url
* @param array $quires
*
* @return string
*/
public function getAPI($url, array $quires = [])
{
return $url;
}
/**
* Parse JSON from response and check error.
*
* @param string $method
* @param array $args
* @param bool $throws
*
* @return \EasyWeChat\Support\Collection
*
* @throws \EasyWeChat\Core\Exceptions\HttpException
*/
public function parseJSON($method, $args, $throws = true)
{
$http = $this->getHttp();
$args = (array) $args;
// Add access token
$args['0'] = $this->getAPI($args[0]);
$contents = $http->parseJSON(call_user_func_array([$http, $method], $args));
$this->checkAndThrow($contents);
return new Collection($contents);
}
/**
* Set request access_token query.
*/
protected function attachAccessToken()
{
if (!$this->accessToken) {
return;
}
$this->getHttp()->addMiddleware(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$field = $this->accessToken->getQueryName();
$token = $this->accessToken->getToken();
$request = $request->withUri(Uri::withQueryValue($request->getUri(), $field, $token));
Log::debug("Request Token: {$token}");
Log::debug('Request Uri: '.$request->getUri());
return $handler($request, $options);
};
});
}
/**
* Check the array data erros, and Throw expcetion when the contents cotnains 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']);
}
}
}