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 | private $options = []; |
||
26 | |||
27 | private $rawRequest; |
||
28 | |||
29 | 2 | public function __construct( |
|
40 | |||
41 | public function addHeader($name, $value) |
||
45 | |||
46 | 1 | public function get($id = null, $payload = null) |
|
63 | |||
64 | public function post($payload) |
||
65 | { |
||
66 | return $this->request($this->getUri(), 'POST', $payload); |
||
67 | } |
||
68 | |||
69 | public function put($id, $payload = null) |
||
70 | { |
||
71 | $uri = $this->getUri($id); |
||
72 | |||
73 | return $this->request($uri, 'PUT', $payload); |
||
74 | } |
||
75 | |||
76 | private function request($uri, $method, $payload = null) |
||
77 | { |
||
78 | $factory = $this->getMessageFactory(); |
||
79 | $request = $factory->createRequest( |
||
80 | $method, |
||
81 | $uri, |
||
82 | $this->headers, |
||
83 | $this->normalizePayload($payload) |
||
84 | ); |
||
85 | |||
86 | $request = $this->authenticate($request); |
||
87 | |||
88 | return $this->resolveResponse($this->sendRequest($request)); |
||
89 | } |
||
90 | |||
91 | public function delete($id) |
||
92 | { |
||
93 | $uri = $this->getUri($id); |
||
94 | |||
95 | return $this->request($uri, 'DELETE'); |
||
96 | } |
||
97 | |||
98 | 3 | public function getUri($id = null) |
|
108 | |||
109 | public function setBasicAuthentication($username, $password) |
||
114 | |||
115 | public function setHeaderAuthentication($name, $value) |
||
119 | |||
120 | 1 | protected function authenticate(RequestInterface $request) |
|
139 | |||
140 | 1 | private function normalizePayload($payload) |
|
148 | |||
149 | 1 | private function getMessageFactory() |
|
153 | |||
154 | /** |
||
155 | * Resolve the response from client. |
||
156 | * |
||
157 | * @param ResponseInterface $response |
||
158 | * @return array An array with following values: |
||
159 | * 'status': The Http status of response |
||
160 | * 'headers': An array of response headers |
||
161 | * 'body': The json decoded body response. (Since we are in |
||
162 | * RestClient) |
||
163 | * 'raw_response': The raw body response for logging purposes. |
||
164 | * 'raw_request': The raw body request for logging purposes. |
||
165 | */ |
||
166 | protected function resolveResponse(ResponseInterface $response) |
||
179 | } |
||
180 |