diff --git a/VERSION b/VERSION index dfda3e0b..6abaeb2f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.1.0 +6.2.0 diff --git a/composer.json b/composer.json index 6487a88a..3a06db61 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "php": "^8.0.2", "ext-json": "*", "guzzlehttp/guzzle": "^7.0", - "laravel/framework": "^9.19|^10", - "nesbot/carbon": "^2.62.1" + "laravel/framework": "^9.19|^10|^11", + "nesbot/carbon": "^2.62.1|^3" }, "require-dev": { "mockery/mockery": "^1.5.1", diff --git a/src/Api/Client.php b/src/Api/Client.php index fef2a9de..abdcea85 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -11,6 +11,7 @@ use Illuminate\Support\Str; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use Spinen\ConnectWise\Exceptions\MalformedRequest; use Spinen\ConnectWise\Support\Collection; use Spinen\ConnectWise\Support\Model; @@ -19,7 +20,6 @@ /** * Class Client * - * * @method LaravelCollection|Model delete(string $resource, array $options = []) * @method LaravelCollection|Model get(string $resource, array $options = []) * @method LaravelCollection|Model getAll(string $resource, array $options = []) @@ -55,17 +55,13 @@ class Client /** * Current page - * - * @var int */ - protected $page; + protected ?int $page = null; /** * Number of records to retrieve - * - * @var int */ - protected $page_size = 100; + protected int $page_size = 1000; /** * Integration password for global calls @@ -122,9 +118,6 @@ public function __construct( protected ModelResolver $resolver = new ModelResolver, protected ?string $version = null, ) { - // $this->token = $token; - // $this->guzzle = $guzzle; - // $this->resolver = $resolver; $this->setVersion($version ?? Arr::last($this->supported)); } @@ -154,7 +147,7 @@ public function __call($verb, $args) throw new InvalidArgumentException(sprintf('Unsupported verb [%s] was requested.', $verb)); } - return $this->request($verb, $this->trimResourceAsNeeded($args[0]), $args[1] ?? []); + return $this->request($verb, $this->trimResourceAsNeeded($args[0]), $args[1] ?? [], $args[2] ?? []); } /** @@ -192,15 +185,17 @@ public function buildAuth() * We always need to login with Basic Auth, so add the "auth" option for Guzzle to use when logging in. * Additionally, pass any headers that have been set. * - * * @return array */ - public function buildOptions(array $options = []) + public function buildOptions(array $body = [], array $options = []) { - return [ - 'body' => empty($options) ? null : json_encode($options), - 'headers' => $this->getHeaders(), - ]; + return array_merge( + $options, + [ + 'body' => empty($body) ? null : json_encode($body), + 'headers' => $this->getHeaders(), + ] + ); } /** @@ -229,6 +224,24 @@ public function buildUri($resource) return $uri; } + /** + * Download a file to path + * + * @param string $resource + * @param mixed $sink + * @param string $verb + * @param array $body + * + * @return LaravelCollection|Model|Response + * + * @throws GuzzleException + * @throws MalformedRequest + */ + public function download(string $resource, mixed $sink, string $verb = 'GET', array $body = []) + { + return $this->request($verb, $resource, $body, ['sink' => $sink]); + } + /** * Remove all set headers * @@ -420,16 +433,22 @@ function ($item) use ($model) { * * @param string $method * @param string $resource + * @param array|null $body * @param array|null $options + * @param bool|null $raw * @return LaravelCollection|Model|Response * * @throws GuzzleException * @throws MalformedRequest */ - protected function request($method, $resource, array $options = []) + protected function request($method, $resource, array $body = [], array $options = [], bool $raw = false) { try { - $response = $this->guzzle->request($method, $this->buildUri($resource), $this->buildOptions($options)); + $response = $this->guzzle->request($method, $this->buildUri($resource), $this->buildOptions($body, $options)); + + if ($raw) { + return $response; + } $processed = $this->processResponse($resource, $response); @@ -448,12 +467,32 @@ protected function request($method, $resource, array $options = []) $processed = $processed->merge($this->processResponse($resource, $response)); } + //Reset for multiple calls + $this->page = null; + return $processed; } catch (RequestException $e) { $this->processError($e); } } + /** + * Shortcut to request with raw set + * + * @param string $method + * @param string $resource + * @param array|null $body + * @param array|null $options + * @return LaravelCollection|Model|Response + * + * @throws GuzzleException + * @throws MalformedRequest + */ + protected function requestRaw($method, $resource, array $body = [], array $options = []) + { + return $this->request($method, $resource, $body, $options, true); + } + /** * Set the Client Id * @@ -555,6 +594,24 @@ public function setVersion($version) return $this; } + /** + * Steam a file + * + * @param string $resource + * @param string $verb + * @param array $body + * + * @return StreamInterface + * + * @throws GuzzleException + * @throws MalformedRequest + * @throws InvalidArgumentException + */ + public function stream(string $resource, string $verb = 'GET', array $body = []) + { + return $this->requestRaw($verb, $resource, $body, ['stream' => true])->getBody(); + } + /** * Make the resource being requested be relative without the leading slash *