Skip to content

Commit

Permalink
Add too many requests retry
Browse files Browse the repository at this point in the history
  • Loading branch information
marekskopal committed Feb 11, 2024
1 parent 21645e5 commit d7c6268
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 69 deletions.
7 changes: 1 addition & 6 deletions src/Api/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@

namespace MarekSkopal\TwelveData\Api;

use MarekSkopal\TwelveData\Client\Client;
use MarekSkopal\TwelveData\Dto\Advanced\ApiUsage;
use MarekSkopal\TwelveData\Enum\FormatEnum;

class Advanced extends TwelveDataApi
{
public function __construct(private readonly Client $client)
{
}

public function apiUsage(?FormatEnum $format = null, ?string $delimiter = null,): ApiUsage
{
$response = $this->client->get(
Expand All @@ -24,6 +19,6 @@ public function apiUsage(?FormatEnum $format = null, ?string $delimiter = null,)
],
);

return ApiUsage::fromJson($this->getResponseContents($response));
return ApiUsage::fromJson($response);
}
}
17 changes: 6 additions & 11 deletions src/Api/CoreData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MarekSkopal\TwelveData\Api;

use DateTimeImmutable;
use MarekSkopal\TwelveData\Client\Client;
use MarekSkopal\TwelveData\Dto\CoreData\CurrencyConversion;
use MarekSkopal\TwelveData\Dto\CoreData\EndOfDayPrice;
use MarekSkopal\TwelveData\Dto\CoreData\ExchangeRate;
Expand All @@ -21,10 +20,6 @@

class CoreData extends TwelveDataApi
{
public function __construct(private readonly Client $client)
{
}

/** @param list<AdjustEnum>|null $adjust */
public function timeSeries(
string $symbol,
Expand Down Expand Up @@ -70,7 +65,7 @@ public function timeSeries(
],
);

return TimeSeries::fromJson($this->getResponseContents($response));
return TimeSeries::fromJson($response);
}

public function exchangeRate(
Expand All @@ -93,7 +88,7 @@ public function exchangeRate(
],
);

return ExchangeRate::fromJson($this->getResponseContents($response));
return ExchangeRate::fromJson($response);
}

public function currencyConversion(
Expand All @@ -118,7 +113,7 @@ public function currencyConversion(
],
);

return CurrencyConversion::fromJson($this->getResponseContents($response));
return CurrencyConversion::fromJson($response);
}

public function quote(
Expand Down Expand Up @@ -157,7 +152,7 @@ public function quote(
],
);

return Quote::fromJson($this->getResponseContents($response));
return Quote::fromJson($response);
}

public function realTimePrice(
Expand Down Expand Up @@ -186,7 +181,7 @@ public function realTimePrice(
],
);

return RealTImePrice::fromJson($this->getResponseContents($response));
return RealTImePrice::fromJson($response);
}

public function endOfDayPrice(
Expand All @@ -211,6 +206,6 @@ public function endOfDayPrice(
],
);

return EndOfDayPrice::fromJson($this->getResponseContents($response));
return EndOfDayPrice::fromJson($response);
}
}
21 changes: 8 additions & 13 deletions src/Api/Fundamentals.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MarekSkopal\TwelveData\Api;

use DateTimeImmutable;
use MarekSkopal\TwelveData\Client\Client;
use MarekSkopal\TwelveData\Dto\Fundamentals\Dividends;
use MarekSkopal\TwelveData\Dto\Fundamentals\Earnings;
use MarekSkopal\TwelveData\Dto\Fundamentals\IncomeStatement;
Expand All @@ -21,10 +20,6 @@

class Fundamentals extends TwelveDataApi
{
public function __construct(private readonly Client $client)
{
}

public function logo(string $symbol, ?string $exchange = null, ?string $micCode = null, ?string $country = null,): Logo
{
$response = $this->client->get(
Expand All @@ -37,7 +32,7 @@ public function logo(string $symbol, ?string $exchange = null, ?string $micCode
],
);

return Logo::fromJson($this->getResponseContents($response));
return Logo::fromJson($response);
}

public function profile(string $symbol, ?string $exchange = null, ?string $micCode = null, ?string $country = null,): Profile
Expand All @@ -52,7 +47,7 @@ public function profile(string $symbol, ?string $exchange = null, ?string $micCo
],
);

return Profile::fromJson($this->getResponseContents($response));
return Profile::fromJson($response);
}

public function dividends(
Expand All @@ -77,7 +72,7 @@ public function dividends(
],
);

return Dividends::fromJson($this->getResponseContents($response));
return Dividends::fromJson($response);
}

public function splits(
Expand All @@ -102,7 +97,7 @@ public function splits(
],
);

return Splits::fromJson($this->getResponseContents($response));
return Splits::fromJson($response);
}

public function earnings(
Expand Down Expand Up @@ -137,7 +132,7 @@ public function earnings(
],
);

return Earnings::fromJson($this->getResponseContents($response));
return Earnings::fromJson($response);
}

public function statistics(string $symbol, ?string $exchange = null, ?string $micCode = null, ?string $country = null,): Statistics
Expand All @@ -152,7 +147,7 @@ public function statistics(string $symbol, ?string $exchange = null, ?string $mi
],
);

return Statistics::fromJson($this->getResponseContents($response));
return Statistics::fromJson($response);
}

public function insiderTransactions(
Expand All @@ -171,7 +166,7 @@ public function insiderTransactions(
],
);

return InsiderTransactions::fromJson($this->getResponseContents($response));
return InsiderTransactions::fromJson($response);
}

public function incomeStatement(
Expand All @@ -196,6 +191,6 @@ public function incomeStatement(
],
);

return IncomeStatement::fromJson($this->getResponseContents($response));
return IncomeStatement::fromJson($response);
}
}
29 changes: 12 additions & 17 deletions src/Api/ReferenceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MarekSkopal\TwelveData\Api;

use MarekSkopal\TwelveData\Client\Client;
use MarekSkopal\TwelveData\Dto\ReferenceData\BondsList;
use MarekSkopal\TwelveData\Dto\ReferenceData\CryptocurrenciesList;
use MarekSkopal\TwelveData\Dto\ReferenceData\CryptocurrencyExchanges;
Expand All @@ -23,10 +22,6 @@

class ReferenceData extends TwelveDataApi
{
public function __construct(private readonly Client $client)
{
}

public function stockList(
?string $symbol = null,
?string $exchange = null,
Expand All @@ -53,7 +48,7 @@ public function stockList(
],
);

return StockList::fromJson($this->getResponseContents($response));
return StockList::fromJson($response);
}

public function forexPairsList(
Expand All @@ -74,7 +69,7 @@ public function forexPairsList(
],
);

return ForexPairsList::fromJson($this->getResponseContents($response));
return ForexPairsList::fromJson($response);
}

public function cryptocurrenciesList(
Expand All @@ -97,7 +92,7 @@ public function cryptocurrenciesList(
],
);

return CryptocurrenciesList::fromJson($this->getResponseContents($response));
return CryptocurrenciesList::fromJson($response);
}

public function etfList(
Expand All @@ -122,7 +117,7 @@ public function etfList(
],
);

return EtfList::fromJson($this->getResponseContents($response));
return EtfList::fromJson($response);
}

public function indicesList(
Expand All @@ -147,7 +142,7 @@ public function indicesList(
],
);

return IndicesList::fromJson($this->getResponseContents($response));
return IndicesList::fromJson($response);
}

public function fundsList(
Expand Down Expand Up @@ -176,7 +171,7 @@ public function fundsList(
],
);

return FundsList::fromJson($this->getResponseContents($response));
return FundsList::fromJson($response);
}

public function bondsList(
Expand Down Expand Up @@ -205,7 +200,7 @@ public function bondsList(
],
);

return BondsList::fromJson($this->getResponseContents($response));
return BondsList::fromJson($response);
}

public function exchanges(
Expand All @@ -228,7 +223,7 @@ public function exchanges(
],
);

return Exchanges::fromJson($this->getResponseContents($response));
return Exchanges::fromJson($response);
}

public function cryptocurrencyExchanges(?FormatEnum $format = null, ?string $delimiter = null,): CryptocurrencyExchanges
Expand All @@ -241,7 +236,7 @@ public function cryptocurrencyExchanges(?FormatEnum $format = null, ?string $del
],
);

return CryptocurrencyExchanges::fromJson($this->getResponseContents($response));
return CryptocurrencyExchanges::fromJson($response);
}

public function symbolSearch(string $symbol, ?int $outputsize = null,): SymbolSearch
Expand All @@ -254,7 +249,7 @@ public function symbolSearch(string $symbol, ?int $outputsize = null,): SymbolSe
],
);

return SymbolSearch::fromJson($this->getResponseContents($response));
return SymbolSearch::fromJson($response);
}

public function earliestTimestamp(
Expand All @@ -275,7 +270,7 @@ public function earliestTimestamp(
],
);

return EarliestTimestamp::fromJson($this->getResponseContents($response));
return EarliestTimestamp::fromJson($response);
}

/** @return list<MarketState> */
Expand All @@ -290,7 +285,7 @@ public function marketState(?string $exchange = null, ?string $code = null, ?str
],
);

$responseContents = $this->getResponseContents($response);
$responseContents = $response;

/**
* @var list<array{
Expand Down
17 changes: 2 additions & 15 deletions src/Api/TwelveDataApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@

namespace MarekSkopal\TwelveData\Api;

use MarekSkopal\TwelveData\Exception\ApiException;
use Psr\Http\Message\ResponseInterface;
use MarekSkopal\TwelveData\Client\Client;

abstract class TwelveDataApi
{
protected function getResponseContents(ResponseInterface $response): string
public function __construct(protected readonly Client $client)
{
$responseContents = $response->getBody()->getContents();

/** @var array{status?: string, code?: int, message?: string} $data */
$data = json_decode($responseContents, associative: true);

$status = $data['status'] ?? null;

if ($status === 'error') {
throw ApiException::fromCode($data['message'] ?? 'Internal Server Error', $data['code'] ?? 500);
}

return $responseContents;
}
}
Loading

0 comments on commit d7c6268

Please sign in to comment.