Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DataExports endpoints (external pr) #166

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class Client
* @var Endpoints\Insurance
*/
public $insurance;

/**
* @var Endpoints\DataExports
*/
public $dataExports;
/*************************/
/**
* @var Endpoints\Configuration
Expand Down Expand Up @@ -155,6 +160,7 @@ private function initEndpoints()
$this->shareOfCheckout = new Endpoints\ShareOfCheckout($this->context);
$this->webhooks = new Endpoints\Webhooks($this->context);
$this->insurance = new Endpoints\Insurance($this->context);
$this->dataExports = new Endpoints\DataExports($this->context);
$this->configuration = new Endpoints\Configuration($this->context);
}

Expand Down
100 changes: 100 additions & 0 deletions src/Endpoints/DataExports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright (c) 2018 Alma / Nabla SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma / Nabla SAS <contact@getalma.eu>
* @copyright Copyright (c) 2018 Alma / Nabla SAS
* @license https://opensource.org/licenses/MIT The MIT License
*
*/

namespace Alma\API\Endpoints;

use Alma\API\Entities\DataExport;
use Alma\API\Exceptions\ParametersException;
use Alma\API\Exceptions\RequestException;
use Alma\API\ParamsError;
use Alma\API\RequestError;

class DataExports extends Base
{
const DATA_EXPORTS_PATH = '/v1/data-exports';
const ACCEPTED_FORMAT = ['csv', 'xlsx'];

/**
* @param $data
*
* @return DataExport
*
* @throws RequestException|RequestError
*/
public function create($data)
{
$res = $this->request(self::DATA_EXPORTS_PATH)->setRequestBody($data)->post();

if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}

return new DataExport($res->json);
}

/**
* @param $reportId
*
* @return DataExport
*
* @throws RequestException|RequestError
*/
public function fetch($reportId)
{
$res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId)->get();

if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}

return new DataExport($res->json);
}

/**
* @param $reportId
*
* @param string $format only csv or xlsx
*
* @return mixed
*
* @throws RequestException|RequestError|ParametersException
*/
public function download($reportId, $format)
{
if (!in_array($format, self::ACCEPTED_FORMAT)) {
throw new ParametersException("Invalid format: $format. Accepted format are: " . implode(', ', self::ACCEPTED_FORMAT));
}

$res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId)
->setQueryParams(['format' => $format])
->get();

if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}

return $res->data;
}
}
79 changes: 79 additions & 0 deletions src/Entities/DataExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright (c) 2018 Alma / Nabla SAS.
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma / Nabla SAS <contact@getalma.eu>
* @copyright Copyright (c) 2018 Alma / Nabla SAS
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Alma\API\Entities;

class DataExport extends Base
{
/** @var bool */
public $complete;

/** @var int Timestamp */
public $created;

/** @var int Timestamp */
public $end;

/** @var string */
public $id;

/** @var bool */
public $include_child_accounts;

/** @var string */
public $merchant;


/** @var int Timestamp */
public $start;

/** @var string */
public $type;

/** @var string Timestamp */
public $updated;

/** @var string */
public $url_csv;

/** @var string */
public $url_pdf;

/** @var string */
public $url_xlsx;

/** @var string */
public $url_xml;

/** @var string */
public $url_zip;

/**
* @param array $attributes
*/
public function __construct($attributes)
{
parent::__construct($attributes);
}
}
9 changes: 8 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ class Response
{
public $responseCode;
public $json;
public $data;
public $errorMessage;

public function __construct($curlHandle, $curlResult)
{
$this->responseCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
$this->json = json_decode($curlResult, true);

if ('application/json' === curl_getinfo($curlHandle, CURLINFO_CONTENT_TYPE)) {
$this->json = json_decode($curlResult, true);
} else {
$this->json = null;
$this->data = $curlResult;
}

if ($this->isError()) {
if ($this->json && array_key_exists('message', $this->json)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Endpoints/DataExportsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Alma\API\Tests\Integration\Endpoints;

use Alma\API\Tests\Integration\TestHelpers\ClientTestHelper;
use PHPUnit\Framework\TestCase;

final class DataExportsTest extends TestCase
{
protected static $almaClient;

public static function setUpBeforeClass(): void
{
DataExportsTest::$almaClient = ClientTestHelper::getAlmaClient();
}

public function testCanFetchDataExport()
{
$data = [
"type" => "payments",
"include_child_accounts" => false
];
$dataExport = DataExportsTest::$almaClient->dataExports->create($data);
$this->assertNotNull($dataExport->id);

for ($i = 0; $i < 5; $i++) {
$fetchedExport = DataExportsTest::$almaClient->dataExports->fetch($dataExport->id);
if ($fetchedExport->complete) {
break;
}
sleep(2);
}
$this->assertEquals($dataExport->id, $fetchedExport->id);

$downloadedCsvExport = DataExportsTest::$almaClient->dataExports->download($dataExport->id, 'csv');
$this->assertNotNull($downloadedCsvExport);
}

}
Loading