Skip to content

Commit

Permalink
Fix getting error as failed to fetch api
Browse files Browse the repository at this point in the history
Fix/getting error as failed to fetch api
  • Loading branch information
mgcodeur authored Apr 29, 2024
2 parents cf6fe7e + a3f8d14 commit bb86043
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
34 changes: 27 additions & 7 deletions src/Services/CurrencyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Mgcodeur\CurrencyConverter\Exceptions\NetworkException;

class CurrencyService
{
private string $baseUrl = 'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies';
private string $baseUrl = 'https://latest.currency-api.pages.dev/v1/currencies';

public function convertAllCurrency($amount, $from, $result, $format = false): array
public function convertAllCurrency($amount, array $result, bool $format = false): array
{
$converted = [];

if (array_key_exists($from, $result) && ! empty($result[$from])) {
foreach ($result[$from] as $currency => $value) {
if (! empty($result)) {
foreach ($result as $currency => $value) {
if ($format) {
$converted[$currency] = number_format(
num: $value * $amount,
Expand All @@ -33,11 +34,30 @@ public function convertAllCurrency($amount, $from, $result, $format = false): ar

public function fetchAllCurrencies(): Response
{
return Http::get($this->baseUrl.'.json');
return Http::withoutVerifying()->get($this->baseUrl.'.json');
}

public function runConversionFrom(string $from, ?string $to = ''): Response
public function runConversionFrom(string $from, ?string $to = '')
{
return $to ? Http::get($this->baseUrl."/{$from}/{$to}.json") : Http::get($this->baseUrl."/{$from}.json");
$response = Http::withoutVerifying()
->get($this->baseUrl."/{$from}.json");

if ($response->failed()) {
throw new NetworkException();
}

$datas = $response->json();

if ($to && array_key_exists($from, $datas)) {
$datas = $datas[$from][$to];
}

if (is_numeric($datas)) {
$datas = [
$to => $datas,
];
}

return $datas;
}
}
13 changes: 3 additions & 10 deletions src/Traits/CurrencyConverterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,23 @@ trait CurrencyConverterManager
/**
* @throws NetworkException | MissingAmountException | MissingCurrencyException
*/
public function get($format = false): float|int|array|string
public function get($format = false)
{
$this->verifyDataBeforeGettingResults();

if ($this->currencies) {
return $this->currencies;
}

$response = $this->currencyService->runConversionFrom(
$result = $this->currencyService->runConversionFrom(
from: $this->from,
to: $this->to
);

if ($response->failed() || ! $response->json()) {
throw new NetworkException();
}

$result = $response->json();

if (! $this->to) {
return $this->currencyService->convertAllCurrency(
amount: $this->amount,
from: $this->from,
result: $result,
result: $result[$this->from],
format: $format
);
}
Expand Down
17 changes: 10 additions & 7 deletions tests/Unit/CurrencyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@
test('convert currency from USD to all supported currencies', function () {
// example of value of 1 USD in [eur, mga, btc]
$dataToMock = [
'eur' => 0.92326769,
'mga' => 4511.5880329,
'btc' => 0.0000229836,
'usd' => [
'eur' => 0.92326769,
'mga' => 4511.5880329,
'btc' => 0.0000229836,
],
];

$amountToConvert = 100;

$convertedAmount = array_map(fn ($value) => $value * $amountToConvert, $dataToMock);
$convertedAmount = array_map(fn ($value) => $value * $amountToConvert, $dataToMock['usd']);

$mockedResponse = new \Illuminate\Http\Client\Response(
$response = new \Illuminate\Http\Client\Response(
new \GuzzleHttp\Psr7\Response(200, [], json_encode($dataToMock))
);

$mockedResponse = $response->json();

$this->currencyServiceMock
->shouldReceive('runConversionFrom')
->with('usd', Mockery::any())
Expand All @@ -95,8 +99,7 @@
->shouldReceive('convertAllCurrency')
->with(
100,
'usd',
$dataToMock,
$dataToMock['usd'],
false
)
->andReturn($convertedAmount);
Expand Down

0 comments on commit bb86043

Please sign in to comment.