Skip to content

Firefly III: a personal finances manager

Use Guzzle, not Requests library.

James Cole20044427b4e9
diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php
index 7c9358e4bb..a9fcd1869a 100644
--- a/app/Helpers/Help/Help.php
+++ b/app/Helpers/Help/Help.php
@@ -24,9 +24,10 @@ namespace FireflyIII\Helpers\Help;
use Cache;
use Exception;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
use League\CommonMark\CommonMarkConverter;
use Log;
-use Requests;
use Route;
/**
@@ -64,20 +65,21 @@ class Help implements HelpInterface
{
$uri = sprintf('https://raw.githubusercontent.com/firefly-iii/help/master/%s/%s.md', $language, $route);
Log::debug(sprintf('Trying to get %s...', $uri));
- $opt = ['useragent' => $this->userAgent];
+ $opt = ['headers' => ['User-Agent' => $this->userAgent]];
$content = '';
try {
- $result = Requests::get($uri, [], $opt);
- } catch (Exception $e) {
+ $client = new Client;
+ $res = $client->request('GET', $uri, $opt);
+ } catch (GuzzleException|Exception $e) {
Log::error($e);
return '';
}
- Log::debug(sprintf('Status code is %d', $result->status_code));
+ Log::debug(sprintf('Status code is %d', $res->getStatusCode()));
- if (200 === $result->status_code) {
- $content = trim($result->body);
+ if (200 === $res->getStatusCode()) {
+ $content = trim($res->getBody()->getContents());
}
if (\strlen($content) > 0) {
diff --git a/app/Services/Currency/FixerIO.php b/app/Services/Currency/FixerIO.php
deleted file mode 100644
index 561cac7315..0000000000
--- a/app/Services/Currency/FixerIO.php
+++ /dev/null
@@ -1,96 +0,0 @@
-<?php
-/**
- * FixerIO.php
- * Copyright (c) 2017 [email protected]
- *
- * 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\Currency;
-
-use Carbon\Carbon;
-use Exception;
-use FireflyIII\Models\CurrencyExchangeRate;
-use FireflyIII\Models\TransactionCurrency;
-use FireflyIII\User;
-use Log;
-use Requests;
-
-/**
- * Class FixerIO.
- */
-class FixerIO implements ExchangeRateInterface
-{
- /** @var User */
- protected $user;
-
- /**
- * @param TransactionCurrency $fromCurrency
- * @param TransactionCurrency $toCurrency
- * @param Carbon $date
- *
- * @return CurrencyExchangeRate
- */
- public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate
- {
- $uri = sprintf('https://api.fixer.io/%s?base=%s&symbols=%s', $date->format('Y-m-d'), $fromCurrency->code, $toCurrency->code);
- $statusCode = -1;
- try {
- $result = Requests::get($uri);
- $statusCode = $result->status_code;
- $body = $result->body;
- } catch (Exception $e) {
- // don't care about error
- $body = sprintf('Requests_Exception: %s', $e->getMessage());
- }
- // Requests_Exception
- $rate = 1.0;
- $content = null;
- if (200 !== $statusCode) {
- Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
- }
- // get rate from body:
- if (200 === $statusCode) {
- $content = json_decode($body, true);
- }
- if (null !== $content) {
- $code = $toCurrency->code;
- $rate = $content['rates'][$code] ?? '1';
- }
-
- // create new currency exchange rate object:
- $exchangeRate = new CurrencyExchangeRate;
- $exchangeRate->user()->associate($this->user);
- $exchangeRate->fromCurrency()->associate($fromCurrency);
- $exchangeRate->toCurrency()->associate($toCurrency);
- $exchangeRate->date = $date;
- $exchangeRate->rate = $rate;
- $exchangeRate->save();
-
- return $exchangeRate;
- }
-
- /**
- * @param User $user
- *
- * @return mixed|void
- */
- public function setUser(User $user)
- {
- $this->user = $user;
- }
-}
diff --git a/app/Services/Currency/FixerIOv2.php b/app/Services/Currency/FixerIOv2.php
index 67d4a77c82..f44e917eb4 100644
--- a/app/Services/Currency/FixerIOv2.php
+++ b/app/Services/Currency/FixerIOv2.php
@@ -27,8 +27,9 @@ use Exception;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
use Log;
-use Requests;
/**
* Class FixerIOv2.
@@ -60,7 +61,9 @@ class FixerIOv2 implements ExchangeRateInterface
$apiKey = env('FIXER_API_KEY', '');
// if no API key, return unsaved exchange rate.
- if (\strlen($apiKey) === 0) {
+ if ('' === $apiKey) {
+ Log::warning('No fixer.IO API key, will not do conversion.');
+
return $exchangeRate;
}
@@ -72,31 +75,36 @@ class FixerIOv2 implements ExchangeRateInterface
$statusCode = -1;
Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
try {
- $result = Requests::get($uri);
- $statusCode = $result->status_code;
- $body = $result->body;
+ $client = new Client;
+ $res = $client->request('GET', $uri);
+ $statusCode = $res->getStatusCode();
+ $body = $res->getBody()->getContents();
Log::debug(sprintf('Result status code is %d', $statusCode));
- } catch (Exception $e) {
+ Log::debug(sprintf('Result body is: %s', $body));
+ } catch (GuzzleException|Exception $e) {
// don't care about error
- $body = sprintf('Requests_Exception: %s', $e->getMessage());
+ $body = sprintf('Guzzle exception: %s', $e->getMessage());
}
- // Requests_Exception
$content = null;
if (200 !== $statusCode) {
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
}
+ $success = false;
// get rate from body:
if (200 === $statusCode) {
$content = json_decode($body, true);
+ $success = $content['success'] ?? false;
}
- if (null !== $content) {
+ if (null !== $content && true === $success) {
$code = $toCurrency->code;
$rate = (float)($content['rates'][$code] ?? 0);
+ Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []);
}
- Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []);
+
$exchangeRate->rate = $rate;
if ($rate !== 0.0) {
+ Log::debug('Rate is not zero, save it!');
$exchangeRate->save();
}
diff --git a/app/Services/Github/Request/GithubRequest.php b/app/Services/Github/Request/GithubRequest.php
index 3e7a5ca5d6..fc3552a18c 100644
--- a/app/Services/Github/Request/GithubRequest.php
+++ b/app/Services/Github/Request/GithubRequest.php
@@ -29,6 +29,6 @@ namespace FireflyIII\Services\Github\Request;
*/
interface GithubRequest
{
- public function call();
+ public function call(): void;
}
diff --git a/app/Services/Github/Request/UpdateRequest.php b/app/Services/Github/Request/UpdateRequest.php
index 9fb0f212dd..fd2d173049 100644
--- a/app/Services/Github/Request/UpdateRequest.php
+++ b/app/Services/Github/Request/UpdateRequest.php
@@ -26,7 +26,9 @@ namespace FireflyIII\Services\Github\Request;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Services\Github\Object\Release;
-use Requests;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
+use Log;
use SimpleXMLElement;
/**
@@ -41,30 +43,34 @@ class UpdateRequest implements GithubRequest
*
* @throws FireflyException
*/
- public function call()
+ public function call(): void
{
$uri = 'https://github.com/firefly-iii/firefly-iii/releases.atom';
+ Log::debug(sprintf('Going to call %s', $uri));
try {
- $response = Requests::get($uri);
- } catch (Exception $e) {
+ $client = new Client();
+ $res = $client->request('GET', $uri);
+ } catch (GuzzleException|Exception $e) {
throw new FireflyException(sprintf('Response error from Github: %s', $e->getMessage()));
}
- if ($response->status_code !== 200) {
- throw new FireflyException(sprintf('Returned code %d, error: %s', $response->status_code, $response->body));
+ if ($res->getStatusCode() !== 200) {
+ throw new FireflyException(sprintf('Returned code %d, error: %s', $res->getStatusCode(), $res->getBody()->getContents()));
}
- $releaseXml = new SimpleXMLElement($response->body, LIBXML_NOCDATA);
+ $releaseXml = new SimpleXMLElement($res->getBody()->getContents(), LIBXML_NOCDATA);
//fetch the products for each category
if (isset($releaseXml->entry)) {
+ Log::debug(sprintf('Count of entries is: %d', \count($releaseXml->entry)));
foreach ($releaseXml->entry as $entry) {
- $array = [
+ $array = [
'id' => (string)$entry->id,
'updated' => (string)$entry->updated,
'title' => (string)$entry->title,
'content' => (string)$entry->content,
];
+ Log::debug(sprintf('Found version %s', $entry->title));
$this->releases[] = new Release($array);
}
}
diff --git a/app/Services/IP/IpifyOrg.php b/app/Services/IP/IpifyOrg.php
index 2e72e23c9c..0c0f8a3404 100644
--- a/app/Services/IP/IpifyOrg.php
+++ b/app/Services/IP/IpifyOrg.php
@@ -24,8 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Services\IP;
use Exception;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
use Log;
-use Requests;
/**
* Class IpifyOrg
@@ -42,19 +43,20 @@ class IpifyOrg implements IPRetrievalInterface
{
$result = null;
try {
- $response = Requests::get('https://api.ipify.org');
- } catch (Exception $e) {
+ $client = new Client;
+ $res = $client->request('GET', 'https://api.ipify.org');
+ } catch (GuzzleException|Exception $e) {
Log::warning(sprintf('The ipify.org service could not retrieve external IP: %s', $e->getMessage()));
Log::warning($e->getTraceAsString());
return null;
}
- if (200 !== $response->status_code) {
- Log::warning(sprintf('Could not retrieve external IP: %d %s', $response->status_code, $response->body));
+ if (200 !== $res->getStatusCode()) {
+ Log::warning(sprintf('Could not retrieve external IP: %d %s', $res->getStatusCode(), $res->getBody()->getContents()));
return null;
}
- return (string)$response->body;
+ return (string)$res->getBody()->getContents();
}
}
diff --git a/app/Services/Password/PwndVerifier.php b/app/Services/Password/PwndVerifier.php
deleted file mode 100644
index d5bc349253..0000000000
--- a/app/Services/Password/PwndVerifier.php
+++ /dev/null
@@ -1,56 +0,0 @@
-<?php
-/**
- * PwndVerifier.php
- * Copyright (c) 2017 [email protected]
- *
- * 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\Password;
-
-use Exception;
-use Log;
-use Requests;
-
-/**
- * Class PwndVerifier.
- */
-class PwndVerifier implements Verifier
-{
- /**
- * Verify the given password against (some) service.
- *
- * @param string $password
- *
- * @return bool
- */
- public function validPassword(string $password): bool
- {
- $hash = sha1($password);
- $uri = sprintf('https://haveibeenpwned.com/api/v2/pwnedpassword/%s', $hash);
- $opt = ['useragent' => 'Firefly III v' . config('firefly.version'), 'timeout' => 2];
-
- try {
- $result = Requests::get($uri, ['originalPasswordIsAHash' => 'true'], $opt);
- } catch (Exception $e) {
- return true;
- }
- Log::debug(sprintf('Status code returned is %d', $result->status_code));
-
- return 404 === $result->status_code;
- }
-}
diff --git a/app/Services/Password/PwndVerifierV2.php b/app/Services/Password/PwndVerifierV2.php
index bf3cd330cf..014fbbcbe6 100644
--- a/app/Services/Password/PwndVerifierV2.php
+++ b/app/Services/Password/PwndVerifierV2.php
@@ -23,8 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Services\Password;
use Exception;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
use Log;
-use Requests;
/**
* Class PwndVerifierV2.
@@ -44,27 +45,30 @@ class PwndVerifierV2 implements Verifier
$prefix = substr($hash, 0, 5);
$rest = substr($hash, 5);
$uri = sprintf('https://api.pwnedpasswords.com/range/%s', $prefix);
- $opt = ['useragent' => 'Firefly III v' . config('firefly.version'), 'timeout' => 2];
+ $opt = [
+ 'headers' => ['User-Agent' => 'Firefly III v' . config('firefly.version')],
+ 'timeout' => 2];
Log::debug(sprintf('hash prefix is %s', $prefix));
Log::debug(sprintf('rest is %s', $rest));
try {
- $result = Requests::get($uri, $opt);
- } catch (Exception $e) {
+ $client = new Client();
+ $res = $client->request('GET', $uri, $opt);
+ } catch (GuzzleException|Exception $e) {
return true;
}
- Log::debug(sprintf('Status code returned is %d', $result->status_code));
- if (404 === $result->status_code) {
+ Log::debug(sprintf('Status code returned is %d', $res->getStatusCode()));
+ if (404 === $res->getStatusCode()) {
return true;
}
- $strpos = stripos($result->body, $rest);
+ $strpos = stripos($res->getBody()->getContents(), $rest);
if ($strpos === false) {
Log::debug(sprintf('%s was not found in result body. Return true.', $rest));
return true;
}
- Log::debug('Could not find %s, return FALSE.');
+ Log::debug(sprintf('Could not find %s, return FALSE.', $rest));
return false;
}
diff --git a/app/Services/Spectre/Request/SpectreRequest.php b/app/Services/Spectre/Request/SpectreRequest.php
index 6b735de91b..1fafadf23c 100644
--- a/app/Services/Spectre/Request/SpectreRequest.php
+++ b/app/Services/Spectre/Request/SpectreRequest.php
@@ -25,9 +25,9 @@ namespace FireflyIII\Services\Spectre\Request;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\User;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
use Log;
-use Requests;
-use Requests_Response;
/**
* Class SpectreRequest
@@ -195,23 +195,24 @@ abstract class SpectreRequest
}
$headers = $this->getDefaultHeaders();
- $body = json_encode($data);
+ $sendBody = json_encode($data); // OK
$fullUri = $this->server . $uri;
- $signature = $this->generateSignature('get', $fullUri, $body);
+ $signature = $this->generateSignature('get', $fullUri, $sendBody);
$headers['Signature'] = $signature;
Log::debug('Final headers for spectre signed get request:', $headers);
try {
- $response = Requests::get($fullUri, $headers);
- } catch (Exception $e) {
- throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
+ $client = new Client;
+ $res = $client->request('GET', $fullUri, ['headers' => $headers]);
+ } catch (GuzzleException|Exception $e) {
+ throw new FireflyException(sprintf('Guzzle Exception: %s', $e->getMessage()));
}
- $this->detectError($response);
- $statusCode = (int)$response->status_code;
+ $statusCode = $res->getStatusCode();
+ $returnBody = $res->getBody()->getContents();
+ $this->detectError($returnBody, $statusCode);
- $body = $response->body;
- $array = json_decode($body, true);
- $responseHeaders = $response->headers->getAll();
+ $array = json_decode($returnBody, true);
+ $responseHeaders = $res->getHeaders();
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
@@ -220,6 +221,7 @@ abstract class SpectreRequest
throw new FireflyException(sprintf('Error of class %s: %s', $array['error_class'], $message));
}
+
return $array;
}
@@ -245,28 +247,32 @@ abstract class SpectreRequest
Log::debug('Final headers for spectre signed POST request:', $headers);
try {
- $response = Requests::post($fullUri, $headers, $body);
- } catch (Exception $e) {
+ $client = new Client;
+ $res = $client->request('POST', $fullUri, ['headers' => $headers, 'body' => $body]);
+ } catch (GuzzleException|Exception $e) {
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
}
- $this->detectError($response);
- $body = $response->body;
+ $body = $res->getBody()->getContents();
+ $statusCode = $res->getStatusCode();
+ $this->detectError($body, $statusCode);
+
$array = json_decode($body, true);
- $responseHeaders = $response->headers->getAll();
+ $responseHeaders = $res->getHeaders();
$array['ResponseHeaders'] = $responseHeaders;
- $array['ResponseStatusCode'] = $response->status_code;
+ $array['ResponseStatusCode'] = $statusCode;
return $array;
}
/**
- * @param Requests_Response $response
+ * @param string $body
+ *
+ * @param int $statusCode
*
* @throws FireflyException
*/
- private function detectError(Requests_Response $response): void
+ private function detectError(string $body, int $statusCode): void
{
- $body = $response->body;
$array = json_decode($body, true);
if (isset($array['error_class'])) {
$message = $array['error_message'] ?? '(no message)';
@@ -279,9 +285,8 @@ abstract class SpectreRequest
throw new FireflyException(sprintf('Error of class %s: %s', $errorClass, $message));
}
- $statusCode = (int)$response->status_code;
if (200 !== $statusCode) {
- throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $response->body));
+ throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $body));
}
}
}