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