Skip to content

Commit

Permalink
Fix usage of sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
joecampo committed May 5, 2022
1 parent 6c33a58 commit a392dcd
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/ShipStation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace LaravelShipStation;

use GuzzleHttp\Client;
Expand Down Expand Up @@ -31,7 +32,7 @@ class ShipStation
'/stores/',
'/users/',
'/warehouses/',
'/webhooks/'
'/webhooks/',
];

/**
Expand Down Expand Up @@ -59,14 +60,14 @@ class ShipStation
*/
public function __construct($apiKey, $apiSecret, $apiURL, $partnerApiKey = null)
{
if (!isset($apiKey, $apiSecret)) {
if (! isset($apiKey, $apiSecret)) {
throw new \Exception('Your API key and/or private key are not set. Did you run artisan vendor:publish?');
}

$this->base_uri = $apiURL;

$headers = [
'Authorization' => 'Basic ' . base64_encode("{$apiKey}:{$apiSecret}"),
'Authorization' => 'Basic '.base64_encode("{$apiKey}:{$apiSecret}"),
];

if (! empty($partnerApiKey)) {
Expand Down Expand Up @@ -143,7 +144,7 @@ public function update($options = [], $endpoint = '')
}

/**
* Get the maximum number of requests that can be sent per window
* Get the maximum number of requests that can be sent per window.
*
* @return int
*/
Expand All @@ -153,7 +154,7 @@ public function getMaxAllowedRequests()
}

/**
* Get the remaining number of requests that can be sent in the current window
* Get the remaining number of requests that can be sent in the current window.
*
* @return int
*/
Expand All @@ -163,7 +164,7 @@ public function getRemainingRequests()
}

/**
* Get the number of seconds remaining until the next window begins
* Get the number of seconds remaining until the next window begins.
*
* @return int
*/
Expand All @@ -174,7 +175,7 @@ public function getSecondsUntilReset()

/**
* Are we currently rate limited?
* We are if there are no more requests allowed in the current window
* We are if there are no more requests allowed in the current window.
*
* @return bool
*/
Expand All @@ -195,7 +196,7 @@ public function sleepIfRateLimited(Response $response)
$this->secondsUntilReset = (int) $response->getHeader('X-Rate-Limit-Reset')[0];

if ($this->isRateLimited() || ($this->secondsUntilReset / $this->remainingRequests) > 1.5) {
sleep(1.5);
usleep(1500000);
}
}

Expand All @@ -207,11 +208,11 @@ public function sleepIfRateLimited(Response $response)
*/
public function __get($property)
{
if (in_array('/' . $property . '/', $this->endpoints)) {
$this->endpoint = '/' . $property . '/';
if (in_array('/'.$property.'/', $this->endpoints)) {
$this->endpoint = '/'.$property.'/';
}

$className = "LaravelShipStation\\Helpers\\" . ucfirst($property);
$className = 'LaravelShipStation\\Helpers\\'.ucfirst($property);

if (class_exists($className)) {
return new $className($this);
Expand Down

0 comments on commit a392dcd

Please sign in to comment.