-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Configuration.php
491 lines (438 loc) · 12.3 KB
/
Configuration.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* Configuration
* PHP version 7.3
*
* @category Class
* @package AmazonBusinessApi
*/
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
namespace AmazonBusinessApi;
use AmazonBusinessApi\Contract\RequestSignerContract;
use Exception;
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use RuntimeException;
/**
* Configuration Class Doc Comment
* PHP version 7.3
*
* @category Class
* @package AmazonBusinessApi
*/
class Configuration
{
/**
* @const array[string]
*/
public const REQUIRED_CONFIG_KEYS = [
'lwaClientId', 'lwaClientSecret', 'lwaRefreshToken', 'awsAccessKeyId', 'awsSecretAccessKey', 'endpoint'
];
/**
* Auth object for the SP API
*
* @var Authentication
*/
protected $auth;
/**
* The SP API endpoint. One of the constant values from the Endpoint class.
*
* @var array
*/
protected $endpoint = Endpoint::NA;
/**
* User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default
*
* @var string
*/
protected $userAgent = 'highsidelabs/amazon-business-api/1.3.0 (Language=PHP)';
/**
* Debug switch (default set to false)
*
* @var bool
*/
protected $debug = false;
/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected $debugFile = 'php://output';
/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected static $tempFolderPath = null;
/**
* @var RequestSignerContract
*/
protected $requestSigner;
/**
* Constructor
* @param array $configurationOptions
* @throws Exception
*/
public function __construct(array $configurationOptions)
{
// Make sure all required configuration options are present
$missingKeys = [];
foreach (static::REQUIRED_CONFIG_KEYS as $key) {
if (!isset($configurationOptions[$key])) {
$missingKeys[] = $key;
}
}
if (count($missingKeys) > 0) {
throw new RuntimeException('Required configuration values were missing: ' . implode(', ', $missingKeys));
}
if (
(isset($configurationOptions['accessToken']) && !isset($configurationOptions['accessTokenExpiration'])) ||
(!isset($configurationOptions['accessToken']) && isset($configurationOptions['accessTokenExpiration']))
) {
throw new RuntimeException('If one of the `accessToken` or `accessTokenExpiration` configuration options is provided, the other must be provided as well');
}
$options = array_merge(
$configurationOptions,
[
'accessToken' => $configurationOptions['accessToken'] ?? null,
'accessTokenExpiration' => $configurationOptions['accessTokenExpiration'] ?? null,
'onUpdateCredentials' => $configurationOptions['onUpdateCredentials'] ?? null,
'roleArn' => $configurationOptions['roleArn'] ?? null,
]
);
$this->endpoint = $options['endpoint'];
$this->auth = new Authentication($options);
$this->setRequestSigner($options['requestSigner'] ?? $this->auth);
}
public function getRequestSigner(): RequestSignerContract
{
return $this->requestSigner;
}
public function setRequestSigner(RequestSignerContract $requestSigner): void
{
$this->requestSigner = $requestSigner;
}
/**
* Gets the host
*
* @return string Host
*/
public function getHost()
{
return $this->endpoint['url'];
}
/**
* Gets the stripped-down host (no protocol or trailing slash)
*
* @return string Host
*/
public function getBareHost()
{
$host = $this->getHost();
$noProtocol = preg_replace('/.+\:\/\//', ' ', $host);
return trim($noProtocol, '/');
}
/**
* Sets the user agent of the api client
*
* @param string $userAgent the user agent of the api client
*
* @throws InvalidArgumentException
* @return $this
*/
public function setUserAgent($userAgent)
{
if (!is_string($userAgent)) {
throw new InvalidArgumentException('User-agent must be a string.');
}
$this->userAgent = $userAgent;
return $this;
}
/**
* Gets the user agent of the api client
*
* @return string user agent
*/
public function getUserAgent()
{
return $this->userAgent;
}
/**
* Sets debug flag
*
* @param bool $debug Debug flag
*
* @return $this
*/
public function setDebug($debug)
{
$this->debug = $debug;
return $this;
}
/**
* Gets the debug flag
*
* @return bool
*/
public function getDebug()
{
return $this->debug;
}
/**
* Sets the debug file
*
* @param string $debugFile Debug file
*
* @return $this
*/
public function setDebugFile($debugFile)
{
$this->debugFile = $debugFile;
return $this;
}
/**
* Gets the debug file
*
* @return string
*/
public function getDebugFile()
{
return $this->debugFile;
}
/**
* Sets the temp folder path
*
* @param ?string $tempFolderPath Temp folder path
* @return void
*/
public static function setTempFolderPath(?string $tempFolderPath = null): void
{
if ($tempFolderPath === null) {
static::$tempFolderPath = sys_get_temp_dir();
} else {
static::$tempFolderPath = $tempFolderPath;
}
}
/**
* Gets the temp folder path
*
* @return string Temp folder path
*/
public static function getTempFolderPath()
{
if (isset(static::$tempFolderPath) || static::$tempFolderPath === null) {
static::setTempFolderPath();
}
return static::$tempFolderPath;
}
/**
* Get the datetime string that was used to sign the most recently signed Selling Partner API request
*
* @return \DateTime The current time
*/
public function getRequestDatetime()
{
return $this->auth->formattedRequestTime();
}
/**
* Get LWA client ID.
*
* @return string
*/
public function getLwaClientId(): ?string
{
return $this->auth->getLwaClientId();
}
/**
* Set LWA client ID.
*
* @param string $lwaClientId
* @return void
*/
public function setLwaClientId(string $lwaClientId): void
{
$this->auth->setLwaClientId($lwaClientId);
}
/**
* Get LWA client secret.
*
* @return string
*/
public function getLwaClientSecret(): ?string
{
return $this->auth->getLwaClientSecret();
}
/**
* Set LWA client secret.
*
* @param string $lwaClientSecret
* @return void
*/
public function setLwaClientSecret(string $lwaClientSecret): void
{
$this->auth->setLwaClientSecret($lwaClientSecret);
}
/**
* Get LWA refresh token.
*
* @return string
*/
public function getLwaRefreshToken(): ?string
{
return $this->auth->getLwaRefreshToken();
}
/**
* Set LWA refresh token.
*
* @param string|null $lwaRefreshToken
* @return void
*/
public function setLwaRefreshToken(?string $lwaRefreshToken = null): void
{
$this->auth->setLwaRefreshToken($lwaRefreshToken);
}
/**
* Get AWS access key ID.
*
* @return string
*/
public function getAwsAccessKeyId(): ?string
{
return $this->auth->getAwsAccessKeyId();
}
/**
* Set AWS access key ID.
*
* @param string $awsAccessKeyId
* @return void
*/
public function setAwsAccessKeyId(string $awsAccessKeyId): void
{
$this->auth->setAwsAccessKeyId($awsAccessKeyId);
}
/**
* Get AWS secret access key.
*
* @return string|null
*/
public function getAwsSecretAccessKey(): ?string
{
return $this->auth->getAwsSecretAccessKey();
}
/**
* Set AWS secret access key.
*
* @param string $awsSecretAccessKey
* @return void
*/
public function setAwsSecretAccessKey(string $awsSecretAccessKey): void
{
$this->auth->setAwsSecretAccessKey($awsSecretAccessKey);
}
/**
* Get current SP API endpoint.
*
* @return array
*/
public function getEndpoint(): array
{
return $this->endpoint;
}
/**
* Set SP API endpoint. $endpoint should be one of the constants from Endpoint.php.
*
* @param array $endpoint
* @throws RuntimeException
* @return void
*/
public function setEndpoint(array $endpoint): void
{
if (!array_key_exists('url', $endpoint) || !array_key_exists('region', $endpoint)) {
throw new RuntimeException('$endpoint must contain `url` and `region` keys');
}
$this->endpoint = $endpoint;
$this->auth->setEndpoint($endpoint);
}
/**
* Sign a request to the Selling Partner API using the AWS Signature V4 protocol.
*
* @param Request $request The request to sign
*
* @return Request The signed request
*/
public function signRequest(Request $request): Request
{
return $this->requestSigner->signRequest($request);
}
/**
* Gets the essential information for debugging
*
* @param string|null $tempFolderPath The path to the temp folder.
* @return string The report for debugging
*/
public static function toDebugReport(?string $tempFolderPath = null)
{
if ($tempFolderPath === null) {
$tempFolderPath = static::getTempFolderPath();
}
$report = 'PHP SDK (AmazonBusinessApi) Debug Report:' . PHP_EOL;
$report .= ' OS: ' . php_uname() . PHP_EOL;
$report .= ' PHP Version: ' . PHP_VERSION . PHP_EOL;
$report .= ' The version of the OpenAPI document: 2020-11-01' . PHP_EOL;
$report .= ' SDK Package Version: 1.3.0' . PHP_EOL;
$report .= ' Temp Folder Path: ' . $tempFolderPath . PHP_EOL;
return $report;
}
/**
* Returns an array of host settings
*
* @return array an array of host settings
*/
public function getHostSettings()
{
return [
[
'url' => 'https://api.business.amazon.com',
'description' => 'No description provided',
]
];
}
/**
* Returns URL based on the index and variables
*
* @param int $index index of the host settings
* @param array|null $variables hash of variable and the corresponding value (optional)
* @return string URL based on host settings
*/
public function getHostFromSettings($index, $variables = null)
{
if (null === $variables) {
$variables = [];
}
$hosts = $this->getHostSettings();
// check array index out of bound
if ($index < 0 || $index >= count($hosts)) {
throw new InvalidArgumentException('Invalid index $index when selecting the host. Must be less than '.count($hosts));
}
$host = $hosts[$index];
$url = $host['url'];
// go through variable and assign a value
foreach ($host['variables'] ?? [] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable['enum_values'], true)) { // check to see if the value is in the enum
$url = str_replace('{'.$name.'}', $variables[$name], $url);
} else {
throw new InvalidArgumentException('The variable `$name` in the host URL has invalid value '.$variables[$name].'. Must be '.implode(',', $variable['enum_values']).'.');
}
} else {
// use default value
$url = str_replace('{'.$name.'}', $variable['default_value'], $url);
}
}
return $url;
}
}