This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CanvasPest.php
367 lines (342 loc) · 11.5 KB
/
CanvasPest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/** CanvasPest and (directly) related classes */
/**
* An object to handle interactions with the Canvas API.
*
* For more information on the Canvas API refer to the offical
* {@link https://canvas.instructure.com/doc/api/ Canvas API documentation} or
* to the (slightly more up-to-date and pleasingly interactive)
* {@link https://canvas.instructure.com/doc/api/live live documentation}.
*
* You can access the live documentation for your own Canvas instance and make
* actual API calls to it at `https://<path-to-your-instance>/doc/api/live`
*
* @author Seth Battis <SethBattis@stmarksschool.org>
**/
class CanvasPest extends Pest {
/** Name of the parameter controlling the number of responses per page */
const PARAM_PER_PAGE = 'per_page';
/** @var string[] $headers Additional headers to be passed to the API with each call */
protected $headers;
/**
* Construct a new CanvasPest
*
* @api
*
* @param string $apiInstanceUrl URL of the API instance (e.g.
* `'https://canvas.instructure.com/api/v1'`)
* @param string $apiAuthorizationToken (Optional) API access token for the
* API instance (if not provided now, it will need to be provided later)
*
* @return void
*
* @see CanvasPest::setupToken() To configure the API access token later
**/
public function __construct($apiInstanceUrl, $apiAuthorizationToken = null) {
parent::__construct($apiInstanceUrl);
if (!empty($apiAuthorizationToken)) {
$this->setupToken($apiAuthorizationToken);
}
}
/**
* Set up a new API access token to access this instance
*
* @param string $token API access token
*
* @throws CanvasPest_Exception INVALID_TOKEN on an empty or non-string token value
**/
public function setupToken($token) {
if (is_string($token) && !empty($token)) {
$this->headers['Authorization'] = "Bearer $token";
} else if ($this->throw_exceptions) {
throw new CanvasPest_Exception(
'API authorization token must be a non-zero-length string',
CanvasPest_Exception::INVALID_TOKEN
);
}
}
/**
* Preprocess API call parameters before use
*
* Force maximum response page size, if not already defined.
*
* @param string[] $data Array of parameters for the next API call
*
* @return string[] Updated array of parameters
*
* @see CanvasArray::MAXIMUM_PER_PAGE Maximum number of responses per page
* @see CanvasPest::PARAM_PER_PAGE Page size parameter
**/
private function preprocessData($data) {
if (is_array($data) && !array_key_exists(self::PARAM_PER_PAGE, $data)) {
$data[self::PARAM_PER_PAGE] = CanvasArray::MAXIMUM_PER_PAGE;
}
return $data;
}
/**
* Prepare data
*
* Extended by CanvasPest to format the HTTP query parameters
with non-indexed array elements (so `...foo?bar[]=1&bar[]=2`, rather than
* `...foo?bar[0]=1&bar[1]=2`, as the Canvas API prefers them).
*
* @param string[] $data Query parameters
*
* @return string|string[]
*/
public function prepData($data)
{
if (is_array($data)) {
$multipart = false;
foreach ($data as $item) {
if (is_string($item) && strncmp($item, "@", 1) == 0 && is_file(substr($item, 1))) {
$multipart = true;
break;
}
}
// FIXME there has _got_ to a better way to do this than a regex
return ($multipart) ? $data : preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', http_build_query($data));
} else {
return $data;
}
}
/**
* Prepare API request headers
*
* Flatten headers from an associative array to a numerically indexed array
* of `"Name: Value"` style entries like `CURLOPT_HTTPHEADER` expects.
* Numerically indexed arrays are not modified.
*
* Extended by CanvasPest to include the API access token in the
* `Authorization` header.
*
* @param string[] $headers
* @return string[]
**/
protected function prepHeaders($headers) {
return parent::prepHeaders(array_merge($this->headers, $headers));
}
/**
* Parse the API response into an object (or collection of objects).
*
* For queries to individually identified endpoints (e.g.
* `accounts/1/users/123`), return a CanvasObject representing the API response
* describing _that_ individually identified object affected by the query.
*
* For queries to generic endpoints (e.g. `accounts/1/users`), return a
* traversable CanvasArray (of CanvasObjects) representing the API response
* describing the list of objects affected by the query.
*
* @param string $path Path to the API endpoint queried
* @param string $response JSON-encoded response from the API
*
* @return CanvasObject|CanvasArray
**/
protected function postprocessResponse($response) {
if (substr($response, 0, 1) == '{') {
return new CanvasObject($response);
} elseif (substr($response, 0, 1) == '[') {
return new CanvasArray($response, $this);
} else {
throw new CanvasPest_Exception(
$response,
CanvasPest_Exception::INVALID_JSON_RESPONSE
);
}
}
/**
* Make a GET call to the API
*
* For queries to individually identified endpoints (e.g.
* `accounts/1/users/123`), return a CanvasObject representing the API response
* describing _that_ individually identified object affected by the query.
*
* For queries to generic endpoints (e.g. `accounts/1/users`), return a
* traversable CanvasArray (of CanvasObjects) representing the API response
* describing the list of objects affected by the query.
*
* @api
*
* @param string $path Path to the API endpoint of this call
* @param string|string[] $data (Optional) Query parameters for this call
* @param string|string[] $headers (Optional) Any additional HTTP headers for this call
*
* @return CanvasObject|CanvasArray
**/
public function get($path, $data = array(), $headers = array()) {
return $this->postprocessResponse(
parent::get($path, $this->preprocessData($data), $headers)
);
}
/**
* Make a POST call to the API
*
* For queries to individually identified endpoints (e.g.
* `accounts/1/users/123`), return a CanvasObject representing the API response
* describing _that_ individually identified object affected by the query.
*
* For queries to generic endpoints (e.g. `accounts/1/users`), return a
* traversable CanvasArray (of CanvasObjects) representing the API response
* describing the list of objects affected by the query.
*
* @api
*
* @param string $path Path to the API endpoint of this call
* @param string|string[] $data (Optional) Query parameters for this call
* @param string|string[] $headers (Optional) Any additional HTTP headers for this call
*
* @return CanvasObject|CanvasArray
**/
public function post($path, $data = array(), $headers = array()) {
return $this->postprocessResponse(
parent::post($path, $this->preprocessData($data), $headers)
);
}
/**
* Make a PUT call to the API
*
* For queries to individually identified endpoints (e.g.
* `accounts/1/users/123`), return a CanvasObject representing the API response
* describing _that_ individually identified object affected by the query.
*
* For queries to generic endpoints (e.g. `accounts/1/users`), return a
* traversable CanvasArray (of CanvasObjects) representing the API response
* describing the list of objects affected by the query.
*
* @api
*
* @param string $path Path to the API endpoint of this call
* @param string|string[] $data (Optional) Query parameters for this call
* @param string|string[] $headers (Optional) Any additional HTTP headers for this call
*
* @return CanvasObject|CanvasArray
**/
public function put($path, $data = array(), $headers = array()) {
return $this->postprocessResponse(
parent::put($path, $this->preprocessData($data), $headers)
);
}
/**
* Make a DELETE call to the API
*
* For queries to individually identified endpoints (e.g.
* `accounts/1/users/123`), return a CanvasObject representing the API response
* describing _that_ individually identified object affected by the query.
*
* For queries to generic endpoints (e.g. `accounts/1/users`), return a
* traversable CanvasArray (of CanvasObjects) representing the API response
* describing the list of objects affected by the query.
*
* @api
*
* @param string $path Path to the API endpoint of this call
* @param string|string[] $data (Optional) Query parameters for this call
* @param string|string[] $headers (Optional) Any additional HTTP headers for this call
*
* @return CanvasObject|CanvasArray
**/
public function delete($path, $headers = array()) {
return $this->postprocessResponse(
parent::delete($path, $headers)
);
}
/**
* Make a PATCH call to the API
*
* @deprecated The Canvas API does not currently support PATCH calls
*
* @return void
*
* @throws CanvasPest_Exception UNSUPPORTED_METHOD All calls to this method will cause an exception
**/
public function patch() {
throw new CanvasPest_Exception(
'The Canvas API does not support the PATCH method',
CanvasPest_Exception::UNSUPPORTED_METHOD
);
}
}
/**
* Treat the API as read-only.
*
* Without excessive editorializing, the permissions structure in Canvas bites.
* For example, one can't create a user who has read-only access to the
* complete API -- if a user has complete access to the API, they have
* _complete_ access to the API, including the ability to alter and delete
* information. This object provides a comparative level of safety, enforcing
* a restriction on GET-only API calls.
*
* @author Seth Battis <SethBattis@stmarksschool.org>
**/
class CanvasPestImmutable extends CanvasPest {
/**
* {@inheritDoc}
*
* @deprecated CanvasPestImmutable only supports GET calls to the API
*
* @return void
*
* @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception
**/
public function put() {
throw new CanvasPestImmutable_Exception(
'Only GET calls to the API are allowed from CanvasPestImmutable.',
CanvasPestImmutable_Exception::IMMUTABLE
);
}
/**
* {@inheritDoc}
*
* @deprecated CanvasPestImmutable only supports GET calls to the API
*
* @return void
*
* @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception
**/
public function post() {
throw new CanvasPestImmutable_Exception(
'Only GET calls to the API are allowed from CanvasPestImmutable.',
CanvasPestImmutable_Exception::IMMUTABLE
);
}
/**
* {@inheritDoc}
*
* @deprecated CanvasPestImmutable only supports GET calls to the API
*
* @return void
*
* @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception
**/
public function delete() {
throw new CanvasPestImmutable_Exception(
'Only GET calls to the API are allowed from CanvasPestImmutable.',
CanvasPestImmutable_Exception::IMMUTABLE
);
}
}
/**
* All exceptions thrown by the CanvasPest object
*
* @author Seth Battis <SethBattis@stmarksschool.org>
**/
class CanvasPest_Exception extends Exception {
/** The API access method is not supported by the Canvas API */
const UNSUPPORTED_METHOD = 1;
/** The API access token provided is invalid */
const INVALID_TOKEN = 2;
/** Unanticipated JSON response from API */
const INVALID_JSON_RESPONSE = 3;
}
/**
* All exceptions thrown by CanvasPestImmutable
*
* @author Seth Battis <SethBattis@stmarksschool.org>
**/
class CanvasPestImmutable_Exception extends CanvasPest_Exception {
/**
* @const IMMUTABLE A request to the API that would change data was attempted
**/
const IMMUTABLE = 1001;
}
?>