11
11
use Illuminate \Support \Str ;
12
12
use InvalidArgumentException ;
13
13
use Psr \Http \Message \ResponseInterface ;
14
+ use Psr \Http \Message \StreamInterface ;
14
15
use Spinen \ConnectWise \Exceptions \MalformedRequest ;
15
16
use Spinen \ConnectWise \Support \Collection ;
16
17
use Spinen \ConnectWise \Support \Model ;
19
20
/**
20
21
* Class Client
21
22
*
22
- *
23
23
* @method LaravelCollection|Model delete(string $resource, array $options = [])
24
24
* @method LaravelCollection|Model get(string $resource, array $options = [])
25
25
* @method LaravelCollection|Model getAll(string $resource, array $options = [])
@@ -55,17 +55,13 @@ class Client
55
55
56
56
/**
57
57
* Current page
58
- *
59
- * @var int
60
58
*/
61
- protected $ page ;
59
+ protected ? int $ page = null ;
62
60
63
61
/**
64
62
* Number of records to retrieve
65
- *
66
- * @var int
67
63
*/
68
- protected $ page_size = 100 ;
64
+ protected int $ page_size = 1000 ;
69
65
70
66
/**
71
67
* Integration password for global calls
@@ -122,9 +118,6 @@ public function __construct(
122
118
protected ModelResolver $ resolver = new ModelResolver ,
123
119
protected ?string $ version = null ,
124
120
) {
125
- // $this->token = $token;
126
- // $this->guzzle = $guzzle;
127
- // $this->resolver = $resolver;
128
121
$ this ->setVersion ($ version ?? Arr::last ($ this ->supported ));
129
122
}
130
123
@@ -154,7 +147,7 @@ public function __call($verb, $args)
154
147
throw new InvalidArgumentException (sprintf ('Unsupported verb [%s] was requested. ' , $ verb ));
155
148
}
156
149
157
- return $ this ->request ($ verb , $ this ->trimResourceAsNeeded ($ args [0 ]), $ args [1 ] ?? []);
150
+ return $ this ->request ($ verb , $ this ->trimResourceAsNeeded ($ args [0 ]), $ args [1 ] ?? [], $ args [ 2 ] ?? [] );
158
151
}
159
152
160
153
/**
@@ -192,15 +185,17 @@ public function buildAuth()
192
185
* We always need to login with Basic Auth, so add the "auth" option for Guzzle to use when logging in.
193
186
* Additionally, pass any headers that have been set.
194
187
*
195
- *
196
188
* @return array
197
189
*/
198
- public function buildOptions (array $ options = [])
190
+ public function buildOptions (array $ body = [], array $ options = [])
199
191
{
200
- return [
201
- 'body ' => empty ($ options ) ? null : json_encode ($ options ),
202
- 'headers ' => $ this ->getHeaders (),
203
- ];
192
+ return array_merge (
193
+ $ options ,
194
+ [
195
+ 'body ' => empty ($ body ) ? null : json_encode ($ body ),
196
+ 'headers ' => $ this ->getHeaders (),
197
+ ]
198
+ );
204
199
}
205
200
206
201
/**
@@ -229,6 +224,24 @@ public function buildUri($resource)
229
224
return $ uri ;
230
225
}
231
226
227
+ /**
228
+ * Download a file to path
229
+ *
230
+ * @param string $resource
231
+ * @param mixed $sink
232
+ * @param string $verb
233
+ * @param array $body
234
+ *
235
+ * @return LaravelCollection|Model|Response
236
+ *
237
+ * @throws GuzzleException
238
+ * @throws MalformedRequest
239
+ */
240
+ public function download (string $ resource , mixed $ sink , string $ verb = 'GET ' , array $ body = [])
241
+ {
242
+ return $ this ->request ($ verb , $ resource , $ body , ['sink ' => $ sink ]);
243
+ }
244
+
232
245
/**
233
246
* Remove all set headers
234
247
*
@@ -420,16 +433,22 @@ function ($item) use ($model) {
420
433
*
421
434
* @param string $method
422
435
* @param string $resource
436
+ * @param array|null $body
423
437
* @param array|null $options
438
+ * @param bool|null $raw
424
439
* @return LaravelCollection|Model|Response
425
440
*
426
441
* @throws GuzzleException
427
442
* @throws MalformedRequest
428
443
*/
429
- protected function request ($ method , $ resource , array $ options = [])
444
+ protected function request ($ method , $ resource , array $ body = [], array $ options = [], bool $ raw = false )
430
445
{
431
446
try {
432
- $ response = $ this ->guzzle ->request ($ method , $ this ->buildUri ($ resource ), $ this ->buildOptions ($ options ));
447
+ $ response = $ this ->guzzle ->request ($ method , $ this ->buildUri ($ resource ), $ this ->buildOptions ($ body , $ options ));
448
+
449
+ if ($ raw ) {
450
+ return $ response ;
451
+ }
433
452
434
453
$ processed = $ this ->processResponse ($ resource , $ response );
435
454
@@ -448,12 +467,32 @@ protected function request($method, $resource, array $options = [])
448
467
$ processed = $ processed ->merge ($ this ->processResponse ($ resource , $ response ));
449
468
}
450
469
470
+ //Reset for multiple calls
471
+ $ this ->page = null ;
472
+
451
473
return $ processed ;
452
474
} catch (RequestException $ e ) {
453
475
$ this ->processError ($ e );
454
476
}
455
477
}
456
478
479
+ /**
480
+ * Shortcut to request with raw set
481
+ *
482
+ * @param string $method
483
+ * @param string $resource
484
+ * @param array|null $body
485
+ * @param array|null $options
486
+ * @return LaravelCollection|Model|Response
487
+ *
488
+ * @throws GuzzleException
489
+ * @throws MalformedRequest
490
+ */
491
+ protected function requestRaw ($ method , $ resource , array $ body = [], array $ options = [])
492
+ {
493
+ return $ this ->request ($ method , $ resource , $ body , $ options , true );
494
+ }
495
+
457
496
/**
458
497
* Set the Client Id
459
498
*
@@ -555,6 +594,24 @@ public function setVersion($version)
555
594
return $ this ;
556
595
}
557
596
597
+ /**
598
+ * Steam a file
599
+ *
600
+ * @param string $resource
601
+ * @param string $verb
602
+ * @param array $body
603
+ *
604
+ * @return StreamInterface
605
+ *
606
+ * @throws GuzzleException
607
+ * @throws MalformedRequest
608
+ * @throws InvalidArgumentException
609
+ */
610
+ public function stream (string $ resource , string $ verb = 'GET ' , array $ body = [])
611
+ {
612
+ return $ this ->requestRaw ($ verb , $ resource , $ body , ['stream ' => true ])->getBody ();
613
+ }
614
+
558
615
/**
559
616
* Make the resource being requested be relative without the leading slash
560
617
*
0 commit comments