Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c7579f4

Browse files
authoredSep 8, 2020
Merge pull request #210 from sgratzl/sgratzl/csv
csv and raw json
2 parents abb4f83 + b822a93 commit c7579f4

File tree

3 files changed

+129
-3
lines changed

3 files changed

+129
-3
lines changed
 

‎integrations/server/test_covidcast.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,76 @@ def test_round_trip(self):
7979
'message': 'success',
8080
})
8181

82+
83+
def test_csv_format(self):
84+
"""Test generate csv data."""
85+
86+
# insert dummy data
87+
self.cur.execute('''
88+
insert into covidcast values
89+
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
90+
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 1, False)
91+
''')
92+
self.cnx.commit()
93+
94+
# make the request
95+
response = requests.get(BASE_URL, params={
96+
'source': 'covidcast',
97+
'data_source': 'src',
98+
'signal': 'sig',
99+
'time_type': 'day',
100+
'geo_type': 'county',
101+
'time_values': 20200414,
102+
'geo_value': '01234',
103+
'format': 'csv'
104+
})
105+
response.raise_for_status()
106+
response = response.text
107+
108+
# assert that the right data came back
109+
self.assertEqual(response,
110+
"""geo_value,signal,time_value,direction,issue,lag,value,stderr,sample_size
111+
01234,sig,20200414,4,20200414,0,1.5,2.5,3.5
112+
""")
113+
114+
def test_raw_json_format(self):
115+
"""Test generate raw json data."""
116+
117+
# insert dummy data
118+
self.cur.execute('''
119+
insert into covidcast values
120+
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
121+
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 1, False)
122+
''')
123+
self.cnx.commit()
124+
125+
# make the request
126+
response = requests.get(BASE_URL, params={
127+
'source': 'covidcast',
128+
'data_source': 'src',
129+
'signal': 'sig',
130+
'time_type': 'day',
131+
'geo_type': 'county',
132+
'time_values': 20200414,
133+
'geo_value': '01234',
134+
'format': 'json'
135+
})
136+
response.raise_for_status()
137+
response = response.json()
138+
139+
# assert that the right data came back
140+
self.assertEqual(response, [{
141+
'time_value': 20200414,
142+
'geo_value': '01234',
143+
'value': 1.5,
144+
'stderr': 2.5,
145+
'sample_size': 3.5,
146+
'direction': 4,
147+
'issue': 20200414,
148+
'lag': 0,
149+
'signal': 'sig',
150+
}])
151+
82152
def test_fields(self):
83153
"""Test to limit fields field"""
84154

‎src/server/api.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,13 @@ function meta_delphi() {
15661566
$data['message'] = 'database error';
15671567
}
15681568

1569-
// send the response as a json object
1570-
header('Content-Type: application/json');
1571-
echo json_encode($data);
1569+
if(isset($_REQUEST['format']) && $_REQUEST['format'] == "csv") {
1570+
send_csv($data);
1571+
} else if(isset($_REQUEST['format']) && $_REQUEST['format'] == "json") {
1572+
send_json($data);
1573+
} else {
1574+
// send the response as a json object
1575+
header('Content-Type: application/json');
1576+
echo json_encode($data);
1577+
}
15721578
?>

‎src/server/api_helpers.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,55 @@ function record_analytics($source, $data) {
367367
mysqli_query($dbh, "INSERT INTO `api_analytics` (`datetime`, `ip`, `ua`, `source`, `result`, `num_rows`) VALUES (now(), '{$ip}', '{$ua}', '{$source}', {$result}, {$num_rows})");
368368
}
369369

370+
function send_status(&$data) {
371+
if (intval($data["result"]) > 0 || intval($data["result"]) == -2) {
372+
return FALSE;
373+
}
374+
if ($data["message"] == 'database error') {
375+
http_response_code(500);
376+
} else if ($data["message"] == 'unauthenticated') {
377+
http_response_code(401);
378+
} else {
379+
http_response_code(400); // bad request
380+
}
381+
header('Content-Type: application/json');
382+
echo json_encode($data);
383+
return TRUE;
384+
}
385+
386+
function send_csv(&$data) {
387+
if (send_status($data)) {
388+
return;
389+
}
390+
header('Content-Type: text/csv');
391+
header('Content-Disposition: attachment; filename=epidata.csv');
392+
393+
if (intval(data["result"]) == -2) {
394+
// empty
395+
return;
396+
}
397+
398+
$rows = $data["epidata"];
399+
$headers = array_keys($rows[0]);
400+
$out = fopen('php://output', 'w');
401+
fputcsv($out, $headers);
402+
foreach ($rows as $row) {
403+
fputcsv($out, $row);
404+
}
405+
fclose($out);
406+
}
407+
408+
function send_json(&$data) {
409+
if (send_status($data)) {
410+
return;
411+
}
412+
header('Content-Type: application/json');
413+
414+
if (intval($data["result"]) == -2) {
415+
echo json_encode(array());
416+
} else {
417+
echo json_encode($data["epidata"]);
418+
}
419+
}
370420

371421
?>

0 commit comments

Comments
 (0)
Please sign in to comment.