Skip to content

Commit 767b574

Browse files
author
Haakon Robinson
committed
Paged requests to fetch all data in time range
1 parent 9bafe26 commit 767b574

1 file changed

Lines changed: 69 additions & 34 deletions

File tree

demo.py

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22
import os
3+
import sys
4+
from time import sleep
35
from urllib.parse import urlencode
46

57
import requests
@@ -29,44 +31,77 @@
2931
# Make a POST request to the token endpoint
3032
token_response = requests.post(keycloak_url, data=token_data)
3133

32-
print(token_response.json())
33-
3434
# Pick the access token from the response
3535
access_token = token_response.json()["access_token"]
3636

37+
# Print the token (for use with Swagger UI)
38+
print(token_response.json())
3739

38-
### Using the NGI Live API
40+
# Using the NGI Live API
3941
api_base_url = "https://api.ngilive.no"
4042

41-
if token_response.status_code == 200:
42-
print("Getting sensors...")
43-
api_response = requests.get(
44-
f"{api_base_url}/projects/{project_id}/sensors",
45-
headers={
46-
"Authorization": f"Bearer {access_token}",
47-
"Content-Type": "application/json",
48-
},
49-
)
50-
51-
print(f"Sensor response: {api_response.status_code}")
52-
print(json.dumps(api_response.json(), indent=2))
53-
54-
## Getting 100 datapoints
55-
56-
api_response = requests.get(
57-
f"{api_base_url}/projects/{project_id}/datapoints/json_array_v0?{urlencode({
58-
"start" : "2023-01-01T09:15:30Z",
59-
"end" : "2025-01-01T09:15:30Z",
60-
"limit" : 100
61-
}
62-
)}",
63-
headers={
64-
"Authorization": f"Bearer {access_token}",
65-
"Content-Type": "application/json",
66-
},
67-
)
68-
69-
print(f"Sensor response: {api_response.status_code}")
70-
print(json.dumps(api_response.json(), indent=2))
71-
else:
43+
if token_response.status_code != 200:
7244
print(f"Error: {token_response.status_code}, {token_response.text}")
45+
sys.exit(1)
46+
47+
# Request sensor metadata and print result
48+
print("Getting sensors...")
49+
api_response = requests.get(
50+
f"{api_base_url}/projects/{project_id}/sensors",
51+
headers={
52+
"Authorization": f"Bearer {access_token}",
53+
"Content-Type": "application/json",
54+
},
55+
)
56+
57+
print(f"Sensor response: {api_response.status_code}")
58+
resp = api_response.json()
59+
print(json.dumps(resp, indent=2))
60+
61+
# Request parameters for datapoints
62+
LIMIT = 1000
63+
START = "2025-02-24T09:15:30Z"
64+
END = "2025-03-01T09:15:30Z"
65+
66+
# Create a dict to hold all sensor data
67+
all_sensor_data = {s["name"]: [] for s in resp["sensors"]}
68+
69+
# Loop over all sensors and request data in pages with size LIMIT
70+
for sensor, sensor_data in all_sensor_data.items():
71+
offset = 0
72+
while True:
73+
sleep(0.11)
74+
75+
# Request LIMIT datapoints at a time
76+
url = f"{api_base_url}/projects/{project_id}/datapoints/json_array_v0?{urlencode({
77+
"name": sensor,
78+
"start" : START,
79+
"end" : END,
80+
"limit" : LIMIT,
81+
"offset" : offset,
82+
}
83+
)}"
84+
85+
api_response = requests.get(
86+
url,
87+
headers={
88+
"Authorization": f"Bearer {access_token}",
89+
"Content-Type": "application/json",
90+
},
91+
)
92+
93+
# Print response code for this page
94+
print(f"{f"{sensor} (page {offset // LIMIT}):": <20} STATUS {api_response.status_code}")
95+
96+
# Extract data and add to dict of sensor data
97+
resp = api_response.json()
98+
page, *_ = resp["data"]
99+
sensor_data.extend(page["data"])
100+
101+
# Go to next sensor if we've fetched all data for this sensor
102+
if len(page["data"]) < LIMIT:
103+
break
104+
else:
105+
offset += LIMIT
106+
107+
print(all_sensor_data)

0 commit comments

Comments
 (0)