Skip to content

Commit

Permalink
Merge pull request #302 from zendesk/mio/tickets-async
Browse files Browse the repository at this point in the history
Allow async query parameter for creating tickets
  • Loading branch information
miogalang authored Dec 23, 2016
2 parents 68ef2bf + 06370c3 commit c0edb4c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
27 changes: 17 additions & 10 deletions src/Zendesk/API/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ public function getSideload(array $params = [])
* @param array $queryParams
*
* @return \stdClass | null
* @throws Exceptions\ApiResponseException
* @throws \Zendesk\API\Exceptions\AuthException
* @throws \Zendesk\API\Exceptions\ApiResponseException
*/
public function get($endpoint, $queryParams = [])
{
Expand All @@ -454,18 +455,22 @@ public function get($endpoint, $queryParams = [])
* @param $endpoint
* @param array $postData
*
* @return \stdClass | null
* @throws Exceptions\ApiResponseException
* @param array $options
* @return null|\stdClass
* @throws \Zendesk\API\Exceptions\AuthException
* @throws \Zendesk\API\Exceptions\ApiResponseException
*/
public function post($endpoint, $postData = [])
public function post($endpoint, $postData = [], $options = [])
{
$extraOptions = array_merge($options, [
'postFields' => $postData,
'method' => 'POST'
]);

$response = Http::send(
$this,
$endpoint,
[
'postFields' => $postData,
'method' => 'POST'
]
$extraOptions
);

return $response;
Expand All @@ -478,7 +483,8 @@ public function post($endpoint, $postData = [])
* @param array $putData
*
* @return \stdClass | null
* @throws Exceptions\ApiResponseException
* @throws \Zendesk\API\Exceptions\AuthException
* @throws \Zendesk\API\Exceptions\ApiResponseException
*/
public function put($endpoint, $putData = [])
{
Expand All @@ -497,7 +503,8 @@ public function put($endpoint, $putData = [])
* @param $endpoint
*
* @return null
* @throws Exceptions\ApiResponseException
* @throws \Zendesk\API\Exceptions\AuthException
* @throws \Zendesk\API\Exceptions\ApiResponseException
*/
public function delete($endpoint)
{
Expand Down
20 changes: 19 additions & 1 deletion src/Zendesk/API/Resources/Core/Tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected function setUpRoutes()
parent::setUpRoutes();

$this->setRoutes([
'create' => 'tickets.json',
'findMany' => 'tickets/show_many.json',
'updateMany' => 'tickets/update_many.json',
'markAsSpam' => 'tickets/{id}/mark_as_spam.json',
Expand Down Expand Up @@ -138,15 +139,32 @@ public function findTwicket(array $params = [])
* @throws ResponseException
* @throws \Exception
* @return \stdClass | null
* @throws \Zendesk\API\Exceptions\AuthException
* @throws \Zendesk\API\Exceptions\ApiResponseException
*/
public function create(array $params)
{
if (count($this->lastAttachments)) {
$params['comment']['uploads'] = $this->lastAttachments;
$this->lastAttachments = [];
}

$extraOptions = [];
if (isset($params['async']) && ($params['async'] == true)) {
$extraOptions = [
'queryParams' => [
'async' => true
]
];
}

$route = $this->getRoute(__FUNCTION__, $params);

return $this->traitCreate($params);
return $this->client->post(
$route,
[$this->objectName => $params],
$extraOptions
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Zendesk/API/Traits/Resource/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Create
/**
* Create a new resource
*
* @param array $params
* @param array $params
*
* @param string $routeKey
* @return null|\stdClass
Expand All @@ -19,7 +19,7 @@ public function create(array $params, $routeKey = __FUNCTION__)
try {
$route = $this->getRoute($routeKey, $params);
} catch (RouteException $e) {
if (! isset($this->resourceName)) {
if (!isset($this->resourceName)) {
$this->resourceName = $this->getResourceNameFromClass();
}

Expand Down
21 changes: 20 additions & 1 deletion tests/Zendesk/API/UnitTests/Core/TicketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testDeleteMultiple()
*/
public function testCreateWithAttachment()
{
$this->mockAPIResponses([
$this->mockApiResponses([
new Response(200, [], json_encode(['upload' => ['token' => 'asdf']])),
new Response(200, [], json_encode(['ticket' => ['id' => '123']])),
]);
Expand Down Expand Up @@ -178,6 +178,25 @@ public function testCreateWithAttachment()
'postFields' => $postFields,
]);
}

/**
* Tests that we can create the ticket with an async parameter which will add `async=true` to the query parameters
*/
public function testCreateAsync()
{
$this->mockApiResponses([
new Response(200, [], json_encode(['ticket' => ['id' => '123']])),
]);

$this->testTicket['async'] = true;
$this->client->tickets()->create($this->testTicket);

$this->assertLastRequestIs([
'method' => 'POST',
'endpoint' => 'tickets.json',
'queryParams' => ['async' => true],
]);
}

/**
* Tests if the client can call and build the export tickets endpoint with the proper pagination query
Expand Down

0 comments on commit c0edb4c

Please sign in to comment.