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

Support /last and /set-last-id endpoints #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 42 additions & 14 deletions src/FioApi/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,59 @@ public function getClient(): ClientInterface

public function downloadFromTo(\DateTimeInterface $from, \DateTimeInterface $to): TransactionList
{
$client = $this->getClient();
$url = $this->urlBuilder->buildPeriodsUrl($from, $to);
return $this->downloadTransactionsList($url);
}

public function downloadSince(\DateTimeInterface $since): TransactionList
{
return $this->downloadFromTo($since, new \DateTimeImmutable());
}

public function downloadLast(): TransactionList
{
$url = $this->urlBuilder->buildLastUrl();
return $this->downloadTransactionsList($url);
}

public function setLastId(string $id): void
{
$client = $this->getClient();
$url = $this->urlBuilder->buildSetLastIdUrl($id);

try {
$client->request('get', $url, ['verify' => $this->getCertificatePath()]);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
$this->handleException($e);
}
}

private function downloadTransactionsList(string $url): TransactionList
{
$client = $this->getClient();

try {
/** @var ResponseInterface $response */
$response = $client->request('get', $url, ['verify' => $this->getCertificatePath()]);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() == 409) {
throw new TooGreedyException('You can use one token for API call every 30 seconds', $e->getCode(), $e);
}
if ($e->getCode() == 500) {
throw new InternalErrorException(
'Server returned 500 Internal Error (probably invalid token?)',
$e->getCode(),
$e
);
}
throw $e;
$this->handleException($e);
}

return TransactionList::create(json_decode($response->getBody()->getContents())->accountStatement);
}

public function downloadSince(\DateTimeInterface $since): TransactionList
private function handleException(\GuzzleHttp\Exception\BadResponseException $e): void
{
return $this->downloadFromTo($since, new \DateTimeImmutable());
if ($e->getCode() == 409) {
throw new TooGreedyException('You can use one token for API call every 30 seconds', $e->getCode(), $e);
}
if ($e->getCode() == 500) {
throw new InternalErrorException(
'Server returned 500 Internal Error (probably invalid token?)',
$e->getCode(),
$e
);
}
throw $e;
}
}
17 changes: 17 additions & 0 deletions src/FioApi/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ public function buildPeriodsUrl(\DateTimeInterface $from, \DateTimeInterface $to
$to->format('Y-m-d')
);
}

public function buildLastUrl(): string
{
return sprintf(
self::BASE_URL . 'last/%s/transactions.json',
$this->getToken()
);
}

public function buildSetLastIdUrl(string $id): string
{
return sprintf(
self::BASE_URL . 'set-last-id/%s/%s',
$this->getToken(),
$id
);
}
}
24 changes: 23 additions & 1 deletion tests/FioApi/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace FioApi;

use FioApi\Exceptions;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -60,6 +59,29 @@ public function testDownloaderDownloadsData()
$this->assertInstanceOf(TransactionList::class, $transactionList);
}

public function testDownloaderDownloadsLast()
{
$handler = HandlerStack::create(new MockHandler([
new Response(200, [], file_get_contents(__DIR__ . '/data/example-response.json')),
]));
$downloader = new Downloader('validToken', new Client(['handler' => $handler]));

$transactionList = $downloader->downloadLast();

$this->assertInstanceOf(TransactionList::class, $transactionList);
}

public function testDownloaderSetsLastId()
{
$handler = HandlerStack::create(new MockHandler([
new Response(200),
]));
$downloader = new Downloader('validToken', new Client(['handler' => $handler]));

$downloader->setLastId('123456');
$this->addToAssertionCount(1);
}

public function testDownloaderSetCertificatePath()
{
$downloader = new Downloader('validToken');
Expand Down