Skip to content

Commit 591c0fb

Browse files
JWittmeyerJWittmeyer
andauthored
Fast Api JSONResponst change (#118)
* Changes response unpacking to single value * Adds config change --------- Co-authored-by: JWittmeyer <[email protected]>
1 parent d27d712 commit 591c0fb

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

controller/data_slice/neural_search_connector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ def request_outlier_detection(project_id: str, embedding_id: str, limit: int):
1111
"embedding_id": embedding_id,
1212
"limit": limit,
1313
}
14-
result = service_requests.get_call_or_raise(url, params)
15-
return result
14+
return service_requests.get_call_or_raise(url, params)

controller/misc/config_service.py

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

88
__config = None
99

10-
# these are ment to be constant values since os variables will sooner or later be removed for adresses (and used with values from config-service)
10+
# these are ment to be constant values since os variables will sooner or later be removed for addresses (and used with values from config-service)
1111
REQUEST_URL = "http://refinery-config:80/full_config"
1212
CHANGE_URL = "http://refinery-config:80/change_config"
1313

@@ -22,22 +22,21 @@ def __get_config() -> Dict[str, Any]:
2222

2323
def refresh_config():
2424
response = requests.get(REQUEST_URL)
25-
if response.status_code == 200:
26-
global __config
27-
__config = json.loads(json.loads(response.text))
28-
daemon.run(invalidate_after, 3600) # one hour as failsave
29-
else:
30-
raise Exception(
25+
if response.status_code != 200:
26+
raise ValueError(
3127
f"Config service cant be reached -- response.code{response.status_code}"
3228
)
29+
global __config
30+
__config = response.json()
31+
daemon.run(invalidate_after, 3600) # one hour as failsave
3332

3433

3534
def get_config_value(
3635
key: str, subkey: Optional[str] = None
3736
) -> Union[str, Dict[str, str]]:
3837
config = __get_config()
3938
if key not in config:
40-
raise Exception(f"Key {key} coudn't be found in config")
39+
raise ValueError(f"Key {key} coudn't be found in config")
4140
value = config[key]
4241

4342
if not subkey:
@@ -46,7 +45,7 @@ def get_config_value(
4645
if isinstance(value, dict) and subkey in value:
4746
return value[subkey]
4847
else:
49-
raise Exception(f"Subkey {subkey} coudn't be found in config[{key}]")
48+
raise ValueError(f"Subkey {subkey} coudn't be found in config[{key}]")
5049

5150

5251
def invalidate_after(sec: int) -> None:

controller/misc/manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_black_white_demo() -> Dict[str, List[str]]:
2929
def get_version_overview() -> List[ServiceVersionResult]:
3030
updater_version_overview = __updater_version_overview()
3131
date_format = "%Y-%m-%dT%H:%M:%S.%f" # 2022-09-06T12:10:39.167397
32-
version_overview = [
32+
return [
3333
ServiceVersionResult(
3434
service=service["name"],
3535
installed_version=service["installed_version"],
@@ -40,12 +40,10 @@ def get_version_overview() -> List[ServiceVersionResult]:
4040
)
4141
for service in updater_version_overview
4242
]
43-
return version_overview
4443

4544

4645
def has_updates() -> List[ServiceVersionResult]:
47-
has_updates = __updater_has_updates()
48-
return has_updates
46+
return __updater_has_updates()
4947

5048

5149
def __updater_version_overview() -> List[Dict[str, Any]]:

util/service_requests.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@
66

77
def post_call_or_raise(url: str, data: Dict[str, Any]) -> Any:
88
response = requests.post(url, json=data)
9-
if response.status_code == 200:
10-
result, _ = response.json()
11-
return result
12-
else:
9+
10+
if response.status_code != 200:
1311
raise GraphQLError(response.text)
1412

13+
if response.headers.get("content-type") == "application/json":
14+
return response.json()
15+
else:
16+
return response.text
17+
1518

1619
def get_call_or_raise(url: str, params: Dict = None) -> Any:
1720
if params is None:
1821
params = {}
1922
response = requests.get(url=url, params=params)
20-
if response.status_code == 200:
21-
result, _ = response.json()
22-
return result
23-
else:
23+
if response.status_code != 200:
2424
raise GraphQLError(response.text)
2525

26+
if response.headers.get("content-type") == "application/json":
27+
return response.json()
28+
else:
29+
return response.text
30+
2631

2732
def delete_call_or_raise(url: str, params: Dict = None) -> int:
2833
if params is None:

0 commit comments

Comments
 (0)