From c667970f4bcc28d67e380a98c35de581a83aecea Mon Sep 17 00:00:00 2001 From: Jimmy Puckett Date: Mon, 29 May 2023 21:06:52 -0400 Subject: [PATCH 1/5] Allow for mulipled calls toggle getAll --- src/Api/Client.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Api/Client.php b/src/Api/Client.php index fef2a9de..0f22e7c3 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -19,7 +19,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 +54,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 +117,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)); } @@ -448,6 +440,9 @@ 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); From 942c4575ed6a7ca8efec7230eacbe358f845a228 Mon Sep 17 00:00:00 2001 From: Jimmy Puckett Date: Wed, 31 May 2023 07:45:12 -0400 Subject: [PATCH 2/5] Allow for downloading/streaming files via client --- src/Api/Client.php | 80 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/src/Api/Client.php b/src/Api/Client.php index 0f22e7c3..54a5d458 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; @@ -146,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] ?? []); } /** @@ -184,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(), + ] + ); } /** @@ -221,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 * @@ -412,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), dump($this->buildOptions($body, $options))); + + if ($raw) { + return $response; + } $processed = $this->processResponse($resource, $response); @@ -449,6 +476,23 @@ protected function request($method, $resource, array $options = []) } } + /** + * 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 * @@ -550,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 * From f1aea9418474893159b17c182e1113df5d2261a7 Mon Sep 17 00:00:00 2001 From: Jimmy Puckett Date: Sun, 11 Jun 2023 10:57:34 -0400 Subject: [PATCH 3/5] Removing debug dump --- src/Api/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/Client.php b/src/Api/Client.php index 54a5d458..abdcea85 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -444,7 +444,7 @@ function ($item) use ($model) { protected function request($method, $resource, array $body = [], array $options = [], bool $raw = false) { try { - $response = $this->guzzle->request($method, $this->buildUri($resource), dump($this->buildOptions($body, $options))); + $response = $this->guzzle->request($method, $this->buildUri($resource), $this->buildOptions($body, $options)); if ($raw) { return $response; From 94b49c325d4e5378d80897fed1f85140e899f817 Mon Sep 17 00:00:00 2001 From: Jimmy Puckett Date: Mon, 8 Apr 2024 11:50:59 -0400 Subject: [PATCH 4/5] Support L11 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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", From d570bbdefee0c8150af6979e650fc28a5e696db9 Mon Sep 17 00:00:00 2001 From: Jimmy Puckett Date: Mon, 8 Apr 2024 11:51:39 -0400 Subject: [PATCH 5/5] Bumping version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index dfda3e0b..6abaeb2f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.1.0 +6.2.0