-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_get.py
143 lines (110 loc) · 3.46 KB
/
http_get.py
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
import requests
import json
from requests.exceptions import HTTPError
from http_config import *
device_urls = {
'dl-pr-26_5100' : pressure_url,
'dl-mbx_5248' : ultrasonic1_url,
'dl-mbx_5249' : ultrasonic2_url,
'dl-atm41_5245' : weather_url,
}
device_keys = {
'dl-pr-26_5100' : pressure_key,
'dl-mbx_5248' : ultrasonic1_key,
'dl-mbx_5249' : ultrasonic2_key,
'dl-atm41_5245' : weather_key,
}
def get_headers(key):
headers = {
'Accept': 'application/json',
'Authorization': 'key ' + key,
}
return headers
def get_devices_ids_by_type(dev_type):
""" Gets the JSON list of devices for which data has been stored
Parameters
----------
device type 'pressure' or 'weather'
Returns
-------
list of devices ids of all devices accross types
TODO
----
clean output currently returns with brackets and quotes
"""
url = device_urls[dev_type] + 'devices'
headers = get_headers(device_keys[dev_type])
return get_response(url, headers)
def get_all_device_ids():
""" Gets the JSON list of devices for which data has been stored
Parameters
----------
None
Returns
-------
list of devices ids of all devices accross types
TODO
----
clean output currently returns with brackets and quotes
"""
device_ids = []
for dev_type, url_base in device_urls.items():
url = url_base + 'devices'
headers = get_headers(device_keys[dev_type])
device_ids.append(get_response(url, headers))
return device_ids
def get_data(dev_type, time='1h'):
""" Query the data for all devices
Parameters
----------
time : str
Duration on which we want to get the data (default 1h).
Pass 30s for the last 30 seconds, 1h for the last hour,
2d for the last 48 hours, etc
Returns
-------
returns the JSON formated list data for all devices
NOTE
----
- units return as ASCII characters, regular print(get_data(stuff)) will throw UnicodeEncodeError,
instead use print(json.dumps(get_data(stuff)), be sure to 'import json' first
"""
url = device_urls[dev_type] + 'query'
headers = get_headers(device_keys[dev_type])
params = {'last':time}
return get_response(url, headers , params)
def get_response(url, headers={}, params={}):
"""
Parameters
----------
url : str
URL to TTN endpoint to query
params : dict
parameters for url
Returns
-------
JSON response for url if successfull else None
Raises
------
HTTPError:
If HTTP error occurs on TNN server side
NOTE
----
- units return as ASCII characters, regular print(get_response(stuff)) will throw UnicodeEncodeError,
instead use print(json.dumps(get_response(stuff)), be sure to 'import json' first
"""
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
if (response.status_code == 200):
response_json = response.text # list of dicts
# print(json.dumps(response_json, indent = 2)) # printing json.loads for nice formatting
return response_json
else:
return None
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
if __name__ == "__main__":
print(json.dumps(get_data('weather')))