-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor config and add handler stack provider
- Loading branch information
Showing
7 changed files
with
176 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Signifly\Shopify\Laravel\Providers; | ||
|
||
use GuzzleHttp\HandlerStack; | ||
use Concat\Http\Middleware\RateLimiter; | ||
use Concat\Http\Middleware\RateLimitProvider; | ||
use Signifly\Shopify\RateLimit\DefaultRateLimitCalculator; | ||
|
||
class DefaultHandlerStackProvider implements HandlerStackProviderContract | ||
{ | ||
public function getHandlerStack() : HandlerStack | ||
{ | ||
$handlerStack = HandlerStack::create(); | ||
$handlerStack->push(new RateLimiter($this->getRateLimitProvider())); | ||
return $handlerStack; | ||
} | ||
|
||
protected function getRateLimitProvider() : RateLimitProvider | ||
{ | ||
return new DefaultRateLimitProvider( | ||
new DefaultRateLimitCalculator( | ||
config('shopify.rate_limit.buffer'), | ||
config('shopify.rate_limit.cycle'), | ||
config('shopify.rate_limit.processes') | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Signifly\Shopify\Laravel\Providers; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Concat\Http\Middleware\RateLimitProvider; | ||
|
||
class DefaultRateLimitProvider implements RateLimitProvider | ||
{ | ||
protected $calculator; | ||
|
||
public function __construct(RateLimitCalculatorContract $calculator) | ||
{ | ||
$this->calculator = $calculator; | ||
} | ||
|
||
/** | ||
* Returns when the last request was made. | ||
* | ||
* @return float|null When the last request was made. | ||
*/ | ||
public function getLastRequestTime() | ||
{ | ||
return Cache::get('last_request_time'); | ||
} | ||
|
||
/** | ||
* Used to set the current time as the last request time to be queried when | ||
* the next request is attempted. | ||
*/ | ||
public function setLastRequestTime() | ||
{ | ||
return Cache::forever('last_request_time', microtime(true)); | ||
} | ||
|
||
/** | ||
* Returns what is considered the time when a given request is being made. | ||
* | ||
* @param RequestInterface $request The request being made. | ||
* | ||
* @return float Time when the given request is being made. | ||
*/ | ||
public function getRequestTime(RequestInterface $request) | ||
{ | ||
return microtime(true); | ||
} | ||
|
||
/** | ||
* Returns the minimum amount of time that is required to have passed since | ||
* the last request was made. This value is used to determine if the current | ||
* request should be delayed, based on when the last request was made. | ||
* | ||
* Returns the allowed time between the last request and the next, which | ||
* is used to determine if a request should be delayed and by how much. | ||
* | ||
* @param RequestInterface $request The pending request. | ||
* | ||
* @return float The minimum amount of time that is required to have passed | ||
* since the last request was made (in seconds). | ||
*/ | ||
public function getRequestAllowance(RequestInterface $request) | ||
{ | ||
return Cache::get('request_allowance', 0.5); | ||
} | ||
|
||
/** | ||
* Used to set the minimum amount of time that is required to pass between | ||
* this request and the next request. | ||
* | ||
* @param ResponseInterface $response The resolved response. | ||
*/ | ||
public function setRequestAllowance(ResponseInterface $response) | ||
{ | ||
Cache::forever('request_allowance', $this->calculator->calculate()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Signifly\Shopify\Laravel\Providers; | ||
|
||
use GuzzleHttp\HandlerStack; | ||
use Concat\Http\Middleware\RateLimitProvider; | ||
|
||
interface HandlerStackProviderContract | ||
{ | ||
public function getHandlerStack() : HandlerStack; | ||
|
||
protected function getRateLimitProvider() : RateLimitProvider; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters