Skip to content

Commit

Permalink
Merge branch 'release/6.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmypuckett committed Apr 8, 2024
2 parents 03a80d4 + d570bbd commit 27853da
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
95 changes: 76 additions & 19 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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] ?? []);
}

/**
Expand Down Expand Up @@ -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(),
]
);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);

Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 27853da

Please sign in to comment.