1 | <?php |
||
24 | class Client |
||
25 | { |
||
26 | /** |
||
27 | * The configuration details for connection |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $config = array(); |
||
31 | |||
32 | /** |
||
33 | * The expected format for the config array |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $config_prototype = array( |
||
37 | 'api_key' => '', |
||
38 | 'api_secret' => '', |
||
39 | 'site_url' => '', |
||
40 | ); |
||
41 | |||
42 | /** |
||
43 | * The API Key to use |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $api_key = null; |
||
47 | |||
48 | /** |
||
49 | * The API Secret to use |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $api_secret = null; |
||
53 | |||
54 | /** |
||
55 | * The URL to the Backup Pro API endpoint |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $site_url = null; |
||
59 | |||
60 | /** |
||
61 | * The debug information |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $debug_info = array(); |
||
65 | |||
66 | /** |
||
67 | * The Curl handle |
||
68 | * @var resource |
||
69 | */ |
||
70 | protected $curl_handle = null; |
||
71 | |||
72 | /** |
||
73 | * The HTTP verb names |
||
74 | * @var string |
||
75 | */ |
||
76 | const HTTP_METHOD_GET = 'GET'; |
||
77 | const HTTP_METHOD_POST = 'POST'; |
||
78 | const HTTP_METHOD_PUT = 'PUT'; |
||
79 | const HTTP_METHOD_DELETE = 'DELETE'; |
||
80 | |||
81 | /** |
||
82 | * Sets it up |
||
83 | * @param array $config |
||
84 | */ |
||
85 | public function __construct(array $config = array()) |
||
92 | |||
93 | /** |
||
94 | * Returns the config |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getConfig() |
||
101 | |||
102 | /** |
||
103 | * Sets the API key to use for authentication |
||
104 | * @param string $key |
||
105 | * @return \JargerApp\Rest\Client |
||
106 | */ |
||
107 | public function setApiKey($key) |
||
112 | |||
113 | /** |
||
114 | * Returns the API key |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getApiKey() |
||
121 | |||
122 | /** |
||
123 | * Sets the API secret to use for authentication |
||
124 | * @param string $secret |
||
125 | * @return \JargerApp\Rest\Client |
||
126 | */ |
||
127 | public function setApiSecret($secret) |
||
132 | |||
133 | /** |
||
134 | * Returns the API secret |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getApiSecret() |
||
141 | |||
142 | /** |
||
143 | * Sets the Backup Pro REST API site URL |
||
144 | * @param string $site_url |
||
145 | * @return \JargerApp\Rest\Client |
||
146 | */ |
||
147 | public function setSiteUrl($site_url) |
||
152 | |||
153 | /** |
||
154 | * Returns the Backup Pro REST API site URL |
||
155 | * @param string $endpoint |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getSiteUrl($endpoint = '', array $query = array()) |
||
166 | |||
167 | /** |
||
168 | * Send a POST request |
||
169 | * @param string $endpoint The API endpoint |
||
170 | * @param array $payload Data to submit |
||
171 | */ |
||
172 | public function post($endpoint, array $payload = array()) |
||
176 | |||
177 | /** |
||
178 | * Sends a GET request |
||
179 | * @param string $endpoint The API endpoint |
||
180 | * @param array $payload |
||
181 | */ |
||
182 | public function get($endpoint, array $payload = array()) |
||
186 | |||
187 | /** |
||
188 | * PUT to an authenciated API endpoint w/ payload |
||
189 | * |
||
190 | * @param string $endpoint |
||
191 | * @param array $payload |
||
192 | * @return array |
||
193 | */ |
||
194 | public function put($endpoint, array $payload = array()) |
||
198 | |||
199 | /** |
||
200 | * Performs a DELETE request |
||
201 | * @param string $endpoint |
||
202 | * @param array $payload |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function delete($endpoint, array $payload = array()) |
||
209 | |||
210 | /** |
||
211 | * Sets up the Hmac authentication headers and dispatches the request |
||
212 | * @param string $endpoint The API endpoing we want |
||
213 | * @param array $payload Any data to send along |
||
214 | * @param string $method The HTTP method |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function fetch($endpoint, array $payload = array(), $method = 'GET') |
||
239 | |||
240 | /** |
||
241 | * Returns the debug data |
||
242 | * @return array |
||
243 | */ |
||
244 | public function getDebugInfo() |
||
248 | |||
249 | /** |
||
250 | * Make a CURL request |
||
251 | * |
||
252 | * @param string $url |
||
253 | * @param array $payload |
||
254 | * @param string $method |
||
255 | * @param array $headers |
||
256 | * @param array $curl_options |
||
257 | * @throws \RuntimeException |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function request($url, array $payload = array(), $method = 'GET', array $headers = array(), array $curl_options = array()) |
||
311 | |||
312 | |||
313 | protected function getCurlHandle() |
||
320 | |||
321 | public function __destruct() |
||
327 | } |