New code for YNAB import.

This commit is contained in:
James Cole
2018-07-29 21:02:03 +02:00
parent 7ad09da4e9
commit dfd9cf0874
19 changed files with 1341 additions and 264 deletions

View File

@@ -0,0 +1,53 @@
<?php
/**
* GetAccountsRequest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Ynab\Request;
use Log;
/**
* Class GetAccountsRequest
*/
class GetAccountsRequest extends YnabRequest
{
/** @var array */
public $accounts;
/** @var string */
public $budgetId;
/**
*
*/
public function call(): void
{
Log::debug('Now in GetAccountsRequest::call()');
$uri = $this->api . sprintf('/budgets/%s/accounts', $this->budgetId);
Log::debug(sprintf('URI is %s', $uri));
$result = $this->authenticatedGetRequest($uri, []);
// expect data in [data][accounts]
$this->accounts = $result['data']['accounts'] ?? [];
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* GetBudgetsRequest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Ynab\Request;
use Log;
/**
* Class GetBudgetsRequest
*/
class GetBudgetsRequest extends YnabRequest
{
/** @var array */
public $budgets;
public function __construct()
{
parent::__construct();
$this->budgets = [];
}
/**
*
*/
public function call(): void
{
Log::debug('Now in GetBudgetsRequest::call()');
$uri = $this->api . '/budgets';
Log::debug(sprintf('URI is %s', $uri));
$result = $this->authenticatedGetRequest($uri, []);
// expect data in [data][budgets]
$rawBudgets = $result['data']['budgets'] ?? [];
$freshBudgets = [];
foreach ($rawBudgets as $rawBudget) {
$freshBudgets[] = [
'id' => $rawBudget['id'],
'name' => $rawBudget['name'],
'currency_code' => $rawBudget['currency_format']['iso_code'],
];
}
$this->budgets = $freshBudgets;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* YnabRequest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Ynab\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Log;
use RuntimeException;
/**
* Class YnabRequest
*/
abstract class YnabRequest
{
/** @var string */
protected $api;
/** @var string */
protected $token;
public function __construct()
{
$this->api = 'https://' . config('import.options.ynab.live') . '/' . config('import.options.ynab.version');
}
/**
* @param string $uri
* @param array|null $params
*
* @return array
*/
public function authenticatedGetRequest(string $uri, array $params = null): array
{
Log::debug(sprintf('Now in YnabRequest::authenticatedGetRequest(%s)', $uri), $params);
$client = new Client;
$params = $params ?? [];
$options = [
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
],
];
if (\count($params) > 0) {
$uri = $uri . '?' . http_build_query($params);
}
Log::debug(sprintf('Going to call YNAB on URI: %s', $uri), $options);
try {
$res = $client->request('get', $uri, $options);
} catch (GuzzleException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
return [];
}
try {
$content = trim($res->getBody()->getContents());
} catch (RuntimeException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
return [];
}
return json_decode($content, true) ?? [];
}
/**
*
*/
abstract public function call(): void;
/**
* @param string $token
*/
public function setAccessToken(string $token): void
{
$this->token = $token;
}
}