-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHypothesis.inc.php
89 lines (74 loc) · 3.05 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
include('./httpful.phar');
set_time_limit(0);
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;
}
}
class DailyMedAPI {
protected $baseUrl = 'https://dailymed.nlm.nih.gov/dailymed/services/v2/';
/**
* Get the SPL (structured product label) for the drug from DailyMed, in JSON format
* @param $name name of the drug
* @return Object See https://dailymed.nlm.nih.gov/dailymed/app-support-web-services.cfm
*/
public function getSPLInfo($name) {
//$response = \Httpful\Request::get($this->baseUrl . 'https://dailymed.nlm.nih.gov/dailymed/services/v2/spls.json?drug_name=' . urlencode($name))
$response = \Httpful\Request::get($this->baseUrl . 'spls.json?drug_name=' . $name)
->send();
if ($response->code == '404' || $response->code == '415' || $response->code == '500') throw new Exception('Unexpected service response ' . $response->code);
return $response->body;
}
}