1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Streak; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
|
7
|
|
|
class Client |
8
|
|
|
{ |
9
|
|
|
protected $handler; |
10
|
|
|
|
11
|
|
|
public function __construct(ClientInterface $handler) |
12
|
|
|
{ |
13
|
|
|
$this->handler = $handler; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Shortcut to make a GET request |
18
|
|
|
* |
19
|
|
|
* @return array|string |
20
|
|
|
*/ |
21
|
|
|
public function get($path, array $options = [], $withJson = true) |
22
|
|
|
{ |
23
|
|
|
return $this->sendRequest('GET', $path, $options, $withJson); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Shortcut to make a PUT request |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function put($path, array $options = []) |
32
|
|
|
{ |
33
|
|
|
return $this->sendRequest('PUT', $path, $options); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Shortcut to make a DELETE request |
38
|
|
|
* |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
public function delete($path, array $options = []) |
42
|
|
|
{ |
43
|
|
|
$response = $this->sendRequest('DELETE', $path, $options); |
44
|
|
|
|
45
|
|
|
return $response['success']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Shortcut to make a POST request |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function post($path, array $options = []) |
54
|
|
|
{ |
55
|
|
|
return $this->sendRequest('POST', $path, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Make the HTTP request through the handler |
60
|
|
|
* |
61
|
|
|
* @return array|string |
62
|
|
|
*/ |
63
|
|
|
public function sendRequest($method, $path, array $options = [], $withJson = true) |
64
|
|
|
{ |
65
|
|
|
$response = $this->handler->request($method, $path, $options); |
66
|
|
|
|
67
|
|
|
$body = (string)$response->getBody(); |
68
|
|
|
|
69
|
|
|
return $withJson ? json_decode($body, true) : $body; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|