Skip to content

Commit 76da993

Browse files
committed
feat(api) add user data to all api requests
1 parent 39d7081 commit 76da993

5 files changed

Lines changed: 56 additions & 10 deletions

File tree

examples/workloads/create_catalogue_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
ManifestBody=manifest_body,
4444
Version=catalogue_version,
4545
Description='Updating versions via sdk',
46-
catalogue_id=catalogue["Id"]
46+
catalogue_id=catalogue_id
4747
)
4848
print(response)

staxapp/api.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
import requests
24

35
from staxapp.config import Config
@@ -7,6 +9,20 @@
79
class Api:
810
_requests_auth = None
911

12+
@classmethod
13+
def _headers(cls, custom_headers) -> dict:
14+
headers = {
15+
**custom_headers,
16+
"User-Agent": json.dumps(
17+
{
18+
"platform": Config.platform,
19+
"python_version": Config.python_version,
20+
"sdk_version": Config.sdk_version,
21+
}
22+
),
23+
}
24+
return headers
25+
1026
@classmethod
1127
def _auth(cls, **kwargs):
1228
if not cls._requests_auth:
@@ -27,7 +43,13 @@ def get(cls, url_frag, params={}, **kwargs):
2743
url_frag = url_frag.replace(f"/{Config.API_VERSION}", "")
2844
url = f"{Config.api_base_url()}/{url_frag.lstrip('/')}"
2945

30-
response = requests.get(url, auth=cls._auth(), params=params, **kwargs)
46+
response = requests.get(
47+
url,
48+
auth=cls._auth(),
49+
params=params,
50+
headers=cls._headers(kwargs.get("headers", {})),
51+
**kwargs,
52+
)
3153
cls.handle_api_response(response)
3254
return response.json()
3355

@@ -36,7 +58,13 @@ def post(cls, url_frag, payload={}, **kwargs):
3658
url_frag = url_frag.replace(f"/{Config.API_VERSION}", "")
3759
url = f"{Config.api_base_url()}/{url_frag.lstrip('/')}"
3860

39-
response = requests.post(url, json=payload, auth=cls._auth(), **kwargs)
61+
response = requests.post(
62+
url,
63+
json=payload,
64+
auth=cls._auth(),
65+
headers=cls._headers(kwargs.get("headers", {})),
66+
**kwargs,
67+
)
4068
cls.handle_api_response(response)
4169
return response.json()
4270

@@ -45,7 +73,13 @@ def put(cls, url_frag, payload={}, **kwargs):
4573
url_frag = url_frag.replace(f"/{Config.API_VERSION}", "")
4674
url = f"{Config.api_base_url()}/{url_frag.lstrip('/')}"
4775

48-
response = requests.put(url, json=payload, auth=cls._auth(), **kwargs)
76+
response = requests.put(
77+
url,
78+
json=payload,
79+
auth=cls._auth(),
80+
headers=cls._headers(kwargs.get("headers", {})),
81+
**kwargs,
82+
)
4983
cls.handle_api_response(response)
5084
return response.json()
5185

@@ -54,6 +88,12 @@ def delete(cls, url_frag, params={}, **kwargs):
5488
url_frag = url_frag.replace(f"/{Config.API_VERSION}", "")
5589
url = f"{Config.api_base_url()}/{url_frag.lstrip('/')}"
5690

57-
response = requests.delete(url, auth=cls._auth(), params=params, **kwargs)
91+
response = requests.delete(
92+
url,
93+
auth=cls._auth(),
94+
params=params,
95+
headers=cls._headers(kwargs.get("headers", {})),
96+
**kwargs,
97+
)
5898
cls.handle_api_response(response)
5999
return response.json()

staxapp/config.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import logging
22
import os
3+
import platform as sysinfo
34

45
import requests
56

7+
import staxapp
68
from staxapp.exceptions import ApiException
79

810
logging.getLogger().setLevel(logging.DEBUG)
@@ -32,17 +34,21 @@ class Config:
3234
expiration = None
3335
load_live_schema = False
3436

37+
platform = (sysinfo.platform(),)
38+
python_version = (sysinfo.python_version(),)
39+
sdk_version = (staxapp.__version__,)
40+
3541
@classmethod
3642
def set_config(cls):
37-
cls.base_url = f"https://api.{cls.STAX_REGION}/{cls.API_VERSION}"
43+
cls.base_url = f"https://{cls.hostname}/{cls.API_VERSION}"
3844
config_url = f"{cls.api_base_url()}/public/config"
3945
config_response = requests.get(config_url)
4046
try:
4147
config_response.raise_for_status()
4248
except requests.exceptions.HTTPError as e:
4349
logging.error(f"{config_response.status_code}: {config_response.json()}")
4450
raise ApiException(
45-
str(e), config_response, detail="Could not load API config."
51+
str(e), config_response, detail=" Could not load API config."
4652
)
4753

4854
cls.api_config = config_response.json()

staxapp/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, message, response, detail=""):
1212
else:
1313
logging.error(f"{response.status_code}: {response.json()}")
1414
self.message = f"Api Exception:{detail} {message}"
15-
except JSONDecodeError:
15+
except:
1616
if response.content:
1717
logging.error(f"{response.status_code}: {response.content}")
1818
else:

staxapp/openapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from staxapp.api import Api
77
from staxapp.auth import ApiTokenAuth
8-
from staxapp.config import ApiException, Config
8+
from staxapp.config import Config
99
from staxapp.contract import StaxContract
10-
from staxapp.exceptions import ValidationException
10+
from staxapp.exceptions import ApiException, ValidationException
1111

1212

1313
class StaxClient:

0 commit comments

Comments
 (0)