|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopUnleashed; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
9
|
|
|
use GuzzleHttp\RequestOptions; |
|
10
|
|
|
use SilverStripe\Core\Config\Config_ForClass; |
|
11
|
|
|
use Monolog\Handler\StreamHandler; |
|
12
|
|
|
use Monolog\Logger; |
|
13
|
|
|
use Monolog\Formatter\LineFormatter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Settings to incorporate Unleashed API into a Guzzle Http Client |
|
17
|
|
|
*/ |
|
18
|
|
|
class UnleashedAPI extends Client |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Helper for getting static config |
|
22
|
|
|
* |
|
23
|
|
|
* The 'config' static function isn't available through Guzzle |
|
24
|
|
|
* @return Config_ForClass configuration object |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function config() |
|
27
|
|
|
{ |
|
28
|
|
|
return new Config_ForClass("AntonyThorpe\SilverShopUnleashed\UnleashedAPI"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Log calls if failed |
|
33
|
|
|
* @var boolean |
|
34
|
|
|
*/ |
|
35
|
|
|
public static $logfailedcalls = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Guzzle debug setting |
|
39
|
|
|
* @var boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
public static $debug = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* id from Unleashed |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $id; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* key from Unleashed |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private static $key; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Send asynchronous request to Restful endpoint |
|
57
|
|
|
* @param string $method Http Request Method |
|
58
|
|
|
* @param string $uri the Restful endpoint and query |
|
59
|
|
|
* @return object either the Guzzle Response Interface or an Guzzle Request Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function sendCall($method, $uri, $options = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$config = []; |
|
64
|
|
|
if ($uri) { |
|
65
|
|
|
$config['base_url'] = $uri; |
|
66
|
|
|
} |
|
67
|
|
|
if (isset($options['query'])) { |
|
68
|
|
|
$config['query'] = $options['query']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$client = new UnleashedAPI($config); |
|
72
|
|
|
|
|
73
|
|
|
if (self::config()->debug) { |
|
74
|
|
|
$options['debug'] = true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$promise = $client->requestAsync($method, $uri, $options); |
|
78
|
|
|
$promise->then( |
|
79
|
|
|
function (ResponseInterface $response) { |
|
80
|
|
|
return $response; |
|
81
|
|
|
}, |
|
82
|
|
|
function (RequestException $reason) { |
|
83
|
|
|
if (self::config()->logfailedcalls) { |
|
84
|
|
|
$line_formatter = new LineFormatter( |
|
85
|
|
|
null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n |
|
86
|
|
|
null, // Datetime format |
|
87
|
|
|
true, // allowInlineLineBreaks option, default false |
|
88
|
|
|
true // discard empty Square brackets in the end, default false |
|
89
|
|
|
); |
|
90
|
|
|
$logger = new Logger("unleashed-log"); |
|
91
|
|
|
$stream_handler = new StreamHandler('./z_silverstripe-unleashed.log', Logger::INFO); |
|
92
|
|
|
$stream_handler->setFormatter($line_formatter); |
|
93
|
|
|
$logger->pushHandler($stream_handler); |
|
94
|
|
|
$logger->info($reason->getMessage()); |
|
95
|
|
|
if (!empty($reason->getResponse())) { |
|
96
|
|
|
$logger->info(print_r($reason->getResponse()->getBody()->getContents(), true)); |
|
97
|
|
|
} |
|
98
|
|
|
$logger->info(print_r($reason->getRequest()->getMethod(), true)); |
|
99
|
|
|
$logger->info(print_r($reason->getRequest()->getHeaders(), true)); |
|
100
|
|
|
} |
|
101
|
|
|
return $reason; |
|
102
|
|
|
} |
|
103
|
|
|
); |
|
104
|
|
|
return $promise->wait(); // execute call and return result |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Clients accept an array of constructor parameters |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $config Client configuration settings. |
|
111
|
|
|
* @see \GuzzleHttp\RequestOptions for a list of available request options. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function __construct(array $config = []) |
|
114
|
|
|
{ |
|
115
|
|
|
// create the params for the base64_enclose |
|
116
|
|
|
$params = ""; |
|
117
|
|
|
if (isset($config['base_url'])) { |
|
118
|
|
|
$params = parse_url($config['base_url'], PHP_URL_QUERY); |
|
119
|
|
|
} |
|
120
|
|
|
if (isset($config['query'])) { |
|
121
|
|
|
if (!empty($params)) { |
|
122
|
|
|
$params .= '&'; |
|
123
|
|
|
} |
|
124
|
|
|
$params .= urldecode(http_build_query($config['query'])); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// add the headers |
|
128
|
|
|
$config['headers'] = [ |
|
129
|
|
|
'Content-Type' => 'application/json', |
|
130
|
|
|
'Accept' => 'application/json', |
|
131
|
|
|
'api-auth-id' => self::config()->id, |
|
132
|
|
|
'api-auth-signature' => base64_encode(hash_hmac('sha256', $params, self::config()->key, true)) |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
parent::__construct($config); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|