-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.php
More file actions
300 lines (241 loc) · 8.63 KB
/
api.php
File metadata and controls
300 lines (241 loc) · 8.63 KB
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
<?php
// This must be done before all others, since a couple of the modules
// use configuration file values
require_once 'php/config.php';
require_once 'php/log.php';
require_once 'php/orm_bill.php';
require_once 'php/update_task.php';
require_once 'php/util.php';
$LOGGER = get_error_logger('api.php');
$LOGGER->set_level(ULOG_DEBUG);
$LOGGER->debug('----- CONNECTION -----');
// We use this during POST /update, to avoid race conditions with other requests
$UPDATE_LOCK = new FileLock(LOCK_FILE);
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$db) {
header('HTTP/1.1 500 Cannot connect to database');
send_text('');
exit;
}
register_shutdown_function(function() {
global $LOGGER;
$LOGGER->debug('Closing database');
global $db;
$db->close();
global $UPDATE_LOCK;
$UPDATE_LOCK->unlock();
// A convenience - if we end up dying for some reason, make sure that the
// browser gets a response even if we don't end up sending one
global $RESPONSE_SENT;
if (!$RESPONSE_SENT) {
$LOGGER->debug('Sending emergency text/plain empty response');
send_text('');
}
});
// Gets rid of bad behavior in Edge (Edge caches JSON requests)
header('Cache-Control: no-cache');
$get_router = new Router();
$post_router = new Router();
/*
* API:
*
* GET /bills?order=<KEY>+<DIR>&start=<INTEGER>&before=<UNIX-TIMESTAMP>&after=<UNIX-TIMESTAMP>&comittee=<NAME>
* All but order are optional.
*
* {
* 'bills': [{
* 'title': STRING,
* 'code': STRING,
* 'summary': STRING,
* 'cbo_url': STRING,
* 'pdf_url': STRING,
* 'financial': [
* {'timespan': YEARS, 'amount': DOLLARS}
* }],
* 'next': '/api.php/bills?order=bills+desc&start=...&...',
* 'update': '/api.php/update'
* }
*/
$get_router->attach('/bills', function($vars) use (&$LOGGER, &$db) {
$query_params = array();
$next_page_query_params = array();
/*
* The order parameter allows the frontend to request ordering from the
* backend - something it couldn't do otherwise because of the limits
* imposed by pagination.
*/
if (!isset($_GET['order'])) {
http404('order parameter is required');
}
// The format of order is 'param dir', where 'param' could be 'date',
// 'committee', or 'net' and order could be 'asc' or 'desc'
$order_param_dir = explode(' ', $_GET['order']);
if (count($order_param_dir) != 2) {
http404('order parameter is malformed');
}
$order_param = $order_param_dir[0];
$order_dir = $order_param_dir[1];
if (!in_array($order_param, array("date", "committee", "net"))) {
http404('order must order by date, committee or cost');
}
if (!in_array($order_dir, array("asc", "desc"))) {
http404('order must order as asc or desc');
}
$LOGGER->debug('order = {order}', $_GET);
$next_page_query_params['order'] = urlencode("$order_param $order_dir");
// Load up all the other URL parameters we care about, so that the ORM will
// consider them when we do our querying
if (isset($_GET['start'])) {
$LOGGER->debug('start = {start}', $_GET);
$query_params['start'] = force_int(
$_GET['start'],
'Starting row must be integer');
$current_offset = $query_params['start'];
} else {
$current_offset = 0;
}
if (isset($_GET['before'])) {
$LOGGER->debug('before = {before}', $_GET);
$query_params['before'] = force_int(
$_GET['before'],
'Before must be a Unix timestamp');
$next_page_query_params['before'] = $_GET['before'];
}
if (isset($_GET['after'])) {
$LOGGER->debug('after = {after}', $_GET);
$query_params['after'] = force_int(
$_GET['after'],
'After must be a Unix timestamp');
$next_page_query_params['after'] = $_GET['after'];
}
if (isset($_GET['committee'])) {
$LOGGER->debug('committee = {committee}', $_GET);
$query_params['committee'] = $_GET['committee'];
$next_page_query_params['committee'] = urlencode($_GET['committee']);
}
// The reason for PAGE_SIZE+1 is to simplify the API - it lets us do a
// check to see if we're on the last page before the frontend requets it,
// by seeing if we get all the elements back we expect or not.
//
// The drawback is that we throw away the final element; we can't include
// it since we're bound to the configured PAGE_SIZE
$bills = Bill::from_query($db, $order_param, $order_dir, $query_params, PAGE_SIZE + 1);
$bill_array = array();
$response = array();
// We need this to figure out where this page ends, so we can make a URL
// for the next page
$generate_next_page = false;
foreach ($bills as $bill) {
// If this is the case, we're looking at our sentinel "+1 bill", which
// we can't return
if (count($bill_array) == PAGE_SIZE) {
$generate_next_page = true;
break;
}
array_push($bill_array, $bill->to_array());
}
$response['bills'] = $bill_array;
// Generate the URL to the next page, for pagination purposes - it has to
// include not only the page offset, but also all the query parameters
$next_page_query_params['start'] = $current_offset + count($bill_array);
if ($generate_next_page) {
$response_params = array();
foreach ($next_page_query_params as $key => $value) {
array_push($response_params, $key . "=" . $value);
}
$response['next'] = '/api.php/bills?' . implode('&', $response_params);
} else {
$response['next'] = null;
}
if (should_run_update_task($db)) {
$response['update'] = '/api.php/update';
} else {
$response['update'] = null;
}
$LOGGER->debug('Next page URL: {next_url}', array('next_url' => $response['next']));
send_json($response);
});
/*
* GET /bills/pending
* Returns a selection of bills which need reviewing, in the same format as
* GET /bills. Includes pagination, but no 'update' field. Note that these
* are implicitly ordered by ascending age, to ensure that outstanding bills
* are given first.
*/
$get_router->attach('/bills/pending', function($vars) use (&$db, &$LOGGER) {
if (isset($_GET['start'])) {
$LOGGER->debug('start = {start}', $_GET);
$current_offset = force_int(
$_GET['start'],
'Starting row must be integer');
} else {
$current_offset = 0;
}
$LOGGER->debug('Pulling pending bills');
$bills = Bill::from_pending($db, $current_offset, PAGE_SIZE + 1);
$bill_array = array();
$response = array();
$generate_next_page = false;
foreach ($bills as $bill) {
// If this is the case, we're looking at our sentinel "+1 bill", which
// we can't return
if (count($bill_array) == PAGE_SIZE) {
$generate_next_page = true;
break;
}
array_push($bill_array, $bill->to_array());
}
$response['bills'] = $bill_array;
if ($generate_next_page) {
$response['next'] = fmt_string(
'/api.php/bills/pending?start={next}',
array('next' => $current_offset + count($bill_array)));
} else {
$response['next'] = null;
}
$LOGGER->debug('Next page URL: {next_url}', array('next_url' => $response['next']));
send_json($response);
});
/*
* POST /bills
*
* This takes the pending bill provided on the input stream, and makes a
* fully-fledged bill out of it.
*/
$post_router->attach('/bills', function($vars) use (&$db, &$LOGGER) {
$request = read_json();
$LOGGER->debug('Parsing bill into request');
$bill = Bill::from_array($request);
$LOGGER->debug('Writing pending bill to database');
$bill->finalize($db, true);
send_text('');
});
/*
* POST /update
*
* This invokes the update script which pulls information from the CBO and adds
* to our PendingBills table.
*/
$post_router->attach('/update', function($vars) use (&$db, &$LOGGER, &$UPDATE_LOCK) {
$LOGGER->debug('Checking on update task');
if (!$UPDATE_LOCK->lock()) {
$LOGGER->debug('Locking failed, update in progress');
return;
}
if (should_run_update_task($db)) {
run_update_task($db);
}
$UPDATE_LOCK->unlock();
});
$ok = false;
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$ok = $get_router->invoke($_SERVER['PATH_INFO']);
break;
case 'POST':
$ok = $post_router->invoke($_SERVER['PATH_INFO']);
break;
}
if (!$ok) {
header('HTTP/1.1 404 Not found');
}