|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodeCloud\Bundle\ShopifyBundle\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\HandlerStack; |
|
7
|
|
|
use GuzzleHttp\Middleware; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Creates authenticated clients for public and private apps. |
|
14
|
|
|
*/ |
|
15
|
|
|
class HttpClientFactory implements HttpClientFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
public function createHttpClient($storeName, $credentials) |
|
18
|
|
|
{ |
|
19
|
|
|
$handlers = HandlerStack::create(); |
|
20
|
|
|
$handlers->push(Middleware::retry( |
|
21
|
|
|
function ($retries, RequestInterface $request, ResponseInterface $response = null, \Exception $error = null) { |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
// todo rate limit by this |
|
24
|
|
|
//$response->getHeaderLine('X-Shopify-Shop-Api-Call-Limit'); |
|
25
|
|
|
if ($response && $response->getStatusCode() == Response::HTTP_TOO_MANY_REQUESTS) { |
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return false; |
|
30
|
|
|
}, |
|
31
|
|
|
function ($retries, ResponseInterface $response) { |
|
32
|
|
|
if (!$response->hasHeader('Retry-After')) { |
|
33
|
|
|
return 1000; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
dump((float) $response->getHeaderLine('Retry-After') * 1000); |
|
|
|
|
|
|
37
|
|
|
die(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
return (float) $response->getHeaderLine('Retry-After') * 1000; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
)); |
|
42
|
|
|
|
|
43
|
|
|
$options = [ |
|
44
|
|
|
'base_uri' => 'https://' . $storeName, |
|
45
|
|
|
//'handler' => $handlers, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
switch (true) { |
|
49
|
|
|
case $credentials instanceof PublicAppCredentials: |
|
50
|
|
|
$options['headers'] = [ |
|
51
|
|
|
'X-Shopify-Access-Token' => $credentials->getAccessToken(), |
|
52
|
|
|
]; |
|
53
|
|
|
break; |
|
54
|
|
|
case $credentials instanceof PrivateAppCredentials: |
|
55
|
|
|
$options['auth'] = [ |
|
56
|
|
|
$credentials->getApiKey(), |
|
57
|
|
|
$credentials->getPassword(), |
|
58
|
|
|
]; |
|
59
|
|
|
break; |
|
60
|
|
|
default: |
|
61
|
|
|
throw new \RuntimeException('Invalid credentials given'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new Client($options); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.