-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathClient.php
222 lines (181 loc) · 5.78 KB
/
Client.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php declare(strict_types=1);
namespace FOXRP\Rippled;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;
use FOXRP\Rippled\Api\Response;
use FOXRP\Rippled\Api\Request;
/**
* A rippled client.
*/
class Client
{
/** @var HttpClient */
private $httpClient;
/** @var string */
private $endpoint;
/** @var string Hostname or IP of the endpoint */
private $host;
/** @var MessageFactory */
protected $messageFactory;
/** @var int Port of the endpoint */
private $port;
/** @var string http or https */
private $scheme;
/**
* Connection constructor.
* @param $config
* @param HttpClient|null $httpClient
*/
public function __construct($config, HttpClient $httpClient = null, MessageFactory $messageFactory = null)
{
$type = gettype($config);
switch ($type) {
case 'string':
$this->setEndpoint($config);
break;
case 'array':
$this->setEndpoint($config['endpoint'] ?? null);
break;
default:
throw new \InvalidArgumentException('Constructor argument must be endpoint string or config array');
}
if ($this->endpoint !== null) {
$parts = parse_url($this->endpoint);
if ($parts === false || !isset($parts['scheme'], $parts['host'])) {
throw new \InvalidArgumentException('Invalid endpoint format');
}
$this->scheme = $parts['scheme'];
$this->setHost($parts['host']);
$this->setPort($parts['port'] ?? null);
} else {
$this->setScheme($config['scheme'] ?? null);
$this->setHost($config['host'] ?? null);
$this->setPort($config['port'] ?? null);
}
if (empty($this->scheme)) {
throw new \InvalidArgumentException('scheme is required if endpoint is not supplied');
}
// Validate protocol
if (!\in_array($this->scheme, ['http', 'https'])) {
throw new \OutOfBoundsException('scheme must be http or https');
}
if (empty($this->host)) {
throw new \InvalidArgumentException('host is required if endpoint is not supplied');
}
// Auto set port based on protocol if it has not been passed in
$port = $config['port'] ?? ($this->getScheme() === 'https' ? 443 : 80);
// Validate port
if ($port < 1 || $port >= 65535) {
throw new \OutOfBoundsException('port must be between 1 and 65535');
}
$this->port = $port;
// Setup HttpClient & messageFactory
$this->setHttpClient($httpClient ?: HttpClientDiscovery::find());
$this->setMessageFactory($messageFactory ?: MessageFactoryDiscovery::find());
// Build endpoint
if (empty($this->endpoint)) {
$this->setEndpoint($this->getScheme() . '://' . $this->getHost() . ':' . $this->getPort());
}
}
/**
* @param string $methodName
* @param array $params
* @return Request
* @throws \Exception
*/
public function request(string $methodName, array $params): Request
{
return new Request($methodName, $params, $this);
}
/**
* @param string $methodName
* @param array $params
* @return Response
* @throws \Exception
*/
public function send(string $methodName, array $params = []): Response
{
return $this->request($methodName, $params)->send();
}
/**
* @param string $method
* @param array|null $params
* @return \Psr\Http\Message\ResponseInterface
* @throws \Http\Client\Exception
*/
public function post(string $method, array $params = null)
{
$json = $this->prepareJson($method, $params);
$request = $this->getMessageFactory()->createRequest(
'POST',
$this->endpoint,
['Content-Type' => 'application/json'],
$json
);
return $this->httpClient->sendRequest($request);
}
/**
* @param string $method The API method string.
* @param array|null $params Associative array of method parameters.
* @return string Raw JSON formatted to send in the API body.
*/
public function prepareJson(string $method, array $params = null): string
{
if ($params === null) {
$params = new \stdClass();
}
$request = ['method' => $method, 'params' => []];
$request['params'][] = $params;
return json_encode($request);
}
public function getHttpClient(): HttpClient
{
return $this->httpClient;
}
public function setHttpClient(HttpClient $httpClient): void
{
$this->httpClient = $httpClient;
}
public function getMessageFactory(): MessageFactory
{
return $this->messageFactory;
}
public function setMessageFactory(MessageFactory $messageFactory): void
{
$this->messageFactory = $messageFactory;
}
public function getEndpoint(): ?string
{
return $this->endpoint;
}
public function setEndpoint(string $endpoint = null): void
{
$this->endpoint = $endpoint;
}
public function getHost(): ?string
{
return $this->host;
}
public function setHost(string $host = null): void
{
$this->host = $host;
}
public function getPort(): ?int
{
return $this->port;
}
public function setPort(int $port = null): void
{
$this->port = $port;
}
public function getScheme(): ?string
{
return $this->scheme;
}
public function setScheme(string $scheme = null): void
{
$this->scheme = $scheme;
}
}