Skip to content

Commit

Permalink
Merge branch 'before_escher4'
Browse files Browse the repository at this point in the history
-AUT-3285
Co-authored-by: Laszlo Halasz <laszlo.halasz@emarsys.com>
  • Loading branch information
fqqdk committed Sep 25, 2024
2 parents 4590ba4 + d8772db commit be8a968
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 50 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;
}
}
}
11 changes: 6 additions & 5 deletions src/Suite/Api/ContactList.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,23 @@ 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, int $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);
}
}

public function getListChunkIterator(int $customerId, int $contactListId, int $chunkSize = 10000) : iterable
public function getListChunkIterator(int $customerId, int $contactListId, int $chunkSize = null) : iterable
{
$next = $this->endPoints->contactIdsInList($customerId, $contactListId, $chunkSize, 'first batch');
$nextUrlFull = $this->endPoints->contactIdsInList($customerId, $contactListId, $chunkSize);
try {
do {
['value' => $value, 'next' => $next] = $this->apiClient->get($next)['data'];
['value' => $value, 'next' => $next] = $this->apiClient->get($nextUrlFull)['data'];
$nextUrlFull = $this->endPoints->contactIdsInListNextChunk($customerId, $contactListId, $next);
yield $value;
} while ($next !== null);
} catch (Error $error) {
Expand Down
24 changes: 18 additions & 6 deletions src/Suite/Api/ContactListEndPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,27 @@ 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)
public function contactIdsInList(int $customerId, int $contactListId, int $top = null, int $skiptoken = null): string
{
$result = $this->baseUrl($customerId) . "/{$contactListId}/contactIds";
if (null !== $limit && null !== $skipToken) {
$result .= "?\$top={$limit}&\$skiptoken={$skipToken}";
}
return $result;
return QueryStringAppender::appendParamsToUrl(
$this->baseUrl($customerId) . "/{$contactListId}/contactIds",
array_filter(
['$top' => $top, '$skiptoken' => $skiptoken],
fn ($value) => $value !== null
)
);
}

public function contactIdsInListNextChunk(int $customerId, int $contactListId, string $next = null): ?string
{
if (null === $next) {
return null;
}
$rawQuery = parse_url($next, PHP_URL_QUERY);
parse_str($rawQuery, $query);
return $this->contactIdsInList($customerId, $contactListId, $query['$top'] ?? null, $query['$skiptoken'] ?? null);
}

public function deleteContactsFromList(int $customerId, int $contactListId): string
{
return $this->baseUrl($customerId) . "/{$contactListId}/delete";
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));
}
}
2 changes: 1 addition & 1 deletion test/helper/AcceptanceBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AcceptanceBaseTestCase extends \PHPUnit\Framework\TestCase
/**
* @var string
*/
protected $apiBaseUrl = 'http://localhost:7984';
protected $apiBaseUrl = 'http://localhost:7984/internal';

protected function setUp(): void
{
Expand Down
24 changes: 12 additions & 12 deletions test/helper/ApiStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,43 @@ public static function setUp()
$app->before(self::authenticateWithEscher());
$app->error(self::handleError());

$app->get('/', self::clientTestEndPoint());
$app->get('/internal/', self::clientTestEndPoint());

foreach (include __DIR__.'/stubs.php' as $route => $data) {
$app->get($route, function () use ($data) { return self::success($data); });
}

$app->post('/{customerId}/email/{campaignId}/preview/', function (Request $request) {
$app->post('/internal/{customerId}/email/{campaignId}/preview/', function (Request $request) {
$params = json_decode($request->getContent(), true);
return new Response(self::success('"'.$params['version'].' version"'));
});

$app->post('/{customerId}/email/{campaignId}/launch/', function (Request $request) {
$app->post('/internal/{customerId}/email/{campaignId}/launch/', function (Request $request) {
return new Response(self::success("null"));
});

$app->post('/{customerId}/email/delete/', function (Request $request) {
$app->post('/internal/{customerId}/email/delete/', function (Request $request) {
return new Response(self::success("null"));
});

$app->get('/serverError', function (Request $request) {
$app->get('/internal/serverError', function (Request $request) {
self::logRetry();
return new Response(self::error("null"), 500);
});

$app->get('/retryCount', function (Request $request) {
$app->get('/internal/retryCount', function (Request $request) {
return new Response(self::success(self::getRetryCount()));
});

$app->get('/{customerId}/contactlist/{contactListId}/contactIds', function (Request $request, $contactListId, $customerId) {
$app->get('/internal/{customerId}/contactlist/{contactListId}/contactIds', function (Request $request, $contactListId, $customerId) {
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":"/internal/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=1"}')),
'1' => new Response(self::success('{"value":[4,5,6],"next":"/internal/'.$customerId.'/contactlist/'.$contactListId.'/contactIds?$skiptoken=2"}')),
'2' => new Response(self::success('{"value":[7,8,9],"next":"/internal/'.$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
10 changes: 5 additions & 5 deletions test/helper/stubs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php return [
'/{customerId}/email/{campaignId}/' => '{
'/internal/{customerId}/email/{campaignId}/' => '{
"id": "1",
"language": "en",
"name": "20160407_1502_Copy of Test mail 1",
Expand Down Expand Up @@ -35,7 +35,7 @@
"source": "userlist"
}',

'/{customerId}/email/' => '[
'/internal/{customerId}/email/' => '[
{
"id": "2",
"administrator": "3",
Expand Down Expand Up @@ -87,7 +87,7 @@
"source": "profile"
}
]',
'/{customerId}/administrator/' => '[
'/internal/{customerId}/administrator/' => '[
{
"id": "1",
"username": "admin",
Expand Down Expand Up @@ -130,7 +130,7 @@
}
]',

'/{customerId}/event' => '[
'/internal/{customerId}/event' => '[
{
"id": "1",
"name": "event egy",
Expand All @@ -149,7 +149,7 @@
}
]',

'/{customerId}/filter' => '[
'/internal/{customerId}/filter' => '[
{
"id": "1",
"name": "segment 1",
Expand Down
19 changes: 17 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,14 +333,29 @@ 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])
);
$iterator = $this->listService->getListChunkIterator($this->customerId, $this->contactListId, $chunkSize);
$this->assertEquals([[1, 2, 3]], iterator_to_array($iterator));
}

/**
* @test
*/
public function getContactListChunkIterator_ChunkSizeNotPassed_TopNotSentInRequest(): void
{
$chunkSize = 3;
$this->apiClient->expects($this->once())->method('get')
->with("api_base_url/$this->customerId/contactlist/$this->contactListId/contactIds")
->willReturn(
$this->apiSuccess(['value' => [1, 2, 3], 'next' => null])
);
$iterator = $this->listService->getListChunkIterator($this->customerId, $this->contactListId);
$this->assertEquals([[1, 2, 3]], iterator_to_array($iterator));
}

/**
* @test
*/
Expand Down

0 comments on commit be8a968

Please sign in to comment.