|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CloudyCity\KuaishouMarketingSDK\Kernel; |
|
4
|
|
|
|
|
5
|
|
|
use CloudyCity\KuaishouMarketingSDK\Kernel\Exceptions\ApiException; |
|
6
|
|
|
use CloudyCity\KuaishouMarketingSDK\Kernel\Traits\HasHttpRequests; |
|
7
|
|
|
use CloudyCity\KuaishouMarketingSDK\Kernel\Traits\HasSdkBaseInfo; |
|
8
|
|
|
use GuzzleHttp\Psr7\Request; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Client. |
|
12
|
|
|
*/ |
|
13
|
|
|
class BaseClient |
|
14
|
|
|
{ |
|
15
|
|
|
use HasHttpRequests, HasSdkBaseInfo { |
|
16
|
|
|
request as performRequest; |
|
17
|
|
|
HasHttpRequests::getResponseType insteadof HasSdkBaseInfo; |
|
18
|
|
|
HasHttpRequests::setResponseType insteadof HasSdkBaseInfo; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $baseUri = 'https://ad.e.kuaishou.com/rest/openapi/'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Client constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $advertiserId |
|
30
|
|
|
* @param string $accessToken |
|
31
|
|
|
* @param string $responseType |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($advertiserId, $accessToken, $responseType = 'array') |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setAdvertiserId($advertiserId); |
|
36
|
|
|
$this->setAccessToken($accessToken); |
|
37
|
|
|
$this->setResponseType($responseType); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* GET request. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $url |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
* @param array $query |
|
46
|
|
|
* |
|
47
|
|
|
* @throws ApiException |
|
48
|
|
|
* @throws Exceptions\InvalidArgumentException |
|
49
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\CloudyCity\KuaishouMarketingSDK\Kernel\Support\Collection|array|object|string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function httpGetJson($url, array $data = [], array $query = []) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->request($url, 'GET', ['query' => $query, 'json' => $data]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* JSON request. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $url |
|
62
|
|
|
* @param array $data |
|
63
|
|
|
* |
|
64
|
|
|
* @throws ApiException |
|
65
|
|
|
* @throws Exceptions\InvalidArgumentException |
|
66
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\CloudyCity\KuaishouMarketingSDK\Kernel\Support\Collection|array|object|string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function httpPost($url, array $data = []) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* JSON request. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $url |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @param array $query |
|
81
|
|
|
* |
|
82
|
|
|
* @throws ApiException |
|
83
|
|
|
* @throws Exceptions\InvalidArgumentException |
|
84
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\CloudyCity\KuaishouMarketingSDK\Kernel\Support\Collection|array|object|string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function httpPostJson($url, array $data = [], array $query = []) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $url |
|
95
|
|
|
* @param string $method |
|
96
|
|
|
* @param array $options |
|
97
|
|
|
* @param bool $returnRaw |
|
98
|
|
|
* |
|
99
|
|
|
* @throws ApiException |
|
100
|
|
|
* @throws Exceptions\InvalidArgumentException |
|
101
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\CloudyCity\KuaishouMarketingSDK\Kernel\Support\Collection|array|object|string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function request($url, $method = 'POST', array $options = [], $returnRaw = false) |
|
106
|
|
|
{ |
|
107
|
|
|
if (empty($this->middlewares)) { |
|
108
|
|
|
$this->registerHttpMiddlewares(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$options['json']['advertiser_id'] = $this->getAdvertiserId(); |
|
112
|
|
|
$response = $this->performRequest($url, $method, $options); |
|
113
|
|
|
|
|
114
|
|
|
$result = $this->castResponseToType($response); |
|
115
|
|
|
$formatted = $this->castResponseToType($response, $this->getResponseType()); |
|
116
|
|
|
|
|
117
|
|
|
if (!isset($result['code']) || $result['code'] != 0) { |
|
118
|
|
|
$message = isset($result['message']) ? $result['message'] : ''; |
|
119
|
|
|
$code = isset($result['code']) ? $result['code'] : 0; |
|
120
|
|
|
|
|
121
|
|
|
throw new ApiException($message, $response, $formatted, $code); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $returnRaw ? $response : $this->castResponseToType($response, $this->getResponseType()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Register Guzzle middlewares. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function registerHttpMiddlewares() |
|
131
|
|
|
{ |
|
132
|
|
|
// access token |
|
133
|
|
|
$this->pushMiddleware($this->accessTokenMiddleware(), 'access_token'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Attache access token to request query. |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Closure |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function accessTokenMiddleware() |
|
142
|
|
|
{ |
|
143
|
|
|
return function ($handler) { |
|
144
|
|
|
return function ($request, $options) use ($handler) { |
|
145
|
|
|
/** @var Request $request */ |
|
146
|
|
|
$request = $request->withHeader('Access-Token', $this->getAccessToken()); |
|
147
|
|
|
|
|
148
|
|
|
return $handler($request, $options); |
|
149
|
|
|
}; |
|
150
|
|
|
}; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|