|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Generator, List |
| 5 | + |
| 6 | +import requests |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | + |
| 10 | +class Fastenings(BaseModel): |
| 11 | + client: DeepInspectionTrackClient |
| 12 | + |
| 13 | + def list(self) -> List[dict]: |
| 14 | + url = f"{self.client.base_url()}/exports/fastenings" |
| 15 | + response = requests.get(url, headers=self.client.auth_headers()) |
| 16 | + response.raise_for_status() |
| 17 | + return response.json() |
| 18 | + |
| 19 | + def get(self, export_id) -> dict: |
| 20 | + url = f"{self.client.base_url()}/exports/fastenings/{export_id}" |
| 21 | + response = requests.get(url, headers=self.client.auth_headers()) |
| 22 | + response.raise_for_status() |
| 23 | + return response.json() |
| 24 | + |
| 25 | + def get_data(self, export_id) -> Generator[dict, None, None]: |
| 26 | + url = f"{self.client.base_url()}/exports/fastenings/{export_id}/data" |
| 27 | + response = requests.get(url, headers=self.client.auth_headers(), stream=True) |
| 28 | + response.raise_for_status() |
| 29 | + |
| 30 | + for line in response.iter_lines(): |
| 31 | + if line: |
| 32 | + yield json.loads(line.decode("utf-8")) |
| 33 | + |
| 34 | + |
| 35 | +class Exports(BaseModel): |
| 36 | + client: DeepInspectionTrackClient |
| 37 | + |
| 38 | + @property |
| 39 | + def fastenings(self): |
| 40 | + return Fastenings(client=self.client) |
| 41 | + |
| 42 | + |
| 43 | +class DeepInspectionTrackClient(BaseModel): |
| 44 | + customer_identifier: str |
| 45 | + client_id: str |
| 46 | + client_secret: str |
| 47 | + |
| 48 | + def base_url(self): |
| 49 | + return f"https://{self.customer_identifier}.api.track.deepinspection.io/external/v0-alpha" |
| 50 | + |
| 51 | + def auth_headers(self): |
| 52 | + return { |
| 53 | + "Authorization": f"Bearer {self.access_token()}", |
| 54 | + "X-Customer-ID": self.customer_identifier, |
| 55 | + } |
| 56 | + |
| 57 | + def openid_connect_url(self): |
| 58 | + return f"https://auth.nextml.com/auth/realms/deepinspection-track-{self.customer_identifier}/.well-known/openid-configuration" |
| 59 | + |
| 60 | + def access_token(self) -> str: |
| 61 | + response = requests.get(self.openid_connect_url()) |
| 62 | + if response.status_code != 200: |
| 63 | + raise Exception("Failed to authenticate with OIDC") |
| 64 | + |
| 65 | + oidc_discoveries = response.json() |
| 66 | + token_url = oidc_discoveries["token_endpoint"] |
| 67 | + |
| 68 | + response = requests.post( |
| 69 | + token_url, |
| 70 | + data=dict(grant_type="client_credentials"), |
| 71 | + auth=(self.client_id, self.client_secret), |
| 72 | + ) |
| 73 | + |
| 74 | + if response.status_code != 200: |
| 75 | + raise Exception( |
| 76 | + f"Failed to authenticate, status code {response.status_code}, text {response.text}" |
| 77 | + ) |
| 78 | + |
| 79 | + return response.json()["access_token"] |
| 80 | + |
| 81 | + @property |
| 82 | + def exports(self): |
| 83 | + return Exports(client=self) |
0 commit comments