Skip to content

Commit

Permalink
add automatic resend request
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid74 committed Feb 4, 2022
1 parent 6036874 commit cbe906f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,7 +1,7 @@
# (English) Wildberries REST API statistics client library with throttling requests
## Русское описание ниже, после английского

A simple Wildberries REST API statistics client library with throttling requests (for example, no more than 10 requests per second according to API rules) and an example for PHP.
A simple Wildberries REST API statistics client library with throttling requests (for example, no more than 10 requests per second according to API rules) and an example for PHP. Automatic request resending is supported when the http response code "429: too many requests" is received.

Statistics API Documentation [Wildberries REST API statistics Documentation](https://images.wbstatic.net/portal/education/Kak_rabotat'_s_servisom_statistiki.pdf)

Expand Down Expand Up @@ -84,7 +84,7 @@ $incomes = $WbApiClient->incomes();

# (Russian) Клиентская REST API библиотека статистики Wildberries с регулированием запросов

Простая клиентская REST API библиотека статистики Wildberries с регулированием запросов (например, не более 10 запросов в секунду в соответствии с правилами API) и примером для PHP.
Простая клиентская REST API библиотека статистики Wildberries с регулированием запросов (например, не более 10 запросов в секунду в соответствии с правилами API) и примером для PHP. Поддерживается автоматическая повторная отправка запроса при получении http кода ответа "429: too many requests".

Описание API статистики [Wildberries REST API statistics](https://images.wbstatic.net/portal/education/Kak_rabotat'_s_servisom_statistiki.pdf)

Expand Down
11 changes: 9 additions & 2 deletions src/WbApiClient.php
Expand Up @@ -311,9 +311,16 @@ private function throttleCurl()
usleep( $usleep );
} while ( false );

$this->lastRequestTime = microtime( true );
do {
$this->lastRequestTime = microtime( true );
$response = curl_exec( $this->curl );

$response = curl_exec( $this->curl );
$oneMoreTry = curl_getinfo( $this->curl, CURLINFO_RESPONSE_CODE ) == 429;
if ( $oneMoreTry ) {
$this->debug( "[{$this->requestCounter}] +++++ TOO MANY REQUESTS, WAITING 0.5sec +++++", self::DEBUG_URL );
usleep( 500000 );
}
} while ( $oneMoreTry );

return $response;
}
Expand Down

0 comments on commit cbe906f

Please sign in to comment.