1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brofist\ApiClient; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
|
7
|
|
|
class Json |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var HttpClient |
11
|
|
|
*/ |
12
|
|
|
private $httpClient; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $endpoint = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $additionalOptions = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $options |
26
|
|
|
*/ |
27
|
7 |
|
public function __construct(array $options = []) |
28
|
|
|
{ |
29
|
7 |
|
if (!isset($options['endpoint'])) { |
30
|
1 |
|
throw new \InvalidArgumentException("Endpoint not set"); |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
$this->endpoint = trim($options['endpoint'], '/'); |
34
|
|
|
|
35
|
7 |
|
if (isset($options['authToken'])) { |
36
|
1 |
|
$this->additionalOptions['auth'] = [$options['authToken'], '']; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
7 |
|
if (isset($options['basicAuth'])) { |
40
|
1 |
|
$this->additionalOptions['auth'] = $options['basicAuth']; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
7 |
|
if (!isset($options['httpClient'])) { |
44
|
1 |
|
$options['httpClient'] = new HttpClient(); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
7 |
|
$this->httpClient = $options['httpClient']; |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function getEndpoint() |
54
|
|
|
{ |
55
|
1 |
|
return $this->endpoint; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $path |
60
|
|
|
* @param array $params |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
3 |
|
public function get($path, array $params = []) |
65
|
1 |
|
{ |
66
|
3 |
|
return $this->request('GET', $path, ['query' => $params]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $path |
71
|
|
|
* @param array $postData |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
1 |
|
public function post($path, array $postData) |
76
|
|
|
{ |
77
|
1 |
|
return $this->request('POST', $path, ['form_params' => $postData]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $path |
82
|
|
|
* @param array $putData |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public function put($path, array $putData) |
87
|
|
|
{ |
88
|
1 |
|
return $this->request('PUT', $path, ['json' => $putData]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $method |
93
|
|
|
* @param string $path |
94
|
|
|
* @param array $options |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
5 |
|
private function request($method, $path, $options) |
99
|
|
|
{ |
100
|
5 |
|
$uri = $this->endpoint . $path; |
101
|
|
|
|
102
|
5 |
|
$options = array_merge($options, $this->additionalOptions); |
103
|
|
|
|
104
|
5 |
|
$response = $this->httpClient->request($method, $uri, $options); |
105
|
|
|
|
106
|
5 |
|
return json_decode($response->getBody(), true); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|