1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WorkEtcClient; |
4
|
|
|
|
5
|
|
|
use Httpful\Request; |
6
|
|
|
|
7
|
|
|
class HttpfulClient implements HttpInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var bool |
11
|
|
|
*/ |
12
|
|
|
protected $hasErrors = false; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Invoke a GET request. |
16
|
|
|
* |
17
|
|
|
* @param string $endpoint |
18
|
|
|
* @param array $parameters |
19
|
|
|
* |
20
|
|
|
* @return array|string |
21
|
|
|
* |
22
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
23
|
|
|
*/ |
24
|
|
|
public function get(string $endpoint, array $parameters = []) |
25
|
|
|
{ |
26
|
|
|
$request = Request::get($endpoint . $this->buildQuery($parameters)) |
27
|
|
|
->expectsType('application/json'); |
28
|
|
|
|
29
|
|
|
return $this->send($request); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Invoke a POST request. |
34
|
|
|
* |
35
|
|
|
* @param string $endpoint |
36
|
|
|
* @param array $parameters |
37
|
|
|
* |
38
|
|
|
* @return array|string |
39
|
|
|
* |
40
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
41
|
|
|
*/ |
42
|
|
|
public function post(string $endpoint, array $parameters = []) |
43
|
|
|
{ |
44
|
|
|
$request = Request::post($endpoint) |
45
|
|
|
->expectsType('application/json') |
46
|
|
|
->body($this->jsonEncode($parameters)); |
47
|
|
|
|
48
|
|
|
return $this->send($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Encode the given array to JSON. If the array would not translate to an |
53
|
|
|
* object, force it. |
54
|
|
|
* |
55
|
|
|
* WORK[etc] expects an object, even if that object would be empty. |
56
|
|
|
* |
57
|
|
|
* @param array $array |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function jsonEncode(array $array): string |
62
|
|
|
{ |
63
|
|
|
if (empty($array)) { |
64
|
|
|
return '{}'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return json_encode($array); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Send the request and check for errors. |
72
|
|
|
* |
73
|
|
|
* @param \Httpful\Request $request |
74
|
|
|
* |
75
|
|
|
* @return array|string |
76
|
|
|
* |
77
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
78
|
|
|
*/ |
79
|
|
|
protected function send(Request $request) |
80
|
|
|
{ |
81
|
|
|
$response = $request->send(); |
82
|
|
|
|
83
|
|
|
$this->hasErrors = $response->hasErrors(); |
84
|
|
|
|
85
|
|
|
return json_decode(json_encode($response->body), true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Builds the query string for the URL. |
90
|
|
|
* |
91
|
|
|
* @param array $data |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function buildQuery(array $data = []): string |
96
|
|
|
{ |
97
|
|
|
if (count($data) === 0) { |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return '?' . http_build_query($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns whether or not errors have occurred. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function hasErrors(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->hasErrors; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|