diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 0ab2fc54..b4bb891a 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -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 = []) { @@ -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; @@ -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 = []) { @@ -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) { diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 1e6c3cda..932b2f9d 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -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', @@ -138,6 +139,8 @@ 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) { @@ -145,8 +148,23 @@ public function create(array $params) $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 + ); } /** diff --git a/src/Zendesk/API/Traits/Resource/Create.php b/src/Zendesk/API/Traits/Resource/Create.php index c063957d..4bdba6e6 100644 --- a/src/Zendesk/API/Traits/Resource/Create.php +++ b/src/Zendesk/API/Traits/Resource/Create.php @@ -9,7 +9,7 @@ trait Create /** * Create a new resource * - * @param array $params + * @param array $params * * @param string $routeKey * @return null|\stdClass @@ -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(); } diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index 2965fd04..c84bf5aa 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -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']])), ]); @@ -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