From 5864f6f1604018541935cc0c1b6c52570a48364c Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 14:43:56 +0800 Subject: [PATCH 1/6] Allow async query parameter for creating tickets --- src/Zendesk/API/HttpClient.php | 17 +++++++++----- src/Zendesk/API/Resources/Core/Tickets.php | 8 +++++++ src/Zendesk/API/Traits/Resource/Create.php | 7 +++--- .../API/UnitTests/Core/TicketsTest.php | 22 ++++++++++++++++++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 0ab2fc54..da12ce32 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -428,6 +428,7 @@ public function getSideload(array $params = []) * @param array $queryParams * * @return \stdClass | null + * @throws \Zendesk\API\Exceptions\AuthException * @throws Exceptions\ApiResponseException */ public function get($endpoint, $queryParams = []) @@ -454,18 +455,22 @@ public function get($endpoint, $queryParams = []) * @param $endpoint * @param array $postData * - * @return \stdClass | null + * @param array $options + * @return null|\stdClass + * @throws \Zendesk\API\Exceptions\AuthException * @throws 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; diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 1e6c3cda..550b0fb5 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -145,6 +145,14 @@ public function create(array $params) $params['comment']['uploads'] = $this->lastAttachments; $this->lastAttachments = []; } + + if (isset($params['async']) && ($params['async'] == true)) { + $params['extraOptions'] = [ + 'queryParams' => [ + 'async' => true + ] + ]; + } return $this->traitCreate($params); } diff --git a/src/Zendesk/API/Traits/Resource/Create.php b/src/Zendesk/API/Traits/Resource/Create.php index c063957d..4288fd39 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(); } @@ -29,7 +29,8 @@ public function create(array $params, $routeKey = __FUNCTION__) return $this->client->post( $route, - [$this->objectName => $params] + [$this->objectName => $params], + isset($params['extraOptions']) ? $params['extraOptions'] : [] ); } } diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index 2965fd04..a0cbf5e6 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,26 @@ public function testCreateWithAttachment() 'postFields' => $postFields, ]); } + + /** + * Tests if the client can call and build the create ticket witch attachment endpoint and initiate the file upload + * headers and POST data + */ + 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 From bcdd2fe2cf89194cce97239d374239ac82875ac8 Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 15:07:51 +0800 Subject: [PATCH 2/6] Remove extra options from create trait --- src/Zendesk/API/Resources/Core/Tickets.php | 13 +++++++++++-- src/Zendesk/API/Traits/Resource/Create.php | 3 +-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 550b0fb5..4d220461 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -138,6 +138,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) { @@ -146,15 +148,22 @@ public function create(array $params) $this->lastAttachments = []; } + $extraOptions = []; if (isset($params['async']) && ($params['async'] == true)) { - $params['extraOptions'] = [ + $extraOptions = [ 'queryParams' => [ 'async' => true ] ]; } - return $this->traitCreate($params); + $route = $this->getRoute(__FUNCTION__, $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 4288fd39..4bdba6e6 100644 --- a/src/Zendesk/API/Traits/Resource/Create.php +++ b/src/Zendesk/API/Traits/Resource/Create.php @@ -29,8 +29,7 @@ public function create(array $params, $routeKey = __FUNCTION__) return $this->client->post( $route, - [$this->objectName => $params], - isset($params['extraOptions']) ? $params['extraOptions'] : [] + [$this->objectName => $params] ); } } From 09c4bce2ff9231e64ee580c65780a8167991852b Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 15:09:05 +0800 Subject: [PATCH 3/6] Add create route --- src/Zendesk/API/Resources/Core/Tickets.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 4d220461..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', From 0cae0cde5e9a6a79fc3c23cae62eefc396fdda26 Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 15:23:11 +0800 Subject: [PATCH 4/6] fixes --- src/Zendesk/API/HttpClient.php | 8 +++++--- tests/Zendesk/API/UnitTests/Core/TicketsTest.php | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index da12ce32..5b80e2e3 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -458,7 +458,7 @@ public function get($endpoint, $queryParams = []) * @param array $options * @return null|\stdClass * @throws \Zendesk\API\Exceptions\AuthException - * @throws Exceptions\ApiResponseException + * @throws \Zendesk\API\Exceptions\ApiResponseException */ public function post($endpoint, $postData = [], $options = []) { @@ -483,7 +483,8 @@ public function post($endpoint, $postData = [], $options = []) * @param array $putData * * @return \stdClass | null - * @throws Exceptions\ApiResponseException + * @throws \Zendesk\API\Exceptions\AuthException + * @throws \Zendesk\API\Exceptions\ApiResponseException */ public function put($endpoint, $putData = []) { @@ -502,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/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index a0cbf5e6..d7870f82 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -180,8 +180,7 @@ public function testCreateWithAttachment() } /** - * Tests if the client can call and build the create ticket witch attachment endpoint and initiate the file upload - * headers and POST data + * Tests that we can create the ticket with an async parameter which will add `async=true` to the query parameters */ public function testCreateAsync() { From 3f0f5ac3998925eff6ff1d2c09ecdd6226ab4f4b Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 15:24:25 +0800 Subject: [PATCH 5/6] Fix get docblock --- src/Zendesk/API/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 5b80e2e3..b4bb891a 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -429,7 +429,7 @@ public function getSideload(array $params = []) * * @return \stdClass | null * @throws \Zendesk\API\Exceptions\AuthException - * @throws Exceptions\ApiResponseException + * @throws \Zendesk\API\Exceptions\ApiResponseException */ public function get($endpoint, $queryParams = []) { From 06370c363f9667d31efd18d24f6d863fbb4aa482 Mon Sep 17 00:00:00 2001 From: Mio Miguel Galang Date: Fri, 23 Dec 2016 15:28:00 +0800 Subject: [PATCH 6/6] Remove extra whitespace --- tests/Zendesk/API/UnitTests/Core/TicketsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index d7870f82..c84bf5aa 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -180,7 +180,7 @@ public function testCreateWithAttachment() } /** - * Tests that we can create the ticket with an async parameter which will add `async=true` to the query parameters + * Tests that we can create the ticket with an async parameter which will add `async=true` to the query parameters */ public function testCreateAsync() {