1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopUnleashed; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
|
|
|
7
|
|
|
use Monolog\Handler\StreamHandler; |
|
|
|
|
8
|
|
|
use Monolog\Logger; |
|
|
|
|
9
|
|
|
use Monolog\Formatter\LineFormatter; |
|
|
|
|
10
|
|
|
use SilverStripe\Core\Config\Config_ForClass; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Settings to incorporate Unleashed API into a Guzzle Http Client |
14
|
|
|
* @link https://apidocs.unleashedsoftware.com/AuthenticationHelp |
15
|
|
|
*/ |
16
|
|
|
class UnleashedAPI extends Client |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Helper for getting static config |
20
|
|
|
* The 'config' static function isn't available through Guzzle |
21
|
|
|
*/ |
22
|
|
|
public static function config(): Config_ForClass |
23
|
|
|
{ |
24
|
|
|
return new Config_ForClass("AntonyThorpe\SilverShopUnleashed\UnleashedAPI"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Log calls if failed |
29
|
|
|
*/ |
30
|
|
|
private static bool $logfailedcalls = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Log calls if failed |
34
|
|
|
*/ |
35
|
|
|
private static bool $logsuccessfulcalls = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Guzzle debug setting |
39
|
|
|
*/ |
40
|
|
|
private static bool $debug = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* id from Unleashed |
44
|
|
|
*/ |
45
|
|
|
private static string $id = ''; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* key from Unleashed |
49
|
|
|
*/ |
50
|
|
|
private static string $key = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* For the header to enable API tracking |
54
|
|
|
*/ |
55
|
|
|
private static string $client_type = ''; |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Send asynchronous request to Restful endpoint |
59
|
|
|
* @param string $method Http Request Method |
60
|
|
|
* @param string $uri the Restful endpoint and query |
61
|
|
|
* @param array $options for Guzzle Request |
62
|
|
|
* @return mixed either the Guzzle Response Interface or an Guzzle Request Exception |
63
|
|
|
*/ |
64
|
|
|
public static function sendCall(string $method, string $uri, array $options = []): mixed |
65
|
|
|
{ |
66
|
|
|
$config = []; |
67
|
|
|
if ($uri) { |
68
|
|
|
$config['base_url'] = $uri; |
69
|
|
|
} |
70
|
|
|
if (isset($options['query'])) { |
71
|
|
|
$config['query'] = $options['query']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$client = new UnleashedAPI($config); |
75
|
|
|
|
76
|
|
|
if (UnleashedAPI::config()->debug) { |
77
|
|
|
$options['debug'] = true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$response = $client->request($method, $uri, $options); |
82
|
|
|
if (UnleashedAPI::config()->logsuccessfulcalls) { |
83
|
|
|
$line_formatter = new LineFormatter( |
84
|
|
|
null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n |
85
|
|
|
null, // Datetime format |
86
|
|
|
true, // allowInlineLineBreaks option, default false |
87
|
|
|
true // discard empty Square brackets in the end, default false |
88
|
|
|
); |
89
|
|
|
$logger = new Logger('unleashed-successful'); |
90
|
|
|
$stream_handler = new StreamHandler('./z_unleashed-successful.log', Logger::INFO); |
91
|
|
|
$stream_handler->setFormatter($line_formatter); |
92
|
|
|
$logger->pushHandler($stream_handler); |
93
|
|
|
$logger->info(_t('UnleashedAPI.RequestSuccessTitle', 'Request successful') . '\n'); |
|
|
|
|
94
|
|
|
$logger->info(_t('UnleashedAPI.Method', 'Request method: ') . $method); |
95
|
|
|
$logger->info(_t('UnleashedAPI.Uri', 'Request uri: ') . $uri); |
96
|
|
|
$logger->info(_t('UnleashedAPI.Options', 'Request options: ')); |
97
|
|
|
$logger->info(print_r($options, true)); |
98
|
|
|
$logger->info(_t('UnleashedAPI.ResponseContent', 'Response Content: ')); |
99
|
|
|
$logger->info(print_r($response->getBody()->getContents(), true)); |
100
|
|
|
} |
101
|
|
|
return $response; |
102
|
|
|
} catch (ClientException $e) { |
103
|
|
|
if (UnleashedAPI::config()->logfailedcalls) { |
104
|
|
|
$line_formatter = new LineFormatter( |
105
|
|
|
null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n |
106
|
|
|
null, // Datetime format |
107
|
|
|
true, // allowInlineLineBreaks option, default false |
108
|
|
|
true // discard empty Square brackets in the end, default false |
109
|
|
|
); |
110
|
|
|
$logger = new Logger('unleashed-client-exception'); |
111
|
|
|
$stream_handler = new StreamHandler('./z_unleashed-client-exception.log', Logger::INFO); |
112
|
|
|
$stream_handler->setFormatter($line_formatter); |
113
|
|
|
$logger->pushHandler($stream_handler); |
114
|
|
|
$logger->info(_t('UnleashedAPI.ClientExceptionTitle', 'Request failed'). '\n'); |
115
|
|
|
$logger->info($e->getMessage()); |
116
|
|
|
$logger->info(print_r($e->getResponse()->getBody()->getContents(), true)); |
117
|
|
|
$logger->info($e->getRequest()->getMethod()); |
118
|
|
|
$logger->info(print_r($e->getRequest()->getHeaders(), true)); |
119
|
|
|
} |
120
|
|
|
return $e; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Clients accept an array of constructor parameters |
126
|
|
|
* |
127
|
|
|
* @param array $config Client configuration settings. |
128
|
|
|
* @see \GuzzleHttp\RequestOptions for a list of available request options. |
129
|
|
|
*/ |
130
|
|
|
public function __construct(array $config = []) |
131
|
|
|
{ |
132
|
|
|
// create the params for the base64_enclose |
133
|
|
|
$params = ''; |
134
|
|
|
if (isset($config['base_url'])) { |
135
|
|
|
$params = parse_url($config['base_url'], PHP_URL_QUERY) ?? ''; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($config['query'])) { |
139
|
|
|
if ($params) { |
140
|
|
|
$params .= '&'; |
141
|
|
|
} |
142
|
|
|
$params .= urldecode(http_build_query($config['query'])); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// add the headers |
146
|
|
|
$config['headers'] = [ |
147
|
|
|
'Content-Type' => 'application/json', |
148
|
|
|
'Accept' => 'application/json', |
149
|
|
|
'api-auth-id' => UnleashedAPI::config()->get('id'), |
150
|
|
|
'api-auth-signature' => base64_encode(hash_hmac('sha256', $params, UnleashedAPI::config()->key, true)) |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
if (UnleashedAPI::config()->get('client_type')) { |
154
|
|
|
$config['headers']['client-type'] = UnleashedAPI::config()->get('client_type'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
parent::__construct($config); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths