-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathODataClient.php
423 lines (375 loc) · 9.39 KB
/
ODataClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
namespace SaintSystems\OData;
use Closure;
use SaintSystems\OData\Exception\ODataException;
use SaintSystems\OData\Query\Builder;
use SaintSystems\OData\Query\Grammar;
use SaintSystems\OData\Query\IGrammar;
use SaintSystems\OData\Query\IProcessor;
use SaintSystems\OData\Query\Processor;
use Illuminate\Support\LazyCollection;
class ODataClient implements IODataClient
{
/**
* The base service URL. For example, "https://services.odata.org/V4/TripPinService/"
* @var string
*/
private $baseUrl;
/**
* The IAuthenticationProvider for authenticating request messages.
* @var IAuthenticationProvider
*/
private $authenticationProvider;
/**
* The IHttpProvider for sending HTTP requests.
* @var IHttpProvider
*/
private $httpProvider;
/**
* The query grammar implementation.
*
* @var IGrammar
*/
protected $queryGrammar;
/**
* The query post processor implementation.
*
* @var IProcessor
*/
protected $postProcessor;
/**
* The return type for the entities
*
* @var string
*/
private $entityReturnType;
/**
* The page size
*
* @var int
*/
private $pageSize;
/**
* The entityKey to be found
*
* @var mixed
*/
private $entityKey;
/**
* Constructs a new ODataClient.
* @param string $baseUrl The base service URL.
* @param IAuthenticationProvider $authenticationProvider The IAuthenticationProvider for authenticating request messages.
* @param IHttpProvider|null $httpProvider The IHttpProvider for sending requests.
*/
public function __construct(
$baseUrl,
Callable $authenticationProvider = null,
IHttpProvider $httpProvider = null
) {
$this->setBaseUrl($baseUrl);
$this->authenticationProvider = $authenticationProvider;
$this->httpProvider = $httpProvider ?: new GuzzleHttpProvider();
// We need to initialize a query grammar and the query post processors
// which are both very important parts of the OData abstractions
// so we initialize these to their default values while starting.
$this->useDefaultQueryGrammar();
$this->useDefaultPostProcessor();
}
/**
* Set the query grammar to the default implementation.
*
* @return void
*/
public function useDefaultQueryGrammar()
{
$this->queryGrammar = $this->getDefaultQueryGrammar();
}
/**
* Get the default query grammar instance.
*
* @return IGrammar
*/
protected function getDefaultQueryGrammar()
{
return new Grammar;
}
/**
* Set the query post processor to the default implementation.
*
* @return void
*/
public function useDefaultPostProcessor()
{
$this->postProcessor = $this->getDefaultPostProcessor();
}
/**
* Get the default post processor instance.
*
* @return IProcessor
*/
protected function getDefaultPostProcessor()
{
return new Processor();
}
/**
* Gets the IAuthenticationProvider for authenticating requests.
*
* @return Closure|IAuthenticationProvider
*/
public function getAuthenticationProvider()
{
return $this->authenticationProvider;
}
/**
* Gets the base URL for requests of the client.
*
* @return string
*/
public function getBaseUrl()
{
return $this->baseUrl;
}
/**
* Sets the base URL for requests of the client.
* @param mixed $value
*
* @throws ODataException
*/
public function setBaseUrl($value)
{
if (empty($value)) {
throw new ODataException(Constants::BASE_URL_MISSING);
}
$this->baseUrl = rtrim($value, '/') . '/';
}
/**
* Gets the IHttpProvider for sending HTTP requests.
*
* @return IHttpProvider
*/
public function getHttpProvider()
{
return $this->httpProvider;
}
/**
* Begin a fluent query against an odata service
*
* @param string $entitySet
*
* @return Builder
*/
public function from($entitySet)
{
return $this->query()->from($entitySet);
}
/**
* Begin a fluent query against an odata service
*
* @param array $properties
*
* @return Builder
*/
public function select($properties = [])
{
$properties = is_array($properties) ? $properties : func_get_args();
return $this->query()->select($properties);
}
/**
* Get a new query builder instance.
*
* @return Builder
*/
public function query()
{
return new Builder(
$this, $this->getQueryGrammar(), $this->getPostProcessor()
);
}
/**
* Run a GET HTTP request against the service.
*
* @param string $requestUri
* @param array $bindings
*
* @return IODataRequest
*/
public function get($requestUri, $bindings = [])
{
list($response, $nextPage) = $this->getNextPage($requestUri, $bindings);
return $response;
}
/**
* Run a GET HTTP request against the service.
*
* @param string $requestUri
* @param array $bindings
*
* @return IODataRequest
*/
public function getNextPage($requestUri, $bindings = [])
{
return $this->request(HttpMethod::GET, $requestUri, $bindings);
}
/**
* Run a GET HTTP request against the service and return a generator.
*
* @param string $requestUri
* @param array $bindings
*
* @return \Illuminate\Support\LazyCollection
*/
public function cursor($requestUri, $bindings = [])
{
return LazyCollection::make(function() use($requestUri, $bindings) {
$nextPage = $requestUri;
while (!is_null($nextPage)) {
list($data, $nextPage) = $this->getNextPage($nextPage, $bindings);
if (!is_null($nextPage)) {
$nextPage = str_replace($this->baseUrl, '', $nextPage);
}
yield from $data;
}
});
}
/**
* Run a POST request against the service.
*
* @param string $requestUri
* @param mixed $postData
*
* @return IODataRequest
*/
public function post($requestUri, $postData)
{
return $this->request(HttpMethod::POST, $requestUri, $postData);
}
/**
* Run a PATCH request against the service.
*
* @param string $requestUri
* @param mixed $body
*
* @return IODataRequest
*/
public function patch($requestUri, $body)
{
return $this->request(HttpMethod::PATCH, $requestUri, $body);
}
/**
* Run a DELETE request against the service.
*
* @param string $requestUri
*
* @return IODataRequest
*/
public function delete($requestUri)
{
return $this->request(HttpMethod::DELETE, $requestUri);
}
/**
* Return an ODataRequest
*
* @param string $method
* @param string $requestUri
* @param mixed $body
*
* @return IODataRequest
*
* @throws ODataException
*/
public function request($method, $requestUri, $body = null)
{
$request = new ODataRequest($method, $this->baseUrl.$requestUri, $this, $this->entityReturnType);
if ($body) {
$request->attachBody($body);
}
return $request->execute();
}
/**
* Get the query grammar used by the connection.
*
* @return IGrammar
*/
public function getQueryGrammar()
{
return $this->queryGrammar;
}
/**
* Set the query grammar used by the connection.
*
* @param IGrammar $grammar
*
* @return void
*/
public function setQueryGrammar(IGrammar $grammar)
{
$this->queryGrammar = $grammar;
}
/**
* Get the query post processor used by the connection.
*
* @return IProcessor
*/
public function getPostProcessor()
{
return $this->postProcessor;
}
/**
* Set the query post processor used by the connection.
*
* @param IProcessor $processor
*
* @return void
*/
public function setPostProcessor(IProcessor $processor)
{
$this->postProcessor = $processor;
}
/**
* Set the entity return type
*
* @param string $entityReturnType
*/
public function setEntityReturnType($entityReturnType)
{
$this->entityReturnType = $entityReturnType;
}
/**
* Set the odata.maxpagesize value of the request.
*
* @param int $pageSize
*
* @return IODataClient
*/
public function setPageSize($pageSize) {
$this->pageSize = $pageSize;
return $this;
}
/**
* Gets the page size
*
* @return int
*/
public function getPageSize() {
return $this->pageSize;
}
/**
* Set the entityKey to be found.
*
* @param mixed $entityKey
*
* @return IODataClient
*/
public function setEntityKey($entityKey) {
$this->entityKey = $entityKey;
return $this;
}
/**
* Gets the entity key
*
* @return mixed
*/
public function getEntityKey() {
return $this->entityKey;
}
}