mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-15 19:09:50 +00:00
New code for YNAB import.
This commit is contained in:
53
app/Services/Ynab/Request/GetAccountsRequest.php
Normal file
53
app/Services/Ynab/Request/GetAccountsRequest.php
Normal 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'] ?? [];
|
||||
}
|
||||
}
|
||||
66
app/Services/Ynab/Request/GetBudgetsRequest.php
Normal file
66
app/Services/Ynab/Request/GetBudgetsRequest.php
Normal 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;
|
||||
}
|
||||
}
|
||||
100
app/Services/Ynab/Request/YnabRequest.php
Normal file
100
app/Services/Ynab/Request/YnabRequest.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user