1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kobas\APIClient; |
4
|
|
|
|
5
|
|
|
use Kobas\APIClient\Auth\Provider; |
6
|
|
|
use Kobas\APIClient\Exception\CurlException; |
7
|
|
|
use Kobas\APIClient\Exception\HttpException; |
8
|
|
|
use Kobas\APIClient\Request\Curl; |
9
|
|
|
use Kobas\APIClient\Request\HttpRequest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Client |
13
|
|
|
* |
14
|
|
|
* @package Kobas |
15
|
|
|
*/ |
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $api_base_url = 'https://api.kobas.co.uk'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $version = 'v3'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Provider |
30
|
|
|
*/ |
31
|
|
|
protected $oAuthProvider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Curl|HttpRequest|null |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $headers; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $curl_options; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Client constructor. |
51
|
|
|
* |
52
|
|
|
* @param Provider $oAuthProvider |
53
|
|
|
* @param HttpRequest|null $request |
54
|
|
|
* @param array $headers |
55
|
|
|
* @param array $curl_options |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
Provider $oAuthProvider, |
59
|
|
|
HttpRequest $request = null, |
60
|
|
|
$headers = array(), |
61
|
|
|
$curl_options = array() |
62
|
|
|
) { |
63
|
|
|
$this->oAuthProvider = $oAuthProvider; |
64
|
|
|
if ($request == null) { |
65
|
|
|
$request = new Curl(); |
66
|
|
|
} |
67
|
|
|
$this->request = $request; |
68
|
|
|
$this->headers = $headers; |
69
|
|
|
$this->curl_options = $curl_options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getApiBaseUrl() |
76
|
|
|
{ |
77
|
|
|
return $this->api_base_url; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getVersion() |
84
|
|
|
{ |
85
|
|
|
return $this->version; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Allows you to set the API Base URL. Default value is 'https://api.kobas.co.uk' |
90
|
|
|
* |
91
|
|
|
* @param $url |
92
|
|
|
*/ |
93
|
|
|
public function setAPIBaseURL($url) |
94
|
|
|
{ |
95
|
|
|
$this->api_base_url = $url; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Allows you to set the API version to use. Default value is 'v2' |
100
|
|
|
* |
101
|
|
|
* @param $version |
102
|
|
|
*/ |
103
|
|
|
public function setAPIVersion($version) |
104
|
|
|
{ |
105
|
|
|
$this->version = $version; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $route |
110
|
|
|
* @param array $params |
111
|
|
|
* @param array $headers |
112
|
|
|
* @return mixed |
113
|
|
|
* @throws HttpException |
114
|
|
|
* @throws CurlException |
115
|
|
|
*/ |
116
|
|
|
public function get($route, array $params = array(), array $headers = array()) |
117
|
|
|
{ |
118
|
|
|
return $this->call('GET', $route, $params, $headers); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $http_method |
123
|
|
|
* @param $route |
124
|
|
|
* @param array $params |
125
|
|
|
* @param array $headers |
126
|
|
|
* @return mixed |
127
|
|
|
* @throws HttpException |
128
|
|
|
* @throws CurlException |
129
|
|
|
*/ |
130
|
|
|
protected function call($http_method, $route, array $params = array(), array $headers = array()) |
131
|
|
|
{ |
132
|
|
|
$url = $this->api_base_url . '/'; |
133
|
|
|
if (!empty($this->version)) { |
134
|
|
|
$url .= $this->version . '/'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$url .= $route; |
138
|
|
|
|
139
|
|
|
$this->request |
140
|
|
|
->init() |
|
|
|
|
141
|
|
|
->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($http_method)) |
142
|
|
|
->setOption(CURLOPT_RETURNTRANSFER, true) |
143
|
|
|
->setOption(CURLOPT_FOLLOWLOCATION, true) |
144
|
|
|
->setOption(CURLOPT_ENCODING, ''); |
145
|
|
|
|
146
|
|
|
foreach ($this->curl_options as $option => $value) { |
147
|
|
|
$this->request->setOption($option, $value); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$headers = array_merge($this->headers, $headers); |
151
|
|
|
$headers['Authorization'] = "Bearer " . $this->oAuthProvider->getAccessToken(); |
152
|
|
|
$headers['x-kobas-company-id'] = $this->oAuthProvider->getCompanyId(); |
153
|
|
|
$headers['Content-Type'] = 'application/json'; |
154
|
|
|
|
155
|
|
|
switch ($http_method) { |
156
|
|
|
case 'POST': |
157
|
|
|
$this->request->setOption(CURLOPT_POSTFIELDS, json_encode($params)); |
158
|
|
|
break; |
159
|
|
|
case 'DELETE': |
160
|
|
|
case 'PUT': |
161
|
|
|
$this->request->setOption(CURLOPT_POSTFIELDS, json_encode($params)); |
162
|
|
|
break; |
163
|
|
|
case 'GET': |
164
|
|
|
if (count($params)) { |
165
|
|
|
$url .= "?" . http_build_query($params); |
166
|
|
|
} |
167
|
|
|
break; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->request->setUrl($url); |
171
|
|
|
|
172
|
|
|
$requestHeaders = array(); |
173
|
|
|
foreach ($headers as $key => $value) { |
174
|
|
|
$requestHeaders[] = $key . ': ' . $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->request->setOption(CURLOPT_HTTPHEADER, $requestHeaders); |
178
|
|
|
|
179
|
|
|
$result = $this->request->execute(); |
180
|
|
|
|
181
|
|
|
if ($this->request->getErrorNumber()) { |
182
|
|
|
throw new CurlException($this->request->getErrorMessage(), $this->request->getErrorNumber()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$last_response = $this->request->getInfo(CURLINFO_HTTP_CODE); |
186
|
|
|
|
187
|
|
|
$this->request->close(); |
188
|
|
|
|
189
|
|
|
if ($last_response >= 400) { |
190
|
|
|
throw new HttpException(json_encode(json_decode($result, true), true), $last_response); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return json_decode($result, true); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param $route |
198
|
|
|
* @param array $params |
199
|
|
|
* @param array $headers |
200
|
|
|
* @return mixed |
201
|
|
|
* @throws HttpException |
202
|
|
|
* @throws CurlException |
203
|
|
|
*/ |
204
|
|
|
public function post($route, array $params = array(), array $headers = array()) |
205
|
|
|
{ |
206
|
|
|
return $this->call('POST', $route, $params, $headers); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $route |
211
|
|
|
* @param array $params |
212
|
|
|
* @param array $headers |
213
|
|
|
* @return mixed |
214
|
|
|
* @throws HttpException |
215
|
|
|
* @throws CurlException |
216
|
|
|
*/ |
217
|
|
|
public function put($route, array $params = array(), array $headers = array()) |
218
|
|
|
{ |
219
|
|
|
return $this->call('PUT', $route, $params, $headers); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param $route |
224
|
|
|
* @param array $params |
225
|
|
|
* @param array $headers |
226
|
|
|
* @return mixed |
227
|
|
|
* @throws HttpException |
228
|
|
|
* @throws CurlException |
229
|
|
|
*/ |
230
|
|
|
public function delete($route, array $params = array(), array $headers = array()) |
231
|
|
|
{ |
232
|
|
|
return $this->call('DELETE', $route, $params, $headers); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
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.