Skip to content

Commit e3eda4d

Browse files
committed
feat(sdk): fix unit tests
fix unit tests
1 parent 5a9c04a commit e3eda4d

11 files changed

Lines changed: 176 additions & 182 deletions

File tree

staxapp/api.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88

99
class Api:
10+
@classmethod
11+
def get_config(cls, config=None, **kwargs):
12+
if config is None:
13+
config = Config.GetDefaultConfig()
14+
return config
15+
1016
@classmethod
1117
def _headers(cls, custom_headers) -> dict:
1218
headers = {
@@ -23,12 +29,13 @@ def handle_api_response(response):
2329
raise ApiException(str(e), response)
2430

2531
@classmethod
26-
def get(cls, url_frag, auth, config, params={}, **kwargs):
32+
def get(cls, url_frag, params={}, config=None, **kwargs):
33+
config = cls.get_config(config)
2734
url_frag = url_frag.replace(f"/{config.API_VERSION}", "")
2835
url = f"{config.api_base_url()}/{url_frag.lstrip('/')}"
2936
response = requests.get(
3037
url,
31-
auth=auth,
38+
auth=config._auth(),
3239
params=params,
3340
headers=cls._headers(kwargs.get("headers", {})),
3441
**kwargs,
@@ -37,43 +44,46 @@ def get(cls, url_frag, auth, config, params={}, **kwargs):
3744
return response.json()
3845

3946
@classmethod
40-
def post(cls, url_frag, auth, config, payload={}, **kwargs):
47+
def post(cls, url_frag, payload={}, config=None, **kwargs):
48+
config = cls.get_config(config)
4149
url_frag = url_frag.replace(f"/{config.API_VERSION}", "")
4250
url = f"{config.api_base_url()}/{url_frag.lstrip('/')}"
4351

4452
response = requests.post(
4553
url,
4654
json=payload,
47-
auth=auth,
55+
auth=config._auth(),
4856
headers=cls._headers(kwargs.get("headers", {})),
4957
**kwargs,
5058
)
5159
cls.handle_api_response(response)
5260
return response.json()
5361

5462
@classmethod
55-
def put(cls, url_frag, auth, config, payload={}, **kwargs):
63+
def put(cls, url_frag, payload={}, config=None, **kwargs):
64+
config = cls.get_config(config)
5665
url_frag = url_frag.replace(f"/{config.API_VERSION}", "")
5766
url = f"{config.api_base_url()}/{url_frag.lstrip('/')}"
5867

5968
response = requests.put(
6069
url,
6170
json=payload,
62-
auth=auth,
71+
auth=config._auth(),
6372
headers=cls._headers(kwargs.get("headers", {})),
6473
**kwargs,
6574
)
6675
cls.handle_api_response(response)
6776
return response.json()
6877

6978
@classmethod
70-
def delete(cls, url_frag, auth, config, params={}, **kwargs):
79+
def delete(cls, url_frag, params={}, config=None, **kwargs):
80+
config = cls.get_config(config)
7181
url_frag = url_frag.replace(f"/{config.API_VERSION}", "")
7282
url = f"{config.api_base_url()}/{url_frag.lstrip('/')}"
7383

7484
response = requests.delete(
7585
url,
76-
auth=auth,
86+
auth=config._auth(),
7787
params=params,
7888
headers=cls._headers(kwargs.get("headers", {})),
7989
**kwargs,

staxapp/auth.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def id_token_from_cognito(
7171
elif e.response["Error"]["Code"] == "UserNotFoundException":
7272
raise InvalidCredentialsException(
7373
message=str(e),
74-
detail=f"Please check your Access Key {username} {password}, that you have created your Api Token and that you are using the right STAX REGION {self.config.hostname} {self.aws_region}",
74+
detail=f"Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION",
7575
)
7676
else:
7777
raise InvalidCredentialsException(
@@ -135,8 +135,11 @@ class RootAuth:
135135
def requests_auth(username, password, **kwargs):
136136
if StaxConfig.expiration and StaxConfig.expiration > datetime.now(timezone.utc):
137137
return StaxConfig.auth
138-
139-
return StaxAuth("JumaAuth").requests_auth(username, password, **kwargs)
138+
config = StaxConfig.GetDefaultConfig()
139+
config.init()
140+
config.access_key = username
141+
config.secret_key = password
142+
return StaxAuth("JumaAuth", config).requests_auth(**kwargs)
140143

141144

142145
class ApiTokenAuth:

staxapp/config.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ class Config:
1818
STAX_REGION = os.getenv("STAX_REGION", "au1.staxapp.cloud")
1919
API_VERSION = "20190206"
2020

21+
cached_api_config = dict()
2122
api_config = dict()
2223
access_key = None
2324
secret_key = None
2425
auth_class = None
26+
_requests_auth = None
2527
_initialized = False
2628
base_url = None
2729
_hostname = f"api.{STAX_REGION}"
30+
hostname = None
2831
org_id = None
2932
auth = None
3033
expiration = None
@@ -37,6 +40,13 @@ class Config:
3740
def set_config(self):
3841
self.base_url = f"https://{self.hostname}/{self.API_VERSION}"
3942
config_url = f"{self.api_base_url()}/public/config"
43+
if config_url == self.cached_api_config.get("caching"):
44+
self.api_config = self.cached_api_config
45+
else:
46+
self.api_config = Config.get_api_config(config_url)
47+
48+
@classmethod
49+
def get_api_config(cls, config_url):
4050
config_response = requests.get(config_url)
4151
try:
4252
config_response.raise_for_status()
@@ -45,20 +55,27 @@ def set_config(self):
4555
raise ApiException(
4656
str(e), config_response, detail=" Could not load API config."
4757
)
58+
cls.cached_api_config = config_response.json()
59+
cls.cached_api_config["caching"] = config_url
60+
return config_response.json()
4861

49-
self.api_config = config_response.json()
50-
51-
def init(self, config=None, hostname=None):
52-
if hostname is not None and self.hostname is None:
53-
self.hostname = hostname
62+
def init(self, hostname=None):
5463
if self._initialized:
5564
return
65+
if self.hostname is None:
66+
self.hostname = hostname
67+
if self.hostname is None:
68+
self.hostname = Config._hostname
5669

57-
if not config:
58-
self.set_config()
70+
self.set_config()
5971

6072
self._initialized = True
6173

74+
def _auth(self, **kwargs):
75+
if not self._requests_auth:
76+
self._requests_auth = self.get_auth_class().requests_auth
77+
return self._requests_auth(self, **kwargs)
78+
6279
def api_base_url(self):
6380
return self.base_url
6481

@@ -72,7 +89,7 @@ def branch(cls):
7289

7390
@classmethod
7491
def schema_url(cls):
75-
return f"https://{cls.hostname}/{cls.API_VERSION}/public/api-document"
92+
return f"https://{cls._hostname}/{cls.API_VERSION}/public/api-document"
7693

7794
@classmethod
7895
def get_auth_class(cls):

staxapp/contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def default_swagger_template() -> dict:
6464
"termsOfService": "/there_is_no_tos",
6565
"contact": {"url": "https://stax.io"},
6666
},
67-
servers=[{"url": f"https://{Config.hostname}"}],
67+
servers=[{"url": f"https://{Config._hostname}"}],
6868
paths=dict(),
6969
components={
7070
"securitySchemes": {

staxapp/openapi.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@ class StaxClient:
1515
_schema = dict()
1616
_initialized = False
1717
_config = None
18-
_requests_auth = None
1918

2019
def _get_config(self):
2120
return self._config
2221

23-
def _auth(self, **kwargs):
24-
if not self._requests_auth:
25-
self._requests_auth = self._config.get_auth_class().requests_auth
26-
return self._requests_auth(self._config, **kwargs)
27-
2822
def __init__(self, classname, force=False, config=Config.GetDefaultConfig()):
2923
# Stax feature, eg 'quotas', 'workloads'
3024
self._config = config
@@ -134,9 +128,7 @@ def stax_wrapper(*args, **kwargs):
134128
if paramter_path["method"].lower() in ["put", "post"]:
135129
# We only validate the payload for POST/PUT routes
136130
StaxContract.validate(payload, method_name)
137-
ret = getattr(Api, paramter_path["method"])(
138-
path, self._auth(), self._config, payload
139-
)
131+
ret = getattr(Api, paramter_path["method"])(path, payload, self._config)
140132
return ret
141133

142134
return stax_wrapper

test.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)