-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
71 lines (63 loc) · 1.8 KB
/
Api.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
<?php declare(strict_types=1);
/**
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* Unauthorized copying of this file, via any medium, is
* strictly prohibited without consent. Any dissemination of
* material herein is prohibited.
*
* For licensing inquiries email <licensing@apisnetworks.com>
*
* Written by Matt Saladna <matt@apisnetworks.com>, August 2018
*/
namespace Opcenter\Dns\Providers\Aws;
use Aws\Credentials\Credentials;
use Aws\Exception\AwsException;
class Api
{
const API_VERISON = '2013-04-01';
protected $proxy;
private function __construct(string $key, string $secret, string $abstract, array $options = [])
{
$credentials = new Credentials($key, $secret, $options['token'] ?? null, $options['expires'] ?? null);
$this->proxy = new $abstract($options + [
'credentials' => $credentials,
'version' => static::API_VERISON,
'auth_headers' => [
'User-Agent' => PANEL_BRAND . ' ' . APNSCP_VERSION,
],
]);
}
/**
* Create an Aws API instance
*
* @param string $key
* @param string $secret
* @param string $abstract
* @param array $options
* @return Api
*/
public static function api(string $key, string $secret, string $abstract, array $options = []): Api
{
return new static($key, $secret, $abstract, $options);
}
public function __call($name, $arguments)
{
static $count = 0;
try {
$ret = $this->proxy->$name(...$arguments);
$count = 0;
return $ret;
} catch (AwsException $e) {
$code = $e->getStatusCode();
if ($code === 429 || ($code === 400 && false !== strpos((string)$e->getAwsErrorMessage(), 'Rate exceeded')) ) {
usleep(250000);
if (++$count > 20) {
throw $e;
}
return $this->__call($name, $arguments);
}
throw $e;
}
}
}