Expand Spectre code.

This commit is contained in:
James Cole 2017-12-27 17:22:44 +01:00
parent deebdef04d
commit a14ae02c27
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
11 changed files with 237 additions and 17 deletions

View File

@ -24,6 +24,7 @@ namespace FireflyIII\Import\Routine;
use FireflyIII\Models\ImportJob;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use Illuminate\Support\Collection;
use Log;
@ -91,9 +92,34 @@ class SpectreRoutine implements RoutineInterface
// create customer if user does not have one:
$customer = $this->getCustomer();
// list all logins present at Spectre
$logins = $this->listLogins($customer);
// use latest (depending on status, and if login exists for selected country + provider)
$country = $this->job->configuration['country'];
$providerId = $this->job->configuration['provider'];
$login = $this->filterLogins($logins, $country, $providerId);
// create new login if list is empty or no login exists.
if (is_null($login)) {
$login = $this->createLogin($customer);
die('new login');
}
echo '<pre>';
print_r($logins);
exit;
return true;
}
/**
* @param Customer $customer
*/
protected function createLogin(Customer $customer) {
}
/**
* @param ImportJob $job
*/
@ -104,18 +130,22 @@ class SpectreRoutine implements RoutineInterface
/**
* @return Customer
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function createCustomer(): Customer
{
$newCustomerRequest = new NewCustomerRequest($this->job->user);
$newCustomerRequest->call();
echo '<pre>';
print_r($newCustomerRequest->getCustomer());
exit;
$customer = $newCustomerRequest->getCustomer();
// store customer. Not sure where. User preference? TODO
return $customer;
}
/**
* @return Customer
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function getCustomer(): Customer
{
@ -126,4 +156,40 @@ class SpectreRoutine implements RoutineInterface
var_dump($preference->data);
exit;
}
/**
* Return login belonging to country and provider
* TODO must return Login object, not array
*
* @param array $logins
* @param string $country
* @param int $providerId
*
* @return array|null
*/
private function filterLogins(array $logins, string $country, int $providerId): ?array
{
if (count($logins) === 0) {
return null;
}
foreach ($logins as $login) {
die('do some filter');
}
return null;
}
/**
* @return array
*/
private function listLogins(Customer $customer): array
{
$listLoginRequest = new ListLoginsRequest($this->job->user);
$listLoginRequest->setCustomer($customer);
$listLoginRequest->call();
$logins = $listLoginRequest->getLogins();
return $logins;
}
}

View File

@ -34,6 +34,18 @@ class Customer extends SpectreObject
/** @var string */
private $secret;
/**
* Customer constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->id = intval($data['id']);
$this->identifier = $data['identifier'];
$this->secret = $data['secret'];
}
/**
* @return int
*/

View File

@ -0,0 +1,101 @@
<?php
/**
* ListLoginsRequest.php
* Copyright (c) 2017 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\Spectre\Request;
use FireflyIII\Services\Spectre\Object\Customer;
use Log;
/**
* Class ListLoginsRequest
*/
class ListLoginsRequest extends SpectreRequest
{
/** @var Customer */
protected $customer;
/** @var array */
protected $logins = [];
/**
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function call(): void
{
$hasNextPage = true;
$nextId = 0;
while ($hasNextPage) {
Log::debug(sprintf('Now calling list-logins for next_id %d', $nextId));
$parameters = ['customer_id' => $this->customer->getId(), 'from_id' => $nextId];
$uri = '/api/v3/logins?' . http_build_query($parameters);
$response = $this->sendSignedSpectreGet($uri, []);
// count entries:
Log::debug(sprintf('Found %d entries in data-array', count($response['data'])));
// extract next ID
$hasNextPage = false;
if (isset($response['meta']['next_id']) && intval($response['meta']['next_id']) > $nextId) {
$hasNextPage = true;
$nextId = $response['meta']['next_id'];
Log::debug(sprintf('Next ID is now %d.', $nextId));
} else {
Log::debug('No next page.');
}
// store providers:
foreach ($response['data'] as $loginArray) {
var_dump($loginArray);
exit;
}
}
return;
}
/**
* @return Customer
*/
public function getCustomer(): Customer
{
return $this->customer;
}
/**
* @param Customer $customer
*/
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
/**
* @return array
*/
public function getLogins(): array
{
return $this->logins;
}
}

View File

@ -25,7 +25,7 @@ namespace FireflyIII\Services\Spectre\Request;
use Log;
/**
* Class ListUserRequest.
* Class ListProvidersRequest
*/
class ListProvidersRequest extends SpectreRequest
{

View File

@ -22,37 +22,44 @@ declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Request;
use FireflyIII\Services\Spectre\Object\Customer;
/**
* Class NewCustomerRequest
*/
class NewCustomerRequest extends SpectreRequest
{
/** @var array */
protected $customer = [];
/** @var Customer */
protected $customer;
/**
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function call(): void
{
$data = [
$data = [
'data' => [
'identifier' => 'default_ff3_customer',
],
];
$uri = '/api/v3/customers/';
$response = $this->sendSignedSpectrePost($uri, $data);
$uri = '/api/v3/customers/';
//$response = $this->sendSignedSpectrePost($uri, $data);
$response = ['data' => [
'id' => 527858,
'identifier' => 'default_ff3_customer',
'secret' => 'qpZjRPJRTb6mMcQgwDkssZ3fQVVDPIH04zBlkKC6MvI',
],
];
// create customer:
$this->customer = $response['data'];
$this->customer = new Customer($response['data']);
return;
}
/**
* @return array
* @return Customer
*/
public function getCustomer(): array
public function getCustomer(): Customer
{
return $this->customer;
}

View File

@ -29,8 +29,6 @@ use Requests;
use Requests_Exception;
use Requests_Response;
//use FireflyIII\Services\Bunq\Object\ServerPublicKey;
/**
* Class BunqRequest.
*/
@ -267,7 +265,7 @@ abstract class SpectreRequest
Log::debug('Final headers for spectre signed POST request:', $headers);
try {
$response = Requests::get($fullUri, $headers);
$response = Requests::post($fullUri, $headers, $body);
} catch (Requests_Exception $e) {
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
}

View File

@ -63,6 +63,7 @@ class SpectreInformation implements InformationInterface
* @return array
*
* @throws FireflyException
* @throws \Exception
*/
public function getAccounts(): array
{

View File

@ -61,6 +61,7 @@ class User extends Authenticatable
protected $table = 'users';
/**
* @codeCoverageIgnore
* Link to accounts.
*
* @return HasMany
@ -95,6 +96,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to attachments
*
* @return HasMany
@ -105,6 +107,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to available budgets
*
* @return HasMany
@ -115,6 +118,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to bills.
*
* @return HasMany
@ -125,6 +129,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to budgets.
*
* @return HasMany
@ -135,6 +140,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to categories
*
* @return HasMany
@ -145,6 +151,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to currency exchange rates
*
* @return HasMany
@ -155,6 +162,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to export jobs
*
* @return HasMany
@ -165,6 +173,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Generates access token.
*
* @return string
@ -177,6 +186,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Checks if the user has a role by its name.
*
* Full credit goes to: https://github.com/Zizaco/entrust
@ -197,6 +207,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to import jobs.
*
* @return HasMany
@ -207,6 +218,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to piggy banks.
*
* @return HasManyThrough
@ -217,6 +229,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to preferences.
*
* @return HasMany
@ -227,6 +240,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to roles.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
@ -237,6 +251,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to rule groups.
*
* @return HasMany
@ -247,6 +262,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to rules.
*
* @return HasMany
@ -257,6 +273,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Send the password reset notification.
*
* @param string $token
@ -269,6 +286,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to tags.
*
* @return HasMany
@ -279,6 +297,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to transaction journals.
*
* @return HasMany
@ -289,6 +308,7 @@ class User extends Authenticatable
}
/**
* @codeCoverageIgnore
* Link to transactions.
*
* @return HasManyThrough

View File

@ -141,6 +141,9 @@ return [
'column_opposing-number' => 'Opposing account (account number)',
'column_note' => 'Note(s)',
// prerequisites
'prerequisites' => 'Prerequisites',
// bunq
'bunq_prerequisites_title' => 'Prerequisites for an import from bunq',
'bunq_prerequisites_text' => 'In order to import from bunq, you need to obtain an API key. You can do this through the app.',

View File

@ -573,6 +573,16 @@ Breadcrumbs::register(
$breadcrumbs->push(trans('import.config_sub_title', ['key' => $job->key]), route('import.configure', [$job->key]));
}
);
Breadcrumbs::register(
'import.prerequisites',
function (BreadCrumbsGenerator $breadcrumbs, string $bank) {
$breadcrumbs->parent('import.index');
$breadcrumbs->push(trans('import.prerequisites'), route('import.prerequisites', [$bank]));
}
);
Breadcrumbs::register(
'import.status',
function (BreadCrumbsGenerator $breadcrumbs, ImportJob $job) {
@ -581,6 +591,8 @@ Breadcrumbs::register(
}
);
// PREFERENCES
Breadcrumbs::register(
'preferences.index',

View File

@ -445,7 +445,7 @@ Route::group(
Route::get('json/{importJob}', ['uses' => 'Import\StatusController@json', 'as' => 'status.json']);
// start a job
Route::post('start/{importJob}', ['uses' => 'Import\IndexController@start', 'as' => 'start']);
Route::any('start/{importJob}', ['uses' => 'Import\IndexController@start', 'as' => 'start']);
// download config
Route::get('download/{importJob}', ['uses' => 'Import\IndexController@download', 'as' => 'download']);