forked from cyberwolf/cnapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnapi.api.inc
162 lines (140 loc) · 5.64 KB
/
cnapi.api.inc
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
<?php
/**
* Make a request to the CultuurNet API using the defaults added by this module and others. The request will also be cleaned to remove redundant parameters before firing off the request.
*
* @param $request
* A CNAPI request array (@see _cnapi_get) representing the request.
*
* @return
* An array representing the answer (@see _cnapi_get) from the CultuurNet API in case of success. FALSE in case an error occured.
*/
function cnapi_get($request) {
cnapi_request_add_defaults($request);
cnapi_request_clean($request);
return cnapi_api_request_cached($request);
}
/**
* Make a request to the CultuurNet API.
*
* @param $request
* A CNAPI request array (@see _cnapi_get) representing the request.
*
* @return
* An array representing the answer (@see _cnapi_get) from the CultuurNet API in case of success. FALSE in case an error occured.
*/
function cnapi_get_raw($request) {
return cnapi_api_request_cached($request);
}
/**
* Request an event from the the CultuurNet API.
*
* @param $cdib
* The CDBID of the requested event.
* @param $related_events
* Specifies if events related to the requested event should be fetched too and how these should be formatted. The options are:
* - CNAPI_LIST_IGNORE : Do not fetch related events.
* - CNAPI_LIST_SUMMARY : Fetch a summary of all related events.
* - CNAPI_LIST_DETAIL : Fetch the full info of related events.
* - CNAPI_LIST_ID : Fetch the CDBIDs of related events.
*
* @return
* An array representing the event (@see _cnapi_get) from the CultuurNet API in case of success. FALSE in case an error occured.
*/
function cnapi_get_event($cdbid, $related_events = CNAPI_LIST_IGNORE) {
$request = array('action' => 'detail', 'type' => 'event', 'query' => array('cdbid' => $cdbid));
if ($related_events != CNAPI_LIST_IGNORE) {
$request['query']['relatedevents'] = ($related_events == CNAPI_LIST_SUMMARY ? 'list' : 'detail');
}
return cnapi_get($request);
}
/**
* Request a production from the the CultuurNet API.
*
* @param $cdib
* The CDBID of the requested production.
* @param $related_events
* Specifies if events related to the requested production should be fetched too and how these should be formatted. The options are:
* - CNAPI_LIST_IGNORE : Do not fetch related events.
* - CNAPI_LIST_SUMMARY : Fetch a summary of all related events.
* - CNAPI_LIST_DETAIL : Fetch the full info of related events.
* - CNAPI_LIST_ID : Fetch the CDBIDs of related events.
*
* @return
* An array representing the production (@see _cnapi_get) from the CultuurNet API in case of success. FALSE in case an error occured.
*/
function cnapi_get_production($cdbid, $related_events = CNAPI_LIST_IGNORE) {
$request = array('action' => 'detail', 'type' => 'production', 'query' => array('cdbid' => $cdbid));
if ($related_events != CNAPI_LIST_IGNORE) {
$request['query']['relatedevents'] = ($related_events == CNAPI_LIST_SUMMARY ? 'list' : 'detail');
}
return cnapi_get($request);
}
/**
* Request an actor from the the CultuurNet API.
*
* @param $cdib
* The CDBID of the requested actor.
*
* @return
* An array representing the actor (@see _cnapi_get) from the CultuurNet API in case of success. FALSE in case an error occured.
*/
function cnapi_get_actor($cdbid) {
$request = array('action' => 'detail', 'type' => 'actor', 'query' => array('cdbid' => $cdbid));
return cnapi_get($request);
}
function cnapi_get_events($query = NULL, $action = CNAPI_LIST_SUMMARY) {
$request = array('action' => ($action == CNAPI_LIST_SUMMARY ? 'list_summary' : 'list_detail'), 'type' => 'event', 'query' => $query);
return cnapi_get($request);
}
function cnapi_get_productions($query = NULL, $action = CNAPI_LIST_SUMMARY) {
$request = array('action' => ($action == CNAPI_LIST_SUMMARY ? 'list_summary' : 'list_detail'), 'type' => 'production', 'query' => $query);
return cnapi_get($request);
}
function cnapi_get_actors($query = NULL, $action = CNAPI_LIST_SUMMARY) {
$request = array('action' => ($action == CNAPI_LIST_SUMMARY ? 'list_summary' : 'list_detail'), 'type' => 'actor', 'query' => $query);
return cnapi_get($request);
}
function cnapi_get_report($type, $query = array(), $parse_geo = TRUE) {
$request = array('action' => 'report', 'type' => $type, 'query' => $query, 'options' => array('parse_geo' => $parse_geo));
return cnapi_get($request);
}
function cnapi_api_request_cached($request) {
if (!cnapi_request_validate($request)) {
watchdog('cnapi', 'Invalid API request !request.', array('!request' => serialize($request)), WATCHDOG_ERROR);
return FALSE;
}
$result = NULL;
$cache = _cnapi_cache_get($request);
if ($cache != NULL) {
$result = $cache;
}
else {
$result = cnapi_api_request($request);
if ($result) {
$result = _cnapi_parse($request, $result);
if ($result) {
if (in_array($request['action'], array('list_detail', 'list_summary'))) {
_cnapi_cache_list_clean_by_total($request, $result['total']);
}
_cnapi_cache_set($request, $result);
}
else {
watchdog('cnapi', 'Failed parsing for request !request.', array('!request' => serialize($request)), WATCHDOG_ERROR);
return FALSE;
}
}
else {
watchdog('cnapi', 'Failed request !request.', array('!request' => serialize($request)), WATCHDOG_ERROR);
return FALSE;
}
}
return $result;
}
function cnapi_api_request($request) {
if (!isset($request['query']['format'])) {
$request['query']['format'] = 'xml';
}
$url = cnapi_url_p2a($request);
$url = rtrim(variable_get('cnapi_api_location', CNAPI_API_LOCATION), '/') . '/api/' . $url;
return cnapi_http_request($url);
}