Skip to content

Commit

Permalink
fix contact list fetch endpoint's default value
Browse files Browse the repository at this point in the history
- AUT-3246

Co-authored-by: Bence Kadar <[email protected]>
  • Loading branch information
fqqdk and bencekadaremar committed Sep 20, 2024
1 parent 65a5df7 commit f1b2dd8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
20 changes: 1 addition & 19 deletions src/Suite/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
}
}
6 changes: 3 additions & 3 deletions src/Suite/Api/ContactList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'];
Expand Down
16 changes: 9 additions & 7 deletions src/Suite/Api/ContactListEndPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Suite/Api/QueryStringAppender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Suite\Api;

class QueryStringAppender
{
public static function appendParamsToUrl(string $url, array $data): string
{
return $url . (empty($data) ? '' : '?' . http_build_query($data));
}
}
10 changes: 5 additions & 5 deletions test/helper/ApiStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public static function setUp()
return match ($contactListId) {
(string) self::LIST_ID_FOR_EMPTY_LIST => 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),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Suite/Api/ContactListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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])
);
Expand Down

0 comments on commit f1b2dd8

Please sign in to comment.