-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
107 lines (86 loc) · 3.05 KB
/
demo.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
import json
import os
import sys
from time import sleep
from urllib.parse import urlencode
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv(override=True)
# Constants
keycloak_url = "https://keycloak.ngiapi.no/auth/realms/tenant-geohub-public/protocol/openid-connect/token"
client_id = os.environ.get("KEYCLOAK_CLIENT_ID")
client_secret = os.environ.get("KEYCLOAK_CLIENT_SECRET")
project_id = os.environ.get("PROJECT_ID")
if not (client_id and client_secret):
print("Client ID or secret not found in environment variables.")
# Handle the case where environment variables are not set
exit()
# Set up the payload for the token request
token_data = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
}
# Make a POST request to the token endpoint
token_response = requests.post(keycloak_url, data=token_data)
# Pick the access token from the response
access_token = token_response.json()["access_token"]
# Print the token (for use with Swagger UI)
print(token_response.json())
# Using the NGI Live API
api_base_url = "https://api.ngilive.no"
if token_response.status_code != 200:
print(f"Error: {token_response.status_code}, {token_response.text}")
sys.exit(1)
# Request sensor metadata and print result
print("Getting sensors...")
api_response = requests.get(
f"{api_base_url}/projects/{project_id}/sensors",
headers={
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
},
)
print(f"Sensor response: {api_response.status_code}")
resp = api_response.json()
print(json.dumps(resp, indent=2))
# Request parameters for datapoints
LIMIT = 1000
START = "2025-02-24T09:15:30Z"
END = "2025-03-01T09:15:30Z"
# Create a dict to hold all sensor data
all_sensor_data = {s["name"]: [] for s in resp["sensors"]}
# Loop over all sensors and request data in pages with size LIMIT
for sensor, sensor_data in all_sensor_data.items():
offset = 0
while True:
sleep(0.11)
# Request LIMIT datapoints at a time
url = f"{api_base_url}/projects/{project_id}/datapoints/json_array_v0?{urlencode({
"name": sensor,
"start" : START,
"end" : END,
"limit" : LIMIT,
"offset" : offset,
}
)}"
api_response = requests.get(
url,
headers={
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
},
)
# Print response code for this page
print(f"{f"{sensor} (page {offset // LIMIT}):": <20} STATUS {api_response.status_code}")
# Extract data and add to dict of sensor data
resp = api_response.json()
page, *_ = resp["data"]
sensor_data.extend(page["data"])
# Go to next sensor if we've fetched all data for this sensor
if len(page["data"]) < LIMIT:
break
else:
offset += LIMIT
print(all_sensor_data)