1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\ApiGateway\Kernel; |
4
|
|
|
|
5
|
|
|
class TencentCloudClient extends BaseClient |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Make a API request. |
9
|
|
|
* |
10
|
|
|
* @param string $url |
11
|
|
|
* @param string $method |
12
|
|
|
* @param array $options |
13
|
|
|
* @param bool $returnRaw |
14
|
|
|
* |
15
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
16
|
|
|
* @throws Exceptions\InvalidConfigException |
17
|
|
|
*/ |
18
|
|
|
public function request($url, $method = 'GET', array $options = [], $returnRaw = false) |
19
|
|
|
{ |
20
|
|
|
if (empty($this->middlewares)) { |
21
|
|
|
$this->registerHttpMiddlewares(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$response = $this->performRequest($url, $method, $options); |
|
|
|
|
25
|
|
|
|
26
|
|
|
return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* GET request. |
31
|
|
|
* |
32
|
|
|
* @param string $url |
33
|
|
|
* @param array $query |
34
|
|
|
* |
35
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
36
|
|
|
* @throws Exceptions\InvalidConfigException |
37
|
|
|
*/ |
38
|
|
|
public function httpGet($url, array $query = []) |
39
|
|
|
{ |
40
|
|
|
$query = array_merge($this->getCommonParameters(), $query); |
41
|
|
|
|
42
|
|
|
$query['Signature'] = $this->signature( |
43
|
|
|
'GET', $this->baseUri . $url, $query |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
return $this->request($url, 'GET', ['query' => $query]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* POST request. |
51
|
|
|
* |
52
|
|
|
* @param string $url |
53
|
|
|
* @param array $data |
54
|
|
|
* |
55
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
56
|
|
|
* @throws Exceptions\InvalidConfigException |
57
|
|
|
*/ |
58
|
|
|
public function httpPost($url, array $data = []) |
59
|
|
|
{ |
60
|
|
|
$data = array_merge($this->getCommonParameters(), $data); |
61
|
|
|
|
62
|
|
|
$data['Signature'] = $this->signature( |
63
|
|
|
'POST', $this->baseUri . $url, $data |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $method |
71
|
|
|
* @param $url |
72
|
|
|
* @param array $params |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function signature($method, $url, array $params = []) |
77
|
|
|
{ |
78
|
|
|
ksort($params); |
79
|
|
|
|
80
|
|
|
$srcStr = sprintf('%s%s%s?%s', |
81
|
|
|
$method, |
82
|
|
|
parse_url($url, PHP_URL_HOST), |
83
|
|
|
parse_url($url, PHP_URL_PATH), |
84
|
|
|
$this->canonicalize($params) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return base64_encode( |
88
|
|
|
hash_hmac('sha1', $srcStr, $this->app->getSecretKey(), true) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function getCommonParameters() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'Timestamp' => time(), |
99
|
|
|
'Nonce' => rand(1, 65535), |
100
|
|
|
'SecretId' => $this->app->getSecretId(), |
101
|
|
|
'Region' => $this->app->getRegion(), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $input |
107
|
|
|
* @param string $keyPrefix |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function canonicalize(array $input, $keyPrefix = '') |
112
|
|
|
{ |
113
|
|
|
$resource = []; |
114
|
|
|
|
115
|
|
|
foreach ($input as $key => $value) { |
116
|
|
|
|
117
|
|
|
$key = $keyPrefix ? $keyPrefix . '.' . $key : $key; |
118
|
|
|
|
119
|
|
|
$resource[] = is_array($value) |
120
|
|
|
? $this->canonicalize($value, $key) |
121
|
|
|
: $key . '=' . $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return implode('&', $resource); |
125
|
|
|
} |
126
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.