1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CryptoMarkets\Common; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
abstract class Endpoint |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The request parameters. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $params; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The HTTP client instance. |
19
|
|
|
* |
20
|
|
|
* @var \CryptoMarkets\Common\Client |
21
|
|
|
*/ |
22
|
|
|
protected $httpClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Determine if the request need authorized. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $authorize = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new Endpoint instance. |
33
|
|
|
* |
34
|
|
|
* @param \CryptoMarkets\Common\Client $httpClient |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct(Client $httpClient) |
38
|
|
|
{ |
39
|
|
|
$this->httpClient = $httpClient; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configure the request parameters. |
44
|
|
|
* |
45
|
|
|
* @param array $params |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function configure(array $params = []) |
49
|
|
|
{ |
50
|
|
|
$this->params = $params; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Send a new request. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function send() |
61
|
|
|
{ |
62
|
|
|
$request = $this->createRequest(); |
63
|
|
|
|
64
|
|
|
$response = $this->httpClient->sendRequest($request); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $this->mapResponse((array) $this->parseResponse($response)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new request object. |
71
|
|
|
* |
72
|
|
|
* @return \GuzzleHttp\Psr7\Request |
73
|
|
|
*/ |
74
|
|
|
public function createRequest() |
75
|
|
|
{ |
76
|
|
|
return $this->httpClient->createRequest( |
77
|
|
|
$this->getMethod(), |
78
|
|
|
$this->getUrl(), |
79
|
|
|
$this->getPreparedHeaders(), |
80
|
|
|
$this->getPreparedData() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the request method. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getMethod() |
90
|
|
|
{ |
91
|
|
|
return 'POST'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the request url. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
abstract public function getUrl(); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the request headers for authorized or not. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getPreparedHeaders() |
107
|
|
|
{ |
108
|
|
|
return $this->getHeaders(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the request headers. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getHeaders() |
117
|
|
|
{ |
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the request data for authorized or not. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getPreparedData() |
127
|
|
|
{ |
128
|
|
|
$params = $this->getData(); |
129
|
|
|
|
130
|
|
|
if ($this->authorize) { |
131
|
|
|
$params = array_merge($params, $this->authenticationData($params)); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $params; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the request data. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getData() |
143
|
|
|
{ |
144
|
|
|
return []; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the authentication request data. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
protected function authenticationData() |
153
|
|
|
{ |
154
|
|
|
return []; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Parse and normalize the raw response. |
159
|
|
|
* |
160
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function parseResponse(ResponseInterface $response) |
164
|
|
|
{ |
165
|
|
|
return json_decode($response->getBody(), true); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Map the given array to create a response object. |
170
|
|
|
* |
171
|
|
|
* @param array $data |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
abstract public function mapResponse(array $data = []); |
175
|
|
|
} |
176
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.