|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Multicoin\Api\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Http\Client\Common\Exception\ClientErrorException; |
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
8
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
|
9
|
|
|
use Http\Client\Common\PluginClient; |
|
10
|
|
|
use Http\Client\HttpClient; |
|
11
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
12
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
13
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
14
|
|
|
use Http\Message\RequestFactory; |
|
15
|
|
|
use Http\Message\UriFactory; |
|
16
|
|
|
use Illuminate\Support\Collection; |
|
17
|
|
|
|
|
18
|
|
|
class ApiClient |
|
19
|
|
|
{ |
|
20
|
|
|
protected $client; |
|
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var HttpClient |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $httpClient; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
protected $plugins = []; |
|
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RequestFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $requestFactory; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var UriFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
private $uriFactory; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
string $baseUrl, |
|
39
|
|
|
array $plugins = [], |
|
40
|
|
|
bool $replace = true, |
|
41
|
|
|
HttpClient $httpClient = null, |
|
42
|
|
|
RequestFactory $requestFactory = null |
|
43
|
|
|
) { |
|
44
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
|
45
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
|
46
|
|
|
$this->uriFactory = new BaseUriPlugin( |
|
|
|
|
|
|
47
|
|
|
UriFactoryDiscovery::find()->createUri($baseUrl), |
|
48
|
|
|
['replace' => $replace] |
|
49
|
|
|
); |
|
50
|
|
|
$this->addPlugins(array_merge($plugins, [$this->uriFactory])); |
|
51
|
|
|
$this->client = $this->getHttpClient(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function addPlugins($plugin) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->plugins = array_merge($this->plugins, $plugin); |
|
57
|
|
|
$this->client = $this->getHttpClient(); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getHttpClient() |
|
63
|
|
|
{ |
|
64
|
|
|
return new HttpMethodsClient($this->getPluginClient(), $this->requestFactory); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getPluginClient() |
|
68
|
|
|
{ |
|
69
|
|
|
return new PluginClient($this->httpClient, $this->plugins); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function doGet(string $url): ?Collection |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$request = $this->client->get($url); |
|
76
|
|
|
$response = $request->getBody()->getContents(); |
|
77
|
|
|
} catch (ClientErrorException $exception) { |
|
78
|
|
|
throw new \Exception($exception); |
|
79
|
|
|
throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return collect([ |
|
82
|
|
|
'error' => [ |
|
83
|
|
|
'code' => $exception->getCode(), |
|
84
|
|
|
'reason' => $exception->getMessage(), |
|
85
|
|
|
], |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->responseResult($response); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function doPost(string $url, array $data = []): ?Collection |
|
93
|
|
|
{ |
|
94
|
|
|
try { |
|
95
|
|
|
$request = $this->client->post($url, $data); |
|
96
|
|
|
$response = $request->getBody()->getContents(); |
|
97
|
|
|
} catch (ClientErrorException $exception) { |
|
98
|
|
|
throw new \Exception($exception); |
|
99
|
|
|
// throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1); |
|
100
|
|
|
|
|
101
|
|
|
return collect([ |
|
|
|
|
|
|
102
|
|
|
'error' => [ |
|
103
|
|
|
'code' => $exception->getCode(), |
|
104
|
|
|
'reason' => $exception->getMessage(), |
|
105
|
|
|
], |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->responseResult($response); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function responseResult(?string $response): ?Collection |
|
113
|
|
|
{ |
|
114
|
|
|
$data = json_decode($response, true); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
if (isset($data['error'])) { |
|
117
|
|
|
throw new Exception($data['error']['message']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return collect($data); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|