diff --git a/src/Suite/Api/Client.php b/src/Suite/Api/Client.php index 28b290a..78833be 100644 --- a/src/Suite/Api/Client.php +++ b/src/Suite/Api/Client.php @@ -9,7 +9,6 @@ use GuzzleHttp\Handler\CurlHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; -use GuzzleHttp\TransferStats; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Suite\Api\Middleware\Retry; @@ -77,7 +76,7 @@ public function get(string $url, array $parameters = array()) { $method = 'GET'; $requestBody = ''; - $fullUrl = $this->buildUrlWithParameters($url, $parameters); + $fullUrl = QueryStringAppender::appendParamsToUrl($url, $parameters); $headers = $this->getHeaders($fullUrl, $method, $requestBody); return $this->executeRequest($this->createRequest($method, $fullUrl, $headers, $requestBody)); } @@ -157,21 +156,4 @@ private function createRequest($method, $url, $headers, $requestBody) { return $this->requestFactory->createRequest($method, $url, $headers, $requestBody); } - - /** - * @param string $url - * @param array $data - * @return string - */ - private function buildUrlWithParameters(string $url, array $data): string - { - if (!empty($data)) - { - return $url . '?' . http_build_query($data); - } - else - { - return $url; - } - } } diff --git a/src/Suite/Api/ContactList.php b/src/Suite/Api/ContactList.php index 56175fd..e4dc6e9 100644 --- a/src/Suite/Api/ContactList.php +++ b/src/Suite/Api/ContactList.php @@ -119,10 +119,10 @@ public function getContactsOfList(int $customerId, int $contactListId, int $limi } } - public function getContactIdsInList(int $customerId, int $contactListId, int $limit = null, string $skipToken = null) + public function getContactIdsInList(int $customerId, int $contactListId, int $top = null, string $skiptoken = null): array { try { - $response = $this->apiClient->get($this->endPoints->contactIdsInList($customerId, $contactListId, $limit, $skipToken)); + $response = $this->apiClient->get($this->endPoints->contactIdsInList($customerId, $contactListId, $top, $skiptoken)); return $response['data'] ?? []; } catch (Error $error) { throw new RequestFailed('Could not fetch contact ids: ' . $error->getMessage(), $error->getCode(), $error); @@ -131,7 +131,7 @@ public function getContactIdsInList(int $customerId, int $contactListId, int $li public function getListChunkIterator(int $customerId, int $contactListId, int $chunkSize = 10000) : iterable { - $next = $this->endPoints->contactIdsInList($customerId, $contactListId, $chunkSize, 'first batch'); + $next = $this->endPoints->contactIdsInList($customerId, $contactListId, $chunkSize); try { do { ['value' => $value, 'next' => $next] = $this->apiClient->get($next)['data']; diff --git a/src/Suite/Api/ContactListEndPoints.php b/src/Suite/Api/ContactListEndPoints.php index 936fd2a..1eebffc 100644 --- a/src/Suite/Api/ContactListEndPoints.php +++ b/src/Suite/Api/ContactListEndPoints.php @@ -44,13 +44,15 @@ public function contactsOfList(int $customerId, int $contactListId, int $limit, return $this->baseUrl($customerId) . "/{$contactListId}/contacts/?limit={$limit}&offset={$offset}"; } - public function contactIdsInList(int $customerId, int $contactListId, int $limit = null, string $skipToken = null) - { - $result = $this->baseUrl($customerId) . "/{$contactListId}/contactIds"; - if (null !== $limit && null !== $skipToken) { - $result .= "?\$top={$limit}&\$skiptoken={$skipToken}"; - } - return $result; + public function contactIdsInList(int $customerId, int $contactListId, int $top = null, int $skiptoken = null): string + { + return QueryStringAppender::appendParamsToUrl( + $this->baseUrl($customerId) . "/{$contactListId}/contactIds", + array_filter( + ['$top' => $top, '$skiptoken' => $skiptoken], + fn ($value) => $value !== null + ) + ); } public function deleteContactsFromList(int $customerId, int $contactListId): string diff --git a/src/Suite/Api/QueryStringAppender.php b/src/Suite/Api/QueryStringAppender.php new file mode 100644 index 0000000..5194b96 --- /dev/null +++ b/src/Suite/Api/QueryStringAppender.php @@ -0,0 +1,11 @@ + new Response(self::success('{"value":[],"next":null}')), (string) self::LIST_ID_FOR_LIST_WITH_SINGLE_CHUNK => new Response(self::success('{"value":[1,2,3],"next":null}')), - (string) self::LIST_ID_FOR_LIST_WITH_MULTIPLE_CHUNKS => match ($request->query->get('$skiptoken')) { - 'first batch' => new Response(self::success('{"value":[1,2,3],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=second%20batch"}')), - 'second batch' => new Response(self::success('{"value":[4,5,6],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=third%20batch"}')), - 'third batch' => new Response(self::success('{"value":[7,8,9],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=fourth%20batch"}')), - 'fourth batch' => new Response(self::success('{"value":[10,11],"next":null}')), + (string) self::LIST_ID_FOR_LIST_WITH_MULTIPLE_CHUNKS => match ($request->query->get('$skiptoken') ?? '0') { + '0' => new Response(self::success('{"value":[1,2,3],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=1"}')), + '1' => new Response(self::success('{"value":[4,5,6],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=2"}')), + '2' => new Response(self::success('{"value":[7,8,9],"next":"http://localhost:7984/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=3"}')), + '3' => new Response(self::success('{"value":[10,11],"next":null}')), }, (string) self::LIST_ID_FOR_WRONG_RESPONSE => new Response(self::error('invalid response format')), default => new Response(self::error('contact list not found'), 404), diff --git a/test/unit/Suite/Api/ContactListTest.php b/test/unit/Suite/Api/ContactListTest.php index 70b4c21..2b293e2 100644 --- a/test/unit/Suite/Api/ContactListTest.php +++ b/test/unit/Suite/Api/ContactListTest.php @@ -262,7 +262,7 @@ public function getContactIdsInList_CalledWithProperUrlAndParams_ApiResponseConv $response = ['value' => [1, 2, 3], 'next' => null]; $this->apiClient ->method('get') - ->with("api_base_url/$this->customerId/contactlist/$this->contactListId/contactIds?\$top=1&\$skiptoken=1") + ->with("api_base_url/$this->customerId/contactlist/$this->contactListId/contactIds?%24top=1&%24skiptoken=1") ->willReturn($this->apiSuccess($response)); $result = $this->listService->getContactIdsInList($this->customerId, $this->contactListId, 1, 1); @@ -333,7 +333,7 @@ public function getContactListChunkIterator_ListFitsInSingleChunk_ContactIdsRetu { $chunkSize = 3; $this->apiClient->expects($this->once())->method('get') - ->with("api_base_url/$this->customerId/contactlist/654321/contactIds?\$top=3&\$skiptoken=first batch") + ->with("api_base_url/$this->customerId/contactlist/$this->contactListId/contactIds?%24top=3") ->willReturn( $this->apiSuccess(['value' => [1, 2, 3], 'next' => null]) );