Skip to content

Commit 9bafe26

Browse files
committed
init
0 parents  commit 9bafe26

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
KEYCLOAK_CLIENT_ID=
2+
KEYCLOAK_CLIENT_SECRET=
3+
PROJECT_ID=

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.venv
2+
__pycache__
3+
.env

demo.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import json
2+
import os
3+
from urllib.parse import urlencode
4+
5+
import requests
6+
from dotenv import load_dotenv
7+
8+
# Load environment variables from .env file
9+
load_dotenv(override=True)
10+
11+
# Constants
12+
keycloak_url = "https://keycloak.ngiapi.no/auth/realms/tenant-geohub-public/protocol/openid-connect/token"
13+
client_id = os.environ.get("KEYCLOAK_CLIENT_ID")
14+
client_secret = os.environ.get("KEYCLOAK_CLIENT_SECRET")
15+
project_id = os.environ.get("PROJECT_ID")
16+
17+
if not (client_id and client_secret):
18+
print("Client ID or secret not found in environment variables.")
19+
# Handle the case where environment variables are not set
20+
exit()
21+
22+
# Set up the payload for the token request
23+
token_data = {
24+
"grant_type": "client_credentials",
25+
"client_id": client_id,
26+
"client_secret": client_secret,
27+
}
28+
29+
# Make a POST request to the token endpoint
30+
token_response = requests.post(keycloak_url, data=token_data)
31+
32+
print(token_response.json())
33+
34+
# Pick the access token from the response
35+
access_token = token_response.json()["access_token"]
36+
37+
38+
### Using the NGI Live API
39+
api_base_url = "https://api.ngilive.no"
40+
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:
72+
print(f"Error: {token_response.status_code}, {token_response.text}")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
python-dotenv

0 commit comments

Comments
 (0)