-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHypothesis.inc.php
68 lines (58 loc) · 2.27 KB
/
Hypothesis.inc.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
<?php
require_once('vendor/autoload.php');
class HypothesisAPI {
protected $baseUrl = 'https://hypothes.is/api/';
/**
* Search for annotations.
* @param $params array See http://h.readthedocs.org/en/latest/api.html#search
* @param $token Optional authorization token
* @return array Set of matching annotations
*/
public function search($params, $token = null) {
$response = \Httpful\Request::get($this->baseUrl . 'search?' . http_build_query($params))
->addHeader('Authorization', $token?"Bearer $token":null)
->send();
if ($response->code != '200') throw new Exception('Unexpected service response ' . $response->code);
return $response->body->rows;
}
/**
* Read an annotation.
* @param $id string ID of annotation to read
* @param $token string Optional authorization token
* @return Object See http://h.readthedocs.io/en/latest/api.html#read
*/
public function read($id, $token = null) {
$response = \Httpful\Request::get($this->baseUrl . 'annotations/' . urlencode($id))
->addHeader('Authorization', $token?"Bearer $token":null)
->send();
if ($response->code != '200') throw new Exception('Unexpected service response ' . $response->code);
return $response->body;
}
/**
* Create an annotation.
* @param $annotation array See http://h.readthedocs.io/en/latest/api.html#create
* @param $token string Authorization token
* @return Object Resultant annotation; see http://h.readthedocs.io/en/latest/api.html#read
*/
public function create($annotation, $token) {
$response = \Httpful\Request::post($this->baseUrl . 'annotations')
->sendsJson()
->addHeader('Authorization', "Bearer $token")
->body(json_encode($annotation))
->send();
if ($response->code != '200') throw new Exception('Unexpected service response ' . $response->code);
return $response->body;
}
/**
* Delete an annotation by ID.
* @param $id string ID of annotation to delete
* @param $token string Authorization token
*/
public function delete($id, $token) {
$response = \Httpful\Request::delete($this->baseUrl . 'annotations/' . urlencode($id))
->addHeader('Authorization', $token?"Bearer $token":null)
->send();
if ($response->code != '200') throw new Exception('Unexpected service response ' . $response->code);
return $response->body;
}
}