|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vindi; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Resource |
|
7
|
|
|
* |
|
8
|
|
|
* @package Vindi |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class Resource |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Vindi\ApiRequester |
|
14
|
|
|
*/ |
|
15
|
|
|
public $apiRequester; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Resource constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $arguments |
|
21
|
|
|
*/ |
|
22
|
448 |
|
public function __construct($arguments = []) |
|
23
|
|
|
{ |
|
24
|
448 |
|
if (key_exists(Vindi::VINDI_API_KEY, $arguments)) { |
|
25
|
|
|
Vindi::setApiKey($arguments[Vindi::VINDI_API_KEY]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
448 |
|
if (key_exists(Vindi::VINDI_API_URI, $arguments)) { |
|
29
|
|
|
Vindi::setApiUri($arguments[Vindi::VINDI_API_URI]); |
|
30
|
|
|
} |
|
31
|
448 |
|
$this->apiRequester = new ApiRequester; |
|
32
|
448 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The endpoint at default state that will hit the API. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract public function endpoint(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Build url that will hit the API. |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $id The resource's id. |
|
45
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
322 |
|
public function url($id = null, $additionalEndpoint = null) |
|
50
|
|
|
{ |
|
51
|
322 |
|
$endpoint = $this->endpoint(); |
|
52
|
|
|
|
|
53
|
322 |
|
if (! is_null($id)) { |
|
54
|
238 |
|
$endpoint .= '/' . $id; |
|
55
|
238 |
|
} |
|
56
|
322 |
|
if (! is_null($additionalEndpoint)) { |
|
57
|
112 |
|
$endpoint .= '/' . $additionalEndpoint; |
|
58
|
112 |
|
} |
|
59
|
|
|
|
|
60
|
322 |
|
return $endpoint; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieve all resources. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $params Pagination and Filter parameters. |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
70
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
71
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
72
|
|
|
*/ |
|
73
|
42 |
|
public function all(array $params = []) |
|
74
|
|
|
{ |
|
75
|
42 |
|
return $this->apiRequester->request('GET', $this->url(), ['query' => $params]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create a new resource. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $form_params The request body. |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
85
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
86
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
87
|
|
|
*/ |
|
88
|
42 |
|
public function create(array $form_params = []) |
|
89
|
|
|
{ |
|
90
|
42 |
|
return $this->apiRequester->request('POST', $this->url(), ['json' => $form_params]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrieve a specific resource. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $id The resource's id. |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
100
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
101
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
102
|
|
|
*/ |
|
103
|
42 |
|
public function retrieve($id = null) |
|
104
|
|
|
{ |
|
105
|
42 |
|
return $this->apiRequester->request('GET', $this->url($id)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Update a specific resource. |
|
110
|
|
|
* |
|
111
|
|
|
* @param int $id The resource's id. |
|
112
|
|
|
* @param array $form_params The request body. |
|
113
|
|
|
* |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
116
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
117
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
118
|
|
|
*/ |
|
119
|
42 |
|
public function update($id = null, array $form_params = []) |
|
120
|
|
|
{ |
|
121
|
42 |
|
return $this->apiRequester->request('PUT', $this->url($id), ['json' => $form_params]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Delete a specific resource. |
|
126
|
|
|
* |
|
127
|
|
|
* @param int $id The resource's id. |
|
128
|
|
|
* @param array $form_params The request body. |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
132
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
133
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
134
|
|
|
*/ |
|
135
|
42 |
|
public function delete($id = null, array $form_params = []) |
|
136
|
|
|
{ |
|
137
|
42 |
|
return $this->apiRequester->request('DELETE', $this->url($id), ['json' => $form_params]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Make a GET request to an additional endpoint for a specific resource. |
|
142
|
|
|
* |
|
143
|
|
|
* @param int $id The resource's id. |
|
144
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
148
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
149
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
150
|
|
|
*/ |
|
151
|
44 |
|
public function get($id = null, $additionalEndpoint = null) |
|
152
|
|
|
{ |
|
153
|
44 |
|
return $this->apiRequester->request('GET', $this->url($id, $additionalEndpoint)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Make a POST request to an additional endpoint for a specific resource. |
|
158
|
|
|
* |
|
159
|
|
|
* @param int $id The resource's id. |
|
160
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
|
161
|
|
|
* @param array $form_params The request body. |
|
162
|
|
|
* |
|
163
|
|
|
* @return mixed |
|
164
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
165
|
|
|
* @throws \Vindi\Exceptions\RateLimitException |
|
166
|
|
|
* @throws \Vindi\Exceptions\RequestException |
|
167
|
|
|
*/ |
|
168
|
68 |
|
public function post($id = null, $additionalEndpoint = null, array $form_params = []) |
|
169
|
|
|
{ |
|
170
|
68 |
|
return $this->apiRequester->request('POST', $this->url($id, $additionalEndpoint), ['json' => $form_params]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Return the last response from a preview request |
|
175
|
|
|
* |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
84 |
|
public function getLastResponse() |
|
179
|
|
|
{ |
|
180
|
84 |
|
return $this->apiRequester->lastResponse; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|