1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2017 Salah Alkhwlani <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Yemenifree\PickServices; |
11
|
|
|
|
12
|
|
|
use Zttp\Zttp; |
13
|
|
|
use Zttp\ZttpResponse; |
14
|
|
|
|
15
|
|
|
class ApiClient |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $apiEndPoint = 'https://api.pick.sa'; |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $apiSandboxEndPoint = 'https://sandbox.pick.sa'; |
21
|
|
|
/** @var array */ |
22
|
|
|
protected $headers = []; |
23
|
|
|
/** @var bool */ |
24
|
|
|
protected $sandbox = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set token for auth services. |
28
|
|
|
* |
29
|
|
|
* @param $token |
30
|
|
|
* |
31
|
|
|
* @return ApiClient |
32
|
|
|
*/ |
33
|
|
|
public function setToken($token): self |
34
|
|
|
{ |
35
|
|
|
$this->headers['X-Api-Key'] = $token; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Process post request. |
42
|
|
|
* |
43
|
|
|
* @param string $endPoint |
44
|
|
|
* @param array $params |
45
|
|
|
* |
46
|
|
|
* @return ZttpResponse |
47
|
|
|
*/ |
48
|
|
|
public function post(string $endPoint, array $params = []): ZttpResponse |
49
|
|
|
{ |
50
|
|
|
return $this->request('post', $endPoint, $params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* process http request. |
55
|
|
|
* |
56
|
|
|
* @param string $method |
57
|
|
|
* @param string $endPoint |
58
|
|
|
* @param array $params |
59
|
|
|
* @param bool $asJson |
60
|
|
|
* |
61
|
|
|
* @return ZttpResponse |
62
|
|
|
*/ |
63
|
|
|
protected function request(string $method, string $endPoint, array $params = [], $asJson = true): ZttpResponse |
64
|
|
|
{ |
65
|
|
|
$request = Zttp::withHeaders($this->getHeaders()) |
66
|
|
|
->withoutRedirecting(); |
67
|
|
|
|
68
|
|
|
// if json except. |
69
|
|
|
if ($asJson === true) { |
70
|
|
|
$request->asJson(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $request->{$method}($this->buildUrl($endPoint), $params); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get Headers request. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getHeaders(): array |
82
|
|
|
{ |
83
|
|
|
return $this->headers; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set headers of http request. |
88
|
|
|
* |
89
|
|
|
* @param array $headers |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setHeaders(array $headers): self |
94
|
|
|
{ |
95
|
|
|
$this->headers = $headers; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Build full api url. |
102
|
|
|
* |
103
|
|
|
* @param $endPoint |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function buildUrl($endPoint): string |
108
|
|
|
{ |
109
|
|
|
return $this->getEndPoint() . '/' . $endPoint; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get endpoint url. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getEndPoint(): string |
118
|
|
|
{ |
119
|
|
|
return $this->sandbox ? $this->apiSandboxEndPoint : $this->apiEndPoint; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Process get request. |
124
|
|
|
* |
125
|
|
|
* @param string $endPoint |
126
|
|
|
* @param array $params |
127
|
|
|
* |
128
|
|
|
* @return ZttpResponse |
129
|
|
|
*/ |
130
|
|
|
public function get(string $endPoint, array $params = []): ZttpResponse |
131
|
|
|
{ |
132
|
|
|
return $this->request('get', $endPoint, $params, false); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Process delete request. |
137
|
|
|
* |
138
|
|
|
* @param string $endPoint |
139
|
|
|
* @param array $params |
140
|
|
|
* |
141
|
|
|
* @return ZttpResponse |
142
|
|
|
*/ |
143
|
|
|
public function delete(string $endPoint, array $params = []): ZttpResponse |
144
|
|
|
{ |
145
|
|
|
return $this->request('delete', $endPoint, $params); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Process put request. |
150
|
|
|
* |
151
|
|
|
* @param string $endPoint |
152
|
|
|
* @param array $params |
153
|
|
|
* |
154
|
|
|
* @return ZttpResponse |
155
|
|
|
*/ |
156
|
|
|
public function put(string $endPoint, array $params): ZttpResponse |
157
|
|
|
{ |
158
|
|
|
return $this->request('put', $endPoint, $params); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Process patch request. |
163
|
|
|
* |
164
|
|
|
* @param string $endPoint |
165
|
|
|
* @param array $params |
166
|
|
|
* |
167
|
|
|
* @return ZttpResponse |
168
|
|
|
*/ |
169
|
|
|
public function patch(string $endPoint, array $params): ZttpResponse |
170
|
|
|
{ |
171
|
|
|
return $this->request('patch', $endPoint, $params); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Enable sandbox. |
176
|
|
|
* |
177
|
|
|
* @param bool $sandbox |
178
|
|
|
* |
179
|
|
|
* @return ApiClient |
180
|
|
|
*/ |
181
|
|
|
public function setSandbox(bool $sandbox): ApiClient |
182
|
|
|
{ |
183
|
|
|
$this->sandbox = $sandbox; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|