Skip to content

Commit 359f08f

Browse files
authored
Merge pull request #10 from IoT-Inspector/playground-auth-header
Playground auth header
2 parents 08fc781 + b2211ea commit 359f08f

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

examples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example scripts
2+
3+
This directory contains examples scripts for simple tasks. Feel free to use and customize these scripts to your needs or
4+
just to use them as inspiration. Please be aware that there are not much error handling in these scripts.
5+
6+
* upload_firmware.py: example how to manage firmware uploads
7+
* get_tenant_token.py: The GraphQL Playground accessible at /api/graphql requires authentication. This script prints the
8+
required authentication header that can be used in playgroud.

examples/get_tenant_token.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import sys
3+
from getpass import getpass
4+
from iot_inspector_client import Client
5+
6+
API_URL = "https://demo.iot-inspector.com/api"
7+
EMAIL = sys.argv[1]
8+
PASSWORD = getpass()
9+
10+
11+
print("Login to IoT Inspector", EMAIL, "@", API_URL)
12+
client = Client(API_URL)
13+
client.login(EMAIL, PASSWORD)
14+
tenants = client.get_all_tenants()
15+
16+
print("Tenants:", ", ".join([tenant.name for tenant in tenants]))
17+
18+
if len(sys.argv) > 2:
19+
# Filter tenants that matches the provided pattern
20+
tenants = filter(lambda tenant: sys.argv[2] in tenant.name, tenants)
21+
22+
# Pick the first one
23+
tenant = tenants[0]
24+
25+
print("Using tenant:", tenant.name)
26+
client.use_tenant(tenant)
27+
28+
print(json.dumps(client.get_auth_headers()))

iot_inspector_client/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,14 @@ def _post(self, path: str, headers: Optional[Dict] = None, **kwargs):
136136

137137
@_tenant_required
138138
def _post_with_token(self, path: str, **kwargs):
139-
headers = {"Authorization": "Bearer " + self._state.raw_tenant_token}
139+
headers = self.get_auth_headers()
140140

141141
return self._post(path, headers, **kwargs)
142142

143+
@_tenant_required
144+
def get_auth_headers(self):
145+
return {"Authorization": "Bearer " + self._state.raw_tenant_token}
146+
143147
@_login_required
144148
def get_tenant(self, name: str):
145149
"""Get Tenant by name. Raises KeyError if not found."""

0 commit comments

Comments
 (0)