Skip to content

Commit d8f9d39

Browse files
authored
Merge pull request #166 from alma/chore/review-external-pr
Add DataExports endpoints (external pr)
2 parents b16c004 + 8d92bc3 commit d8f9d39

6 files changed

Lines changed: 417 additions & 1 deletion

File tree

src/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class Client
7070
* @var Endpoints\Insurance
7171
*/
7272
public $insurance;
73+
74+
/**
75+
* @var Endpoints\DataExports
76+
*/
77+
public $dataExports;
7378
/*************************/
7479
/**
7580
* @var Endpoints\Configuration
@@ -155,6 +160,7 @@ private function initEndpoints()
155160
$this->shareOfCheckout = new Endpoints\ShareOfCheckout($this->context);
156161
$this->webhooks = new Endpoints\Webhooks($this->context);
157162
$this->insurance = new Endpoints\Insurance($this->context);
163+
$this->dataExports = new Endpoints\DataExports($this->context);
158164
$this->configuration = new Endpoints\Configuration($this->context);
159165
}
160166

src/Endpoints/DataExports.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2018 Alma / Nabla SAS
4+
*
5+
* THE MIT LICENSE
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10+
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
12+
* Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
15+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17+
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18+
* IN THE SOFTWARE.
19+
*
20+
* @author Alma / Nabla SAS <contact@getalma.eu>
21+
* @copyright Copyright (c) 2018 Alma / Nabla SAS
22+
* @license https://opensource.org/licenses/MIT The MIT License
23+
*
24+
*/
25+
26+
namespace Alma\API\Endpoints;
27+
28+
use Alma\API\Entities\DataExport;
29+
use Alma\API\Exceptions\ParametersException;
30+
use Alma\API\Exceptions\RequestException;
31+
use Alma\API\ParamsError;
32+
use Alma\API\RequestError;
33+
34+
class DataExports extends Base
35+
{
36+
const DATA_EXPORTS_PATH = '/v1/data-exports';
37+
const ACCEPTED_FORMAT = ['csv', 'xlsx'];
38+
39+
/**
40+
* @param $data
41+
*
42+
* @return DataExport
43+
*
44+
* @throws RequestException|RequestError
45+
*/
46+
public function create($data)
47+
{
48+
$res = $this->request(self::DATA_EXPORTS_PATH)->setRequestBody($data)->post();
49+
50+
if ($res->isError()) {
51+
throw new RequestException($res->errorMessage, null, $res);
52+
}
53+
54+
return new DataExport($res->json);
55+
}
56+
57+
/**
58+
* @param $reportId
59+
*
60+
* @return DataExport
61+
*
62+
* @throws RequestException|RequestError
63+
*/
64+
public function fetch($reportId)
65+
{
66+
$res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId)->get();
67+
68+
if ($res->isError()) {
69+
throw new RequestException($res->errorMessage, null, $res);
70+
}
71+
72+
return new DataExport($res->json);
73+
}
74+
75+
/**
76+
* @param $reportId
77+
*
78+
* @param string $format only csv or xlsx
79+
*
80+
* @return mixed
81+
*
82+
* @throws RequestException|RequestError|ParametersException
83+
*/
84+
public function download($reportId, $format)
85+
{
86+
if (!in_array($format, self::ACCEPTED_FORMAT)) {
87+
throw new ParametersException("Invalid format: $format. Accepted format are: " . implode(', ', self::ACCEPTED_FORMAT));
88+
}
89+
90+
$res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId)
91+
->setQueryParams(['format' => $format])
92+
->get();
93+
94+
if ($res->isError()) {
95+
throw new RequestException($res->errorMessage, null, $res);
96+
}
97+
98+
return $res->responseFile;
99+
}
100+
}

src/Entities/DataExport.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2018 Alma / Nabla SAS.
4+
*
5+
* THE MIT LICENSE
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10+
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
12+
* Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
15+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17+
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18+
* IN THE SOFTWARE.
19+
*
20+
* @author Alma / Nabla SAS <contact@getalma.eu>
21+
* @copyright Copyright (c) 2018 Alma / Nabla SAS
22+
* @license https://opensource.org/licenses/MIT The MIT License
23+
*/
24+
25+
namespace Alma\API\Entities;
26+
27+
class DataExport extends Base
28+
{
29+
/** @var bool */
30+
public $complete;
31+
32+
/** @var int Timestamp */
33+
public $created;
34+
35+
/** @var int Timestamp */
36+
public $end;
37+
38+
/** @var string */
39+
public $id;
40+
41+
/** @var bool */
42+
public $include_child_accounts;
43+
44+
/** @var string */
45+
public $merchant;
46+
47+
48+
/** @var int Timestamp */
49+
public $start;
50+
51+
/** @var string */
52+
public $type;
53+
54+
/** @var string Timestamp */
55+
public $updated;
56+
57+
/** @var string */
58+
public $url_csv;
59+
60+
/** @var string */
61+
public $url_pdf;
62+
63+
/** @var string */
64+
public $url_xlsx;
65+
66+
/** @var string */
67+
public $url_xml;
68+
69+
/** @var string */
70+
public $url_zip;
71+
72+
/**
73+
* @param array $attributes
74+
*/
75+
public function __construct($attributes)
76+
{
77+
parent::__construct($attributes);
78+
}
79+
}

src/Response.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ class Response
2929
{
3030
public $responseCode;
3131
public $json;
32+
public $responseFile;
3233
public $errorMessage;
3334

3435
public function __construct($curlHandle, $curlResult)
3536
{
3637
$this->responseCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
37-
$this->json = json_decode($curlResult, true);
38+
39+
if ('application/json' === curl_getinfo($curlHandle, CURLINFO_CONTENT_TYPE)) {
40+
$this->json = json_decode($curlResult, true);
41+
} else {
42+
$this->json = null;
43+
$this->responseFile = $curlResult;
44+
}
3845

3946
if ($this->isError()) {
4047
if ($this->json && array_key_exists('message', $this->json)) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Alma\API\Tests\Integration\Endpoints;
4+
5+
use Alma\API\Tests\Integration\TestHelpers\ClientTestHelper;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class DataExportsTest extends TestCase
9+
{
10+
protected static $almaClient;
11+
12+
public static function setUpBeforeClass(): void
13+
{
14+
DataExportsTest::$almaClient = ClientTestHelper::getAlmaClient();
15+
}
16+
17+
public function testCanFetchDataExport()
18+
{
19+
$data = [
20+
"type" => "payments",
21+
"include_child_accounts" => false
22+
];
23+
$dataExport = DataExportsTest::$almaClient->dataExports->create($data);
24+
$this->assertNotNull($dataExport->id);
25+
26+
for ($i = 0; $i < 5; $i++) {
27+
$fetchedExport = DataExportsTest::$almaClient->dataExports->fetch($dataExport->id);
28+
if ($fetchedExport->complete) {
29+
break;
30+
}
31+
sleep(2);
32+
}
33+
$this->assertEquals($dataExport->id, $fetchedExport->id);
34+
35+
$downloadedCsvExport = DataExportsTest::$almaClient->dataExports->download($dataExport->id, 'csv');
36+
$this->assertNotNull($downloadedCsvExport);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)