1 | <?php |
||
11 | class RestClient extends AbstractClient |
||
12 | { |
||
13 | private $baseUri; |
||
14 | |||
15 | private $resource; |
||
16 | |||
17 | private $username; |
||
18 | |||
19 | private $pass; |
||
20 | |||
21 | private $headerAuthentication = []; |
||
22 | |||
23 | private $headers = []; |
||
24 | |||
25 | 2 | public function __construct( |
|
36 | |||
37 | public function addHeader($name, $value) |
||
41 | |||
42 | 1 | public function get($id = null, $payload = null) |
|
43 | { |
||
44 | 1 | $factory = $this->getMessageFactory(); |
|
45 | 1 | $uri = $this->getUri($id); |
|
46 | 1 | if ($query = $this->normalizePayload($payload)) { |
|
47 | 1 | $uri = $uri . '?' . ltrim($query, '?'); |
|
48 | } |
||
49 | 1 | $request = $factory->createRequest( |
|
50 | 1 | 'GET', |
|
51 | 1 | $uri, |
|
52 | 1 | $this->headers |
|
53 | ); |
||
54 | |||
55 | 1 | $request = $this->authenticate($request); |
|
56 | |||
57 | 1 | return $this->resolveResponse($this->sendRequest($request)); |
|
58 | } |
||
59 | |||
60 | public function post($payload) |
||
64 | |||
65 | public function put($id, $payload = null) |
||
71 | |||
72 | private function request($uri, $method, $payload = null) |
||
73 | { |
||
74 | $factory = $this->getMessageFactory(); |
||
75 | $request = $factory->createRequest( |
||
76 | $method, |
||
77 | $uri, |
||
78 | $this->headers, |
||
79 | $this->normalizePayload($payload) |
||
80 | ); |
||
81 | |||
82 | $request = $this->authenticate($request); |
||
83 | |||
84 | return $this->resolveResponse($this->sendRequest($request)); |
||
85 | } |
||
86 | |||
87 | public function delete($id) |
||
93 | |||
94 | 3 | public function getUri($id = null) |
|
95 | { |
||
106 | |||
107 | public function setBasicAuthentication($username, $password) |
||
112 | |||
113 | public function setHeaderAuthentication($name, $value) |
||
117 | |||
118 | 1 | protected function authenticate(RequestInterface $request) |
|
137 | |||
138 | 1 | private function normalizePayload($payload) |
|
146 | |||
147 | 1 | private function getMessageFactory() |
|
151 | |||
152 | /** |
||
153 | * Resolve the response from client. |
||
154 | * |
||
155 | * @param ResponseInterface $response |
||
156 | * @return array An array with following values: |
||
157 | * 'status': The Http status of response |
||
158 | * 'headers': An array of response headers |
||
159 | * 'body': The json decoded body response. (Since we are in |
||
160 | * RestClient) |
||
161 | * 'raw_response': The raw body response for logging purposes. |
||
162 | * 'raw_request': The raw body request for logging purposes. |
||
163 | */ |
||
164 | protected function resolveResponse(ResponseInterface $response) |
||
177 | } |
||
178 |