Skip to content

Commit

Permalink
Add export handling endpoint (#1)
Browse files Browse the repository at this point in the history
* Add export handling endpoint

FA-4095

* update get data endpoint to handle CSV response FA-4095

* make special send method more explicit

FA-4095

---------

Co-authored-by: Enrico Wolf <[email protected]>
  • Loading branch information
WaffelWeib and H0m3rS1mp50n committed Nov 10, 2023
1 parent 32ae258 commit 550577a
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"guzzlehttp/guzzle": "^6|^7"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "8.*"
},
"autoload": {
"psr-0": {
Expand Down
82 changes: 79 additions & 3 deletions src/Emarsys/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,17 +745,50 @@ public function triggerEvent($eventId, array $data)
}

/**
* Fetches the status data of an export.
* Triggers an export of a given segment.
*
* @param array $data
*
* @return Response
* @throws \Emarsys\Exception\ClientException
* @throws \Emarsys\Exception\ServerException
*/
public function getExportStatus(array $data)
public function triggerExport(array $data)
{
return $this->send(self::ACTION_GET, 'export', $data);
return $this->send(self::ACTION_POST, 'export/filter', $data);
}

/**
* Fetches the status data of an export.
*
* @param int $exportId
*
* @return Response
* @throws \Emarsys\Exception\ClientException
* @throws \Emarsys\Exception\ServerException
*/
public function getExportStatus(int $exportId)
{
$url = sprintf('export/%d', $exportId);

return $this->send(self::ACTION_GET, $url);
}

/**
* Download data for a local export.
*
* @param int $exportId
* @param string $usedCSVDelimiter
*
* @return array
* @throws \Emarsys\Exception\ClientException
* @throws \Emarsys\Exception\ServerException
*/
public function getExportData(int $exportId, string $usedCSVDelimiter)
{
$url = sprintf('export/%d/data', $exportId);

return $this->getExportDataAsArray($usedCSVDelimiter, $url);
}

/**
Expand Down Expand Up @@ -1069,6 +1102,39 @@ protected function send($method = 'GET', $uri = '', array $body = [])
return new Response($responseArray, (strpos($uri, '/rds') !== false && $method === 'GET'));
}

/**
* send a request to retrieve data of a local export, returns csv data as array
*
* @param string $usedCSVDelimiter the delimiter for the csv data
* @param string $uri path to use with basePath for sending request, defaults to basePath
*
* @return array
* @throws \Emarsys\Exception\ClientException
* @throws \Emarsys\Exception\ServerException
*/
protected function getExportDataAsArray($usedCSVDelimiter, $uri = '')
{
$headers = [
'Content-Type' => 'application/json',
'X-WSSE' => $this->getAuthenticationSignature(),
];
$uri = $this->baseUrl . $uri;

$options = [
'headers' => $headers,
];

try {
$response = $this->client->request('GET', $uri, $options);
} catch (\Exception $exception) {
throw new ServerException($exception->getMessage());
}

$csvData = $response->getBody()->getContents();

return $this->resolveCSVResponseToArray($csvData, $usedCSVDelimiter);
}

/**
* Generate X-WSSE signature used to authenticate
*
Expand Down Expand Up @@ -1165,4 +1231,14 @@ private function mapFieldsForMultipleContacts(array $data)
return array_merge($data, ['contacts' => array_map([$this, 'mapFieldsToIds'], $data['contacts'])]);
}

private function resolveCSVResponseToArray($csvData, $usedCSVDelimiter)
{
$lines = explode(PHP_EOL, $csvData);
$data = [];
foreach ($lines as $line) {
$data[] = str_getcsv($line, $usedCSVDelimiter);
}

return $data;
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Suites/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,50 @@ public function testUpdateRds()
$this->assertSame('Rows updated: 2', $response->getReplyText());
}

public function testItTriggersExport()
{
$expectedResponse = $this->createExpectedResponse('postExport');
$client = $this->getGuzzleMock(200, $expectedResponse);

$data = [
'filter' => 12345,
'language' => 'de',
'delimiter' => ';',
'distribution_method' => 'local',
'add_field_names_header' => 1,
'contact_fields' => [
1,
2,
],
];
$response = $client->triggerExport($data);

$this->assertInstanceOf('\Emarsys\Response', $response);
}

public function testItReturnsExportStatus()
{
$expectedResponse = $this->createExpectedResponse('getExportStatus');
$client = $this->getGuzzleMock(200, $expectedResponse);

$response = $client->getExportStatus(12345);

$this->assertInstanceOf('\Emarsys\Response', $response);
}

public function testItReturnsDataOfExport()
{
$expectedResponse = $this->createExpectedResponse('getExportData');
$client = $this->getGuzzleMock(200, $expectedResponse);

$response = $client->getExportData(12345, ';');

$this->assertSame(
'user_id;E-Mail' . PHP_EOL . '12345;[email protected]' . PHP_EOL . '67890;[email protected]',
$response
);
}

/**
* Get a json test data and decode it
*
Expand All @@ -317,6 +361,20 @@ private function createExpectedResponse($fileName)
return $fileContent;
}

/**
* Get a json test data and decode it
*
* @param string $fileName
*
* @return mixed
*/
private function createExpectedCSVResponse($fileName)
{
$fileContent = file_get_contents(__DIR__ . '/TestData/' . $fileName . '.csv');

return $fileContent;
}

/**
* returns a emarsys client with mocked handler
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Suites/TestData/getExportData.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user_id;E-Mail
12345;[email protected]
67890;[email protected]
12 changes: 12 additions & 0 deletions tests/Unit/Suites/TestData/getExportStatus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"replyCode":0,
"replyText":"OK",
"data": {
"id":"12345"
},
"created":"2023-11-06 15:58:12",
"status":"done",
"type":"contactlist",
"distribution_method":"local",
"file_name":"export_12345_6_de-7a8b9c.csv"
}
7 changes: 7 additions & 0 deletions tests/Unit/Suites/TestData/postExport.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"replyCode": 0,
"replyText": "OK",
"data": {
"id": 2140
}
}

0 comments on commit 550577a

Please sign in to comment.