Skip to content

Commit ed68206

Browse files
committed
feature: list, get, and download methods
0 parents  commit ed68206

14 files changed

Lines changed: 1478 additions & 0 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.pyc
2+
.venv
3+
venv
4+
.vscode
5+
.env.development.secrets

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.13

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# deepinspection python
2+
3+
Python client library for deepinspection. Designed to simplify and exemplify interaction with the External API.
4+
5+
## Usage
6+
7+
```python
8+
import deepinspection
9+
10+
11+
client = deepinspection.track.client(
12+
deepinspection_customer="customer-identifier",
13+
deepinspection_client_id="XYZ",
14+
deepinspection_client_secret="XYZ",
15+
)
16+
17+
# list exports
18+
exports = client.exports.fastenings.list()
19+
20+
# get export data
21+
for line in client.exports.fastenings.get_data(exports[0]["id"]):
22+
pass
23+
```
24+
25+
_The customer identifier should match with the website url `https://customer-identifier.track.deepinspection.io/`._
26+
27+
exports
28+
29+
```json
30+
[
31+
{
32+
"updated": "2024-02-01T23:20:20.605136+00:00",
33+
"id": "461731a1-96c8-4ed6-99e4-9fe424eb9c40",
34+
"measurement_name": "20231001_124327_2011T",
35+
"type": "fastenings",
36+
"downloaded": null,
37+
"created": "2024-02-01T23:20:20.605136+00:00",
38+
"user_id": "a338595f-6eba-481b-9f0f-112290a1078b"
39+
},
40+
...
41+
```
42+
43+
export_data
44+
45+
```json
46+
{"export_datetime": "2024-02-04T21:21:12.866787", "measurement": "20231001_124327_2011T"}
47+
{"id": "56fb582a-0280-43fa-81f3-2a444e7e4273", "position_geographical": {"track_section": "111",
48+
...
49+
```
50+
51+
## Installation
52+
53+
Use your preferred package manager:
54+
55+
```bash
56+
poetry add deepinspection
57+
```
58+
59+
or
60+
61+
```bash
62+
pip install deepinspection
63+
```

deepinspection/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import track

deepinspection/track/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .client import DeepInspectionTrackClient as client

deepinspection/track/client.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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)

poetry.lock

Lines changed: 1254 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[tool.poetry]
2+
name = "deepinspection-python"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["NextML"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.9,<3.11"
10+
requests = "^2.31.0"
11+
pydantic = "^2.6.0"
12+
13+
14+
[tool.poetry.group.dev.dependencies]
15+
black = "^24.1.1"
16+
pytest = "^8.0.0"
17+
ipykernel = "^6.29.0"
18+
ipywidgets = "^8.1.1"
19+
pydantic-settings = "^2.1.0"
20+
21+
[build-system]
22+
requires = ["poetry-core"]
23+
build-backend = "poetry.core.masonry.api"

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
python_files = *.py
3+
norecursedirs = venv .venv __pycache__ .git .pytest_cache
4+
testpaths = deepinspection tests

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)