Skip to content

Commit

Permalink
Merge pull request #15 from ASAPIRL/post-for-query-request
Browse files Browse the repository at this point in the history
Add support for getting data via POST
  • Loading branch information
aidan-casey authored May 24, 2023
2 parents 7a2c264 + 0edfbe5 commit d3c641c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Helpers/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function endpoints() : array
if (
str_contains($path, '{parentId}') ||
str_contains($path, 'EntityInformation') ||
str_contains($path, 'entityInformation') ||
str_contains($path, 'Modules') ||
str_contains($path, 'ThresholdInformation') ||
str_contains($path, 'Version') ||
Expand Down
21 changes: 18 additions & 3 deletions templates/Package/API/Paginator.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ class {{ entityName.singular }}Paginator
/** @var PageEntity Page data transfer object. */
protected PageEntity $page;
/** @var array Search params for POST /query request */
protected $postParams;
/**
* Sets up the paginator.
*
* @param HttpClient $client Http client for retrieving pages.
* @param Response $response Response from Http client.
* @param array $postParams Search params for POST /query request
*
* @author Aidan Casey <[email protected]>
*/
public function __construct(HttpClient $client, $response)
public function __construct(HttpClient $client, $response, $postParams = null)
{
$this->client = $client;
$this->collection = {{ entityName.singular }}Collection::fromResponse($response);
$this->page = PageEntity::fromResponse($response);
$this->postParams = $postParams;
}
/**
Expand Down Expand Up @@ -70,7 +75,12 @@ class {{ entityName.singular }}Paginator
*/
public function nextPage(): {{ entityName.singular }}Paginator
{
$response = $this->client->getClient()->get($this->page->nextPageUrl);
if(is_null($this->postParams)){
$response = $this->client->getClient()->get($this->page->nextPageUrl);
}else{
$response = $this->client->getClient()->post($this->page->nextPageUrl, ['json' => $this->postParams]);
}
return new static($this->client, $response);
}
Expand All @@ -81,7 +91,12 @@ class {{ entityName.singular }}Paginator
*/
public function prevPage (): {{ entityName.singular }}Paginator
{
$response = $this->client->getClient()->get($this->page->prevPageUrl);
if(is_null($this->postParams)){
$response = $this->client->getClient()->get($this->page->prevPageUrl);
}else{
$response = $this->client->getClient()->post($this->page->prevPageUrl, ['json' => $this->postParams]);
}
return new static($this->client, $response);
}
}
36 changes: 25 additions & 11 deletions templates/Package/API/QueryBuilder.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class {{ entityName.singular }}QueryBuilder
/** @var int The maximum number of records to be returned. */
protected int $records;
private const GET_LIMIT = 1800;
/**
* Sets up the class to perform a query.
*
Expand All @@ -42,9 +44,13 @@ class {{ entityName.singular }}QueryBuilder
*/
public function count(): int
{
$response = $this->client->get("{{ entityName.plural }}/query/count", [
'search' => json_encode( $this->toArray() )
]);
if (strlen($this->__toString()) >= self::GET_LIMIT) {
$response = $this->client->post("{{ entityName.plural }}/query/count", $this->toArray());
}else{
$response = $this->client->get("{{ entityName.plural }}/query/count", [
'search' => json_encode( $this->toArray() )
]);
}
$responseArray = json_decode($response->getBody(), true);
Expand Down Expand Up @@ -84,9 +90,13 @@ class {{ entityName.singular }}QueryBuilder
*/
public function get(): {{ entityName.singular }}Collection
{
$response = $this->client->get("{{ entityName.plural }}/query", [
'search' => json_encode( $this->toArray() )
]);
if (strlen($this->__toString()) >= self::GET_LIMIT) {
$response = $this->client->post("{{ entityName.plural }}/query", $this->toArray());
}else{
$response = $this->client->get("{{ entityName.plural }}/query", [
'search' => json_encode( $this->toArray() )
]);
}
return {{ entityName.singular }}Collection::fromResponse($response);
}
Expand All @@ -96,11 +106,15 @@ class {{ entityName.singular }}QueryBuilder
*/
public function paginate(): {{ entityName.singular }}Paginator
{
$response = $this->client->get("{{ entityName.plural }}/query", [
'search' => json_encode($this->toArray())
]);
return new {{ entityName.singular }}Paginator($this->client, $response);
if (strlen($this->__toString()) >= self::GET_LIMIT) {
$response = $this->client->post("{{ entityName.plural }}/query", $this->toArray());
return new {{ entityName.singular }}Paginator($this->client, $response, $this->toArray());
}else{
$response = $this->client->get("{{ entityName.plural }}/query", [
'search' => json_encode( $this->toArray() )
]);
return new {{ entityName.singular }}Paginator($this->client, $response);
}
}
/**
Expand Down
1 change: 0 additions & 1 deletion templates/Package/API/Service.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class {{ entityName.singular }}Service
{
/** @var Client An HTTP client for making requests to the Autotask API. */
protected HttpClient $client;
/**
* Instantiates the class.
*
Expand Down

0 comments on commit d3c641c

Please sign in to comment.