-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrapper.php
387 lines (351 loc) · 16.2 KB
/
Wrapper.php
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php /** @noinspection PhpPropertyOnlyWrittenInspection */
namespace HMS\Core;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleHttp\RequestOptions;
use JetBrains\PhpStorm\ArrayShape;
use Monolog\Logger;
use Psr\Http\Message\ResponseInterface;
use stdClass;
/**
* Class HMS Core Wrapper
*
* @property int $oauth2_client_id OAuth2 client ID (app_id).
* @property string|null $oauth2_client_secret OAuth2 client secret (app_secret).
* @property string|null $oauth2_api_scope OAuth2 client side flow
* @property string|null $oauth2_redirect_url OAuth2 client side flow
* @property string|null $access_token OAuth2 (app) access token.
* @property string|null $refresh_token OAuth2 refresh token.
* @property string|null $id_token OAuth2 ID token.
* @property int $token_expiry OAuth2 access token expiry.
* @property string|null $api_key MapKit API key.
* @property string|null $api_signature MapKit Static API signature key.
* @property string|null $package_name AnalyticsKit related; for PushKit click_action?
* @property int $product_id AnalyticsKit related.
* @property int $agc_team_client_id AGConnect API client ID, admin level.
* @property string|null $agc_team_client_secret AGConnect API client secret, admin level.
* @property int $agc_project_client_id AGConnect API client ID, project level.
* @property string|null $agc_project_client_secret AGConnect API client secret, project level.
* @property int $agc_app_client_id AGConnect API client ID, app level.
* @property string|null $agc_app_client_secret AGConnect API client secret, app level.
* @property ResponseInterface $response Default response.
* @property stdClass $result Default API result.
* @property array $headers Default request headers.
* @author Martin Zeitler
*/
abstract class Wrapper {
protected int $oauth2_client_id = 0;
protected string|null $oauth2_client_secret = null;
/** client-side flow */
protected string $oauth2_api_scope = 'openid profile';
/** client-side flow */
protected string|null $oauth2_redirect_url = null;
protected int $token_expiry = 0;
protected string|null $api_key = null;
protected string|null $api_signature = null;
protected string|null $access_token = null;
protected string|null $refresh_token = null;
protected string|null $id_token = null;
protected string|null $package_name = null;
protected int $developer_id = 0;
protected int $project_id = 0;
/** AGC admin level access credentials */
protected int $agc_team_client_id = 0;
/** AGC admin level access credentials */
protected string|null $agc_team_client_secret = null;
/** AGC project level access credentials */
protected int $agc_project_client_id = 0;
/** AGC project level access credentials */
protected string|null $agc_project_client_secret = null;
/** AGC app level access credentials */
protected int $agc_app_client_id = 0;
/** AGC app level access credentials */
protected string|null $agc_app_client_secret = null;
protected bool $debug_mode = false;
protected string $base_url;
protected Client $client;
protected ResponseInterface $response;
protected stdClass $result;
/** Constructor. */
public function __construct( array|null $config = null ) {
$this->init( $config );
}
/** Initialize the client; either by array or by environmental variables. */
private function init( array|null $config = null): void {
$this->result = new stdClass();
if ( is_array( $config ) && isset( $config['debug_mode'] ) && is_bool($config['debug_mode']) ) {
$this->debug_mode = $config['debug_mode'];
}
if ( $this->debug_mode ) {
$templates = [
'{code} >> {req_headers}',
'{code} >> {req_body}',
'{code} << {res_headers}',
'{code} << {res_body}'
];
$handlers = HandlerStack::create();
$logger = new Logger('Logger');
foreach ($templates as $template) {
$handlers->unshift($this->getMiddleware($logger, $template));
}
$this->client = new Client( [
RequestOptions::VERIFY => !$this->is_windows(),
RequestOptions::DEBUG => false,
'handler' => $handlers
] );
} else {
$this->client = new Client( [
RequestOptions::VERIFY => !$this->is_windows(),
RequestOptions::DEBUG => false
] );
}
if ( is_array( $config ) ) {
$this->init_by_array( $config );
} else {
$this->init_by_environment();
}
}
private function getMiddleware( Logger $logger, string $template ): callable {
return Middleware::log($logger, new MessageFormatter($template));
}
/**
* Not all class properties of Core\Wrapper are used by each particular child class.
* Unset irrelevant properties per child class, in order to make debugging easier.
*/
protected abstract function post_init(): void;
/** Determine if running on Windows. */
private function is_windows(): bool {
return DIRECTORY_SEPARATOR === '\\';
}
/**
* Determine if an access token has already been fetched.
* Implementations may check for the presence of other values.
*/
public function is_ready(): bool {
return $this->access_token != null;
}
public function withDebugEnabled(): Wrapper {
$this->debug_mode = true;
return $this;
}
public function withBaseUrl( string $base_url ): Wrapper {
$this->base_url = $base_url;
return $this;
}
/** Try to initialize the client from array. */
private function init_by_array( array $config ): void {
if ( isset( $config['oauth2_client_id'] ) ) {
$this->oauth2_client_id = (int) $config['oauth2_client_id'];
}
if ( isset( $config['oauth2_client_secret'] ) ) {
$this->oauth2_client_secret = (string) $config['oauth2_client_secret'];
}
if ( isset( $config['oauth2_api_scope'] ) ) {
$this->oauth2_api_scope = (string) $config['oauth2_api_scope'];
}
if ( isset( $config['oauth2_redirect_url'] ) ) {
$this->oauth2_redirect_url = (string) $config['oauth2_redirect_url'];
}
if ( isset( $config['agc_team_client_id'] ) ) {
$this->agc_team_client_id = (int) $config['agc_team_client_id'];
}
if ( isset( $config['agc_team_client_secret'] ) ) {
$this->agc_team_client_secret = (string) $config['agc_team_client_secret'];
}
if ( isset( $config['agc_project_client_id'] ) ) {
$this->agc_project_client_id = (int) $config['agc_project_client_id'];
}
if ( isset( $config['agc_project_client_secret'] ) ) {
$this->agc_project_client_secret = (string) $config['agc_project_client_secret'];
}
if ( isset( $config['agc_app_client_id'] ) ) {
$this->agc_app_client_id = (int) $config['agc_project_client_id'];
}
if ( isset( $config['agc_app_client_secret'] ) ) {
$this->agc_app_client_secret = (string) $config['agc_app_client_secret'];
}
if ( isset($config['developer_id']) && is_int($config['developer_id'])) {
$this->developer_id = $config['developer_id'];
}
if ( isset($config['project_id']) && is_int($config['project_id'])) { // product_id.
$this->project_id = $config['project_id'];
}
if ( isset($config['package_name']) && is_int($config['package_name'])) {
$this->package_name = $config['package_name'];
}
if ( isset( $config['api_key'] ) ) {
$this->api_key = (string) $config['api_key'];
}
if ( isset( $config['api_signature'] ) ) {
$this->api_signature = (string) $config['api_signature'];
}
}
/** Try to initialize the client from environment. */
private function init_by_environment(): void {
if ( is_string( getenv('HUAWEI_OAUTH2_CLIENT_ID' ) ) ) {
$this->oauth2_client_id = (int) getenv( 'HUAWEI_OAUTH2_CLIENT_ID' );
}
if ( is_string( getenv('HUAWEI_OAUTH2_CLIENT_SECRET' ) ) ) {
$this->oauth2_client_secret = (string) getenv( 'HUAWEI_OAUTH2_CLIENT_SECRET' );
}
if ( is_string( getenv('HUAWEI_OAUTH2_API_SCOPE' ) ) ) {
$this->oauth2_api_scope = (string) getenv( 'HUAWEI_OAUTH2_API_SCOPE' );
}
if ( is_string( getenv('HUAWEI_OAUTH2_REDIRECT_URL' ) ) ) {
$this->oauth2_redirect_url = (string) getenv( 'HUAWEI_OAUTH2_REDIRECT_URL' );
}
if ( is_string( getenv('HUAWEI_CONNECT_TEAM_CLIENT_ID' ) ) ) { // team_client_id.
$this->agc_team_client_id = (int) getenv( 'HUAWEI_CONNECT_TEAM_CLIENT_ID' );
}
if ( is_string( getenv('HUAWEI_CONNECT_TEAM_CLIENT_SECRET' ) ) ) { // team_client_id.
$this->agc_team_client_secret = (string) getenv( 'HUAWEI_CONNECT_TEAM_CLIENT_SECRET' );
}
if ( is_string( getenv('HUAWEI_CONNECT_PROJECT_CLIENT_ID' ) ) ) { // project_client_id.
$this->agc_project_client_id = (int) getenv( 'HUAWEI_CONNECT_PROJECT_CLIENT_ID' );
}
if ( is_string( getenv('HUAWEI_CONNECT_PROJECT_CLIENT_SECRET' ) ) ) { // project_client_id.
$this->agc_project_client_secret = (string) getenv( 'HUAWEI_CONNECT_PROJECT_CLIENT_SECRET' );
}
if ( is_string( getenv('HUAWEI_CONNECT_APP_CLIENT_ID' ) ) ) {
$this->agc_app_client_id = (int) getenv( 'HUAWEI_CONNECT_APP_CLIENT_ID' );
}
if ( is_string( getenv('HUAWEI_CONNECT_APP_CLIENT_SECRET' ) ) ) {
$this->agc_app_client_secret = (string) getenv( 'HUAWEI_CONNECT_APP_CLIENT_SECRET' );
}
if ( is_string( getenv('HUAWEI_CONNECT_DEVELOPER_ID' ) ) ) {
$this->developer_id = (int) getenv( 'HUAWEI_CONNECT_DEVELOPER_ID' );
}
if ( is_string( getenv('HUAWEI_CONNECT_PROJECT_ID' ) ) ) { // product_id.
$this->project_id = (int) getenv( 'HUAWEI_CONNECT_PROJECT_ID' );
}
if ( is_string( getenv('HUAWEI_CONNECT_PACKAGE_NAME' ) ) ) {
$this->package_name = (int) getenv( 'HUAWEI_CONNECT_PACKAGE_NAME' );
}
if ( is_string( getenv('HUAWEI_MAPKIT_API_KEY' ) ) ) {
$this->api_key = (string) getenv( 'HUAWEI_MAPKIT_API_KEY' );
}
if ( is_string( getenv('HUAWEI_MAPKIT_SIGNATURE_KEY' ) ) ) {
$this->api_signature = (string) getenv( 'HUAWEI_MAPKIT_SIGNATURE_KEY' );
}
}
/** Provide HTTP request headers as array. */
#[ArrayShape(['Content-Type' => 'string'])]
protected function request_headers(): array {
return [ 'Content-Type' => 'application/json;charset=utf-8' ];
}
/** Provide HTTP request headers as array. */
#[ArrayShape(['Content-Type' => 'string', 'Authorization' => 'string'])]
protected function auth_headers(): array {
return [
'Content-Type' => 'application/json;charset=utf-8',
'Authorization' => ' Bearer ' . $this->access_token
];
}
/** Perform GuzzleHttp GET/POST/PATCH/PUT/DELETE request. */
protected function request( string $method='POST', ?string $url=null, array $headers=[], array|object|string $post_data=[], $urlencoded=false ): stdClass|bool {
$request = [ RequestOptions::HEADERS => $headers, RequestOptions::DEBUG => $this->debug_mode ];
try {
switch ($method) {
case 'GET':
$request[RequestOptions::QUERY] = $post_data; // as query string.
$this->response = $this->client->get( $url, $request );
break;
case 'POST':
if ($urlencoded) {
$request[RequestOptions::FORM_PARAMS] = $post_data; // as form.
} else {
if (is_array($post_data)) {
$request[RequestOptions::JSON] = $post_data; // as JSON.
} else if (is_string($post_data)) {
$request[RequestOptions::BODY] = $post_data; // as raw string.
}
}
$this->response = $this->client->post( $url, $request );
break;
case 'PATCH':
$request[RequestOptions::JSON] = $post_data;
$this->response = $this->client->patch( $url, $request );
break;
case 'PUT':
$request[RequestOptions::JSON] = $post_data;
$this->response = $this->client->put( $url, $request );
break;
case 'DELETE':
$request[RequestOptions::JSON] = $post_data;
$this->response = $this->client->delete( $url, $request );
break;
}
$this->result->code = $this->response->getStatusCode();
if ($this->result->code == 200) {
$content_type = strtolower($this->response->getHeader('Content-Type')[0]);
switch ($content_type) {
case 'application/json':
case 'application/json;charset=utf-8':
case 'application/json; charset=utf-8':
$this->result = json_decode( $this->response->getBody() );
break;
case 'application/octet-stream': // DriveKit download
$binary = $this->response->getBody()->getContents();
$this->result->base64 = base64_encode($binary);
$this->result->raw = $binary;
break;
case 'image/png': // MapKit download
$binary = $this->response->getBody()->getContents();
$this->result->url = 'data:image/png;base64,'.base64_encode($binary);
$this->result->raw = $binary;
break;
}
}
} catch (GuzzleException $e) {
$this->result->code = $e->getCode();
$this->result->message = $e->getMessage();
}
return $this->sanitize( $this->result );
}
/** Different kinds of field descriptors may be returned ... */
protected function sanitize(object $data ): object {
/** $data->code */
if ( property_exists( $data, 'code') ) {
$data->code = (int) $data->code;
}
if (property_exists($data, 'result_code')) {
$data->code = (int) $data->result_code;
unset( $data->result_code );
}
if (property_exists($data, 'resultCode')) {
$data->code = (int) $data->resultCode;
unset( $data->resultCode );
}
/** $data->message */
if (property_exists($data, 'msg')) {
$data->message = (string) $data->msg;
unset( $data->msg );
}
if (property_exists($data, 'result_msg')) {
$data->message = (string) $data->result_msg;
unset( $data->result_msg );
}
if (property_exists($data, 'resultMsg')) {
$data->message = (string) $data->resultMsg;
unset( $data->resultMsg );
}
/** $data->sub_error */
if (property_exists($data, 'sub_error')) {
$data->sub_error = (int) $data->sub_error;
}
/** $data->ret */
if (property_exists($data, 'ret')) {
if (property_exists($data->ret, 'code')) {
$data->code = (int) $data->ret->code;
}
if (property_exists($data->ret, 'msg')) {
$data->message = (string) $data->ret->msg;
}
unset( $data->ret );
}
return $data;
}
}