Skip to content

Commit 27853da

Browse files
committed
Merge branch 'release/6.2.0'
2 parents 03a80d4 + d570bbd commit 27853da

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.1.0
1+
6.2.0

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"php": "^8.0.2",
1919
"ext-json": "*",
2020
"guzzlehttp/guzzle": "^7.0",
21-
"laravel/framework": "^9.19|^10",
22-
"nesbot/carbon": "^2.62.1"
21+
"laravel/framework": "^9.19|^10|^11",
22+
"nesbot/carbon": "^2.62.1|^3"
2323
},
2424
"require-dev": {
2525
"mockery/mockery": "^1.5.1",

src/Api/Client.php

+76-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Str;
1212
use InvalidArgumentException;
1313
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\StreamInterface;
1415
use Spinen\ConnectWise\Exceptions\MalformedRequest;
1516
use Spinen\ConnectWise\Support\Collection;
1617
use Spinen\ConnectWise\Support\Model;
@@ -19,7 +20,6 @@
1920
/**
2021
* Class Client
2122
*
22-
*
2323
* @method LaravelCollection|Model delete(string $resource, array $options = [])
2424
* @method LaravelCollection|Model get(string $resource, array $options = [])
2525
* @method LaravelCollection|Model getAll(string $resource, array $options = [])
@@ -55,17 +55,13 @@ class Client
5555

5656
/**
5757
* Current page
58-
*
59-
* @var int
6058
*/
61-
protected $page;
59+
protected ?int $page = null;
6260

6361
/**
6462
* Number of records to retrieve
65-
*
66-
* @var int
6763
*/
68-
protected $page_size = 100;
64+
protected int $page_size = 1000;
6965

7066
/**
7167
* Integration password for global calls
@@ -122,9 +118,6 @@ public function __construct(
122118
protected ModelResolver $resolver = new ModelResolver,
123119
protected ?string $version = null,
124120
) {
125-
// $this->token = $token;
126-
// $this->guzzle = $guzzle;
127-
// $this->resolver = $resolver;
128121
$this->setVersion($version ?? Arr::last($this->supported));
129122
}
130123

@@ -154,7 +147,7 @@ public function __call($verb, $args)
154147
throw new InvalidArgumentException(sprintf('Unsupported verb [%s] was requested.', $verb));
155148
}
156149

157-
return $this->request($verb, $this->trimResourceAsNeeded($args[0]), $args[1] ?? []);
150+
return $this->request($verb, $this->trimResourceAsNeeded($args[0]), $args[1] ?? [], $args[2] ?? []);
158151
}
159152

160153
/**
@@ -192,15 +185,17 @@ public function buildAuth()
192185
* We always need to login with Basic Auth, so add the "auth" option for Guzzle to use when logging in.
193186
* Additionally, pass any headers that have been set.
194187
*
195-
*
196188
* @return array
197189
*/
198-
public function buildOptions(array $options = [])
190+
public function buildOptions(array $body = [], array $options = [])
199191
{
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+
);
204199
}
205200

206201
/**
@@ -229,6 +224,24 @@ public function buildUri($resource)
229224
return $uri;
230225
}
231226

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+
232245
/**
233246
* Remove all set headers
234247
*
@@ -420,16 +433,22 @@ function ($item) use ($model) {
420433
*
421434
* @param string $method
422435
* @param string $resource
436+
* @param array|null $body
423437
* @param array|null $options
438+
* @param bool|null $raw
424439
* @return LaravelCollection|Model|Response
425440
*
426441
* @throws GuzzleException
427442
* @throws MalformedRequest
428443
*/
429-
protected function request($method, $resource, array $options = [])
444+
protected function request($method, $resource, array $body = [], array $options = [], bool $raw = false)
430445
{
431446
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+
}
433452

434453
$processed = $this->processResponse($resource, $response);
435454

@@ -448,12 +467,32 @@ protected function request($method, $resource, array $options = [])
448467
$processed = $processed->merge($this->processResponse($resource, $response));
449468
}
450469

470+
//Reset for multiple calls
471+
$this->page = null;
472+
451473
return $processed;
452474
} catch (RequestException $e) {
453475
$this->processError($e);
454476
}
455477
}
456478

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+
457496
/**
458497
* Set the Client Id
459498
*
@@ -555,6 +594,24 @@ public function setVersion($version)
555594
return $this;
556595
}
557596

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+
558615
/**
559616
* Make the resource being requested be relative without the leading slash
560617
*

0 commit comments

Comments
 (0)