Client.php
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Lackoxygen\TopWarehouse;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use Lackoxygen\TopWarehouse\Contracts\ClientInterface;
use Lackoxygen\TopWarehouse\Contracts\RequestInterface;
use GuzzleHttp\Client as guzzleClient;
use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use Lackoxygen\TopWarehouse\Exception\Exception;
use Lackoxygen\TopWarehouse\Utils\SignatureUtil;
class Client implements ClientInterface
{
/**
* @var RequestInterface
*/
protected $request;
/**
* @var guzzleClient
*/
protected $guzzleClient;
/**
* @var array|null
*/
protected $config;
/**
* Client constructor.
*
* @param RequestInterface $request
*/
protected function __construct(RequestInterface $request)
{
$this->request = $request();
$this->config = config('top-warehouse');
$clientOption = \Arr::get($this->config, 'options.client', []);
$this->guzzleClient = new guzzleClient($clientOption);
}
/**
* @param RequestInterface $request
*
* @return ClientInterface
*/
protected static function make(RequestInterface $request): ClientInterface
{
return new static($request);
}
/**
* @inheritDoc
*/
public static function request(RequestInterface $request): ResponseInterface
{
return self::make($request)->send();
}
/**
* @inheritDoc
*/
public function send(): ResponseInterface
{
$retry = Arr::get($this->config, 'options.retry', 1);
try {
return retry($retry, function ($attempt) {
return $this->execute($attempt);
}, 100, function ($exception) {
return $exception instanceof RequestException || $exception instanceof BadResponseException;
});
} catch (\Throwable $exception) {
throw new Exception($exception);
}
}
/**
* @param $attempt
*
* @return ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function execute($attempt): ResponseInterface
{
$key = RequestOptions::QUERY;
if ('GET' === strtoupper($this->request->getMethod())) {
$key = RequestOptions::QUERY;
} elseif ('POST' === strtoupper($this->request->getMethod())) {
if ($this->request->getContentType() == 'application/json') {
$key = RequestOptions::JSON;
} else {
$key = RequestOptions::FORM_PARAMS;
}
}
$pathInfo = parse_url($this->request->getPath());
$pathQuery = Arr::get($pathInfo, 'query');
parse_str($pathQuery, $pathQueryArray);
$data = $this->request->toArray() + [
'app_key' => Arr::get($this->config, 'app_key'),
'v' => $this->request->getVersion(),
'timestamp' => date('Y-m-d H:i:s'),
] + $pathQueryArray;
$data['sign'] = SignatureUtil::generate($data, Arr::get($this->config, 'app_secret'));
ksort($data);
$response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [
$key => array_filter($data, function ($v) {
return !(is_null($v) || $v === '');
}),
RequestOptions::HEADERS => [
'Content-Type' => 'application/json;charset=UTF-8',
]
]);
return new Response($response);
}
}