Progress for Spectre import.

This commit is contained in:
James Cole
2017-12-28 18:38:59 +01:00
parent 0977d8d631
commit a10672a683
19 changed files with 272 additions and 1379 deletions

View File

@@ -93,4 +93,16 @@ class Customer extends SpectreObject
{
$this->secret = $secret;
}
/**
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'identifier' => $this->identifier,
'secret' => $this->secret,
];
}
}

View File

@@ -1,32 +0,0 @@
<?php
/**
* Login.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\Object;
/**
* Class Login
*/
class Login extends SpectreObject
{
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* Token.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\Object;
use Carbon\Carbon;
/**
* Class Token
*/
class Token extends SpectreObject
{
/** @var string */
private $connectUrl;
/** @var Carbon */
private $expiresAt;
/** @var string */
private $token;
/**
* Token constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->token = $data['token'];
$this->expiresAt = new Carbon($data['expires_at']);
$this->connectUrl = $data['connect_url'];
}
/**
* @return string
*/
public function getConnectUrl(): string
{
return $this->connectUrl;
}
/**
* @return Carbon
*/
public function getExpiresAt(): Carbon
{
return $this->expiresAt;
}
/**
* @return string
*/
public function getToken(): string
{
return $this->token;
}
}

View File

@@ -1,109 +0,0 @@
<?php
/**
* CreateLoginRequest.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\Exceptions\FireflyException;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Services\Spectre\Object\Customer;
/**
* Class CreateLoginRequest
*/
class CreateLoginRequest extends SpectreRequest
{
/** @var Customer */
private $customer;
/** @var array */
private $mandatoryFields = [];
/** @var SpectreProvider */
private $provider;
/**
*
* @throws FireflyException
*/
public function call(): void
{
// add mandatory fields to login object
$data = [
'customer_id' => $this->customer->getId(),
'country_code' => $this->provider->country_code,
'provider_code' => $this->provider->code,
'credentials' => $this->buildCredentials(),
'daily_refresh' => true,
'fetch_type' => 'recent',
'include_fake_providers' => true,
];
$uri = '/api/v3/logins';
$response = $this->sendSignedSpectrePost($uri, $data);
echo '<pre>';
print_r($response);
exit;
}
/**
* @param Customer $customer
*/
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
/**
* @param array $mandatoryFields
*/
public function setMandatoryFields(array $mandatoryFields): void
{
$this->mandatoryFields = $mandatoryFields;
}
/**
* @param SpectreProvider $provider
*/
public function setProvider(SpectreProvider $provider): void
{
$this->provider = $provider;
}
/**
* @return array
* @throws FireflyException
*/
private function buildCredentials(): array
{
$return = [];
/** @var array $requiredField */
foreach ($this->provider->data['required_fields'] as $requiredField) {
$fieldName = $requiredField['name'];
if (!isset($this->mandatoryFields[$fieldName])) {
throw new FireflyException(sprintf('Mandatory field "%s" is missing from job.', $fieldName));
}
$return[$fieldName] = $this->mandatoryFields[$fieldName];
}
return $return;
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* CreateTokenRequest.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 FireflyIII\Services\Spectre\Object\Token;
/**
* Class CreateTokenRequest
*/
class CreateTokenRequest extends SpectreRequest
{
/** @var Customer */
private $customer;
/** @var Token */
private $token;
/** @var string */
private $uri;
/**
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function call(): void
{
// add mandatory fields to login object
$data = [
'data' => [
'customer_id' => $this->customer->getId(),
'fetch_type' => 'recent',
'daily_refresh' => true,
'include_fake_providers' => true,
'show_consent_confirmation' => true,
'credentials_strategy' => 'ask',
'return_to' => $this->uri,
],
];
$uri = '/api/v3/tokens/create';
$response = $this->sendSignedSpectrePost($uri, $data);
$this->token = new Token($response['data']);
return;
}
/**
* @return Token
*/
public function getToken(): Token
{
return $this->token;
}
/**
* @param Customer $customer
*/
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
/**
* @param string $uri
*/
public function setUri(string $uri): void
{
$this->uri = $uri;
}
}

View File

@@ -1,101 +0,0 @@
<?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

@@ -1,81 +0,0 @@
<?php
/**
* ListProvidersRequest.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 Log;
/**
* Class ListProvidersRequest
*/
class ListProvidersRequest extends SpectreRequest
{
/**
* @var array
*/
protected $providers = [];
/**
* @throws \Exception
*/
public function call(): void
{
$hasNextPage = true;
$nextId = 0;
while ($hasNextPage) {
Log::debug(sprintf('Now calling for next_id %d', $nextId));
$parameters = ['include_fake_providers' => 'true', 'include_provider_fields' => 'true', 'from_id' => $nextId];
$uri = '/api/v3/providers?' . 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 $providerArray) {
$providerId = $providerArray['id'];
$this->providers[$providerId] = $providerArray;
Log::debug(sprintf('Stored provider #%d', $providerId));
}
}
return;
}
/**
* @return array
*/
public function getProviders(): array
{
return $this->providers;
}
}

View File

@@ -37,19 +37,13 @@ class NewCustomerRequest extends SpectreRequest
*/
public function call(): void
{
$data = [
$data = [
'data' => [
'identifier' => 'default_ff3_customer',
],
];
$uri = '/api/v3/customers/';
//$response = $this->sendSignedSpectrePost($uri, $data);
$response = ['data' => [
'id' => 527858,
'identifier' => 'default_ff3_customer',
'secret' => 'qpZjRPJRTb6mMcQgwDkssZ3fQVVDPIH04zBlkKC6MvI',
],
];
$uri = '/api/v3/customers/';
$response = $this->sendSignedSpectrePost($uri, $data);
// create customer:
$this->customer = new Customer($response['data']);