mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Use Guzzle, not Requests library.
This commit is contained in:
parent
073dedd483
commit
20044427b4
@ -24,9 +24,10 @@ namespace FireflyIII\Helpers\Help;
|
|||||||
|
|
||||||
use Cache;
|
use Cache;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use League\CommonMark\CommonMarkConverter;
|
use League\CommonMark\CommonMarkConverter;
|
||||||
use Log;
|
use Log;
|
||||||
use Requests;
|
|
||||||
use Route;
|
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);
|
$uri = sprintf('https://raw.githubusercontent.com/firefly-iii/help/master/%s/%s.md', $language, $route);
|
||||||
Log::debug(sprintf('Trying to get %s...', $uri));
|
Log::debug(sprintf('Trying to get %s...', $uri));
|
||||||
$opt = ['useragent' => $this->userAgent];
|
$opt = ['headers' => ['User-Agent' => $this->userAgent]];
|
||||||
$content = '';
|
$content = '';
|
||||||
try {
|
try {
|
||||||
$result = Requests::get($uri, [], $opt);
|
$client = new Client;
|
||||||
} catch (Exception $e) {
|
$res = $client->request('GET', $uri, $opt);
|
||||||
|
} catch (GuzzleException|Exception $e) {
|
||||||
Log::error($e);
|
Log::error($e);
|
||||||
|
|
||||||
return '';
|
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) {
|
if (200 === $res->getStatusCode()) {
|
||||||
$content = trim($result->body);
|
$content = trim($res->getBody()->getContents());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\strlen($content) > 0) {
|
if (\strlen($content) > 0) {
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* FixerIO.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\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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,8 +27,9 @@ use Exception;
|
|||||||
use FireflyIII\Models\CurrencyExchangeRate;
|
use FireflyIII\Models\CurrencyExchangeRate;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Log;
|
use Log;
|
||||||
use Requests;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FixerIOv2.
|
* Class FixerIOv2.
|
||||||
@ -60,7 +61,9 @@ class FixerIOv2 implements ExchangeRateInterface
|
|||||||
$apiKey = env('FIXER_API_KEY', '');
|
$apiKey = env('FIXER_API_KEY', '');
|
||||||
|
|
||||||
// if no API key, return unsaved exchange rate.
|
// 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;
|
return $exchangeRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,31 +75,36 @@ class FixerIOv2 implements ExchangeRateInterface
|
|||||||
$statusCode = -1;
|
$statusCode = -1;
|
||||||
Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
|
Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
|
||||||
try {
|
try {
|
||||||
$result = Requests::get($uri);
|
$client = new Client;
|
||||||
$statusCode = $result->status_code;
|
$res = $client->request('GET', $uri);
|
||||||
$body = $result->body;
|
$statusCode = $res->getStatusCode();
|
||||||
|
$body = $res->getBody()->getContents();
|
||||||
Log::debug(sprintf('Result status code is %d', $statusCode));
|
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
|
// don't care about error
|
||||||
$body = sprintf('Requests_Exception: %s', $e->getMessage());
|
$body = sprintf('Guzzle exception: %s', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Requests_Exception
|
|
||||||
$content = null;
|
$content = null;
|
||||||
if (200 !== $statusCode) {
|
if (200 !== $statusCode) {
|
||||||
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
|
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
|
||||||
}
|
}
|
||||||
|
$success = false;
|
||||||
// get rate from body:
|
// get rate from body:
|
||||||
if (200 === $statusCode) {
|
if (200 === $statusCode) {
|
||||||
$content = json_decode($body, true);
|
$content = json_decode($body, true);
|
||||||
|
$success = $content['success'] ?? false;
|
||||||
}
|
}
|
||||||
if (null !== $content) {
|
if (null !== $content && true === $success) {
|
||||||
$code = $toCurrency->code;
|
$code = $toCurrency->code;
|
||||||
$rate = (float)($content['rates'][$code] ?? 0);
|
$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;
|
$exchangeRate->rate = $rate;
|
||||||
if ($rate !== 0.0) {
|
if ($rate !== 0.0) {
|
||||||
|
Log::debug('Rate is not zero, save it!');
|
||||||
$exchangeRate->save();
|
$exchangeRate->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +29,6 @@ namespace FireflyIII\Services\Github\Request;
|
|||||||
*/
|
*/
|
||||||
interface GithubRequest
|
interface GithubRequest
|
||||||
{
|
{
|
||||||
public function call();
|
public function call(): void;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,9 @@ namespace FireflyIII\Services\Github\Request;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Services\Github\Object\Release;
|
use FireflyIII\Services\Github\Object\Release;
|
||||||
use Requests;
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
|
use Log;
|
||||||
use SimpleXMLElement;
|
use SimpleXMLElement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,30 +43,34 @@ class UpdateRequest implements GithubRequest
|
|||||||
*
|
*
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function call()
|
public function call(): void
|
||||||
{
|
{
|
||||||
$uri = 'https://github.com/firefly-iii/firefly-iii/releases.atom';
|
$uri = 'https://github.com/firefly-iii/firefly-iii/releases.atom';
|
||||||
|
Log::debug(sprintf('Going to call %s', $uri));
|
||||||
try {
|
try {
|
||||||
$response = Requests::get($uri);
|
$client = new Client();
|
||||||
} catch (Exception $e) {
|
$res = $client->request('GET', $uri);
|
||||||
|
} catch (GuzzleException|Exception $e) {
|
||||||
throw new FireflyException(sprintf('Response error from Github: %s', $e->getMessage()));
|
throw new FireflyException(sprintf('Response error from Github: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response->status_code !== 200) {
|
if ($res->getStatusCode() !== 200) {
|
||||||
throw new FireflyException(sprintf('Returned code %d, error: %s', $response->status_code, $response->body));
|
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
|
//fetch the products for each category
|
||||||
if (isset($releaseXml->entry)) {
|
if (isset($releaseXml->entry)) {
|
||||||
|
Log::debug(sprintf('Count of entries is: %d', \count($releaseXml->entry)));
|
||||||
foreach ($releaseXml->entry as $entry) {
|
foreach ($releaseXml->entry as $entry) {
|
||||||
$array = [
|
$array = [
|
||||||
'id' => (string)$entry->id,
|
'id' => (string)$entry->id,
|
||||||
'updated' => (string)$entry->updated,
|
'updated' => (string)$entry->updated,
|
||||||
'title' => (string)$entry->title,
|
'title' => (string)$entry->title,
|
||||||
'content' => (string)$entry->content,
|
'content' => (string)$entry->content,
|
||||||
];
|
];
|
||||||
|
Log::debug(sprintf('Found version %s', $entry->title));
|
||||||
$this->releases[] = new Release($array);
|
$this->releases[] = new Release($array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,9 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Services\IP;
|
namespace FireflyIII\Services\IP;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Log;
|
use Log;
|
||||||
use Requests;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IpifyOrg
|
* Class IpifyOrg
|
||||||
@ -42,19 +43,20 @@ class IpifyOrg implements IPRetrievalInterface
|
|||||||
{
|
{
|
||||||
$result = null;
|
$result = null;
|
||||||
try {
|
try {
|
||||||
$response = Requests::get('https://api.ipify.org');
|
$client = new Client;
|
||||||
} catch (Exception $e) {
|
$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(sprintf('The ipify.org service could not retrieve external IP: %s', $e->getMessage()));
|
||||||
Log::warning($e->getTraceAsString());
|
Log::warning($e->getTraceAsString());
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (200 !== $response->status_code) {
|
if (200 !== $res->getStatusCode()) {
|
||||||
Log::warning(sprintf('Could not retrieve external IP: %d %s', $response->status_code, $response->body));
|
Log::warning(sprintf('Could not retrieve external IP: %d %s', $res->getStatusCode(), $res->getBody()->getContents()));
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string)$response->body;
|
return (string)$res->getBody()->getContents();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* PwndVerifier.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\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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,8 +23,9 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Services\Password;
|
namespace FireflyIII\Services\Password;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Log;
|
use Log;
|
||||||
use Requests;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PwndVerifierV2.
|
* Class PwndVerifierV2.
|
||||||
@ -44,27 +45,30 @@ class PwndVerifierV2 implements Verifier
|
|||||||
$prefix = substr($hash, 0, 5);
|
$prefix = substr($hash, 0, 5);
|
||||||
$rest = substr($hash, 5);
|
$rest = substr($hash, 5);
|
||||||
$uri = sprintf('https://api.pwnedpasswords.com/range/%s', $prefix);
|
$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('hash prefix is %s', $prefix));
|
||||||
Log::debug(sprintf('rest is %s', $rest));
|
Log::debug(sprintf('rest is %s', $rest));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = Requests::get($uri, $opt);
|
$client = new Client();
|
||||||
} catch (Exception $e) {
|
$res = $client->request('GET', $uri, $opt);
|
||||||
|
} catch (GuzzleException|Exception $e) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Status code returned is %d', $result->status_code));
|
Log::debug(sprintf('Status code returned is %d', $res->getStatusCode()));
|
||||||
if (404 === $result->status_code) {
|
if (404 === $res->getStatusCode()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$strpos = stripos($result->body, $rest);
|
$strpos = stripos($res->getBody()->getContents(), $rest);
|
||||||
if ($strpos === false) {
|
if ($strpos === false) {
|
||||||
Log::debug(sprintf('%s was not found in result body. Return true.', $rest));
|
Log::debug(sprintf('%s was not found in result body. Return true.', $rest));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log::debug('Could not find %s, return FALSE.');
|
Log::debug(sprintf('Could not find %s, return FALSE.', $rest));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,9 @@ namespace FireflyIII\Services\Spectre\Request;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Log;
|
use Log;
|
||||||
use Requests;
|
|
||||||
use Requests_Response;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SpectreRequest
|
* Class SpectreRequest
|
||||||
@ -195,23 +195,24 @@ abstract class SpectreRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
$headers = $this->getDefaultHeaders();
|
$headers = $this->getDefaultHeaders();
|
||||||
$body = json_encode($data);
|
$sendBody = json_encode($data); // OK
|
||||||
$fullUri = $this->server . $uri;
|
$fullUri = $this->server . $uri;
|
||||||
$signature = $this->generateSignature('get', $fullUri, $body);
|
$signature = $this->generateSignature('get', $fullUri, $sendBody);
|
||||||
$headers['Signature'] = $signature;
|
$headers['Signature'] = $signature;
|
||||||
|
|
||||||
Log::debug('Final headers for spectre signed get request:', $headers);
|
Log::debug('Final headers for spectre signed get request:', $headers);
|
||||||
try {
|
try {
|
||||||
$response = Requests::get($fullUri, $headers);
|
$client = new Client;
|
||||||
} catch (Exception $e) {
|
$res = $client->request('GET', $fullUri, ['headers' => $headers]);
|
||||||
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
|
} catch (GuzzleException|Exception $e) {
|
||||||
|
throw new FireflyException(sprintf('Guzzle Exception: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
$this->detectError($response);
|
$statusCode = $res->getStatusCode();
|
||||||
$statusCode = (int)$response->status_code;
|
$returnBody = $res->getBody()->getContents();
|
||||||
|
$this->detectError($returnBody, $statusCode);
|
||||||
|
|
||||||
$body = $response->body;
|
$array = json_decode($returnBody, true);
|
||||||
$array = json_decode($body, true);
|
$responseHeaders = $res->getHeaders();
|
||||||
$responseHeaders = $response->headers->getAll();
|
|
||||||
$array['ResponseHeaders'] = $responseHeaders;
|
$array['ResponseHeaders'] = $responseHeaders;
|
||||||
$array['ResponseStatusCode'] = $statusCode;
|
$array['ResponseStatusCode'] = $statusCode;
|
||||||
|
|
||||||
@ -220,6 +221,7 @@ abstract class SpectreRequest
|
|||||||
throw new FireflyException(sprintf('Error of class %s: %s', $array['error_class'], $message));
|
throw new FireflyException(sprintf('Error of class %s: %s', $array['error_class'], $message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,28 +247,32 @@ abstract class SpectreRequest
|
|||||||
|
|
||||||
Log::debug('Final headers for spectre signed POST request:', $headers);
|
Log::debug('Final headers for spectre signed POST request:', $headers);
|
||||||
try {
|
try {
|
||||||
$response = Requests::post($fullUri, $headers, $body);
|
$client = new Client;
|
||||||
} catch (Exception $e) {
|
$res = $client->request('POST', $fullUri, ['headers' => $headers, 'body' => $body]);
|
||||||
|
} catch (GuzzleException|Exception $e) {
|
||||||
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
|
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
$this->detectError($response);
|
$body = $res->getBody()->getContents();
|
||||||
$body = $response->body;
|
$statusCode = $res->getStatusCode();
|
||||||
|
$this->detectError($body, $statusCode);
|
||||||
|
|
||||||
$array = json_decode($body, true);
|
$array = json_decode($body, true);
|
||||||
$responseHeaders = $response->headers->getAll();
|
$responseHeaders = $res->getHeaders();
|
||||||
$array['ResponseHeaders'] = $responseHeaders;
|
$array['ResponseHeaders'] = $responseHeaders;
|
||||||
$array['ResponseStatusCode'] = $response->status_code;
|
$array['ResponseStatusCode'] = $statusCode;
|
||||||
|
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Requests_Response $response
|
* @param string $body
|
||||||
|
*
|
||||||
|
* @param int $statusCode
|
||||||
*
|
*
|
||||||
* @throws FireflyException
|
* @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);
|
$array = json_decode($body, true);
|
||||||
if (isset($array['error_class'])) {
|
if (isset($array['error_class'])) {
|
||||||
$message = $array['error_message'] ?? '(no message)';
|
$message = $array['error_message'] ?? '(no message)';
|
||||||
@ -279,9 +285,8 @@ abstract class SpectreRequest
|
|||||||
throw new FireflyException(sprintf('Error of class %s: %s', $errorClass, $message));
|
throw new FireflyException(sprintf('Error of class %s: %s', $errorClass, $message));
|
||||||
}
|
}
|
||||||
|
|
||||||
$statusCode = (int)$response->status_code;
|
|
||||||
if (200 !== $statusCode) {
|
if (200 !== $statusCode) {
|
||||||
throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $response->body));
|
throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $body));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user