Skip to content

Commit

Permalink
Refactor to skip Configuration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Jan 24, 2024
1 parent 9dba095 commit 31a129a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
20 changes: 6 additions & 14 deletions neon_utils/hana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,22 @@ def _refresh_token(backend_address: str):


def request_backend(endpoint: str, request_data: dict,
server_config: Optional[dict] = None) -> dict:
server_url: str = _DEFAULT_BACKEND_URL) -> dict:
"""
Make a request to a Hana backend server and return the json response
@param endpoint: server endpoint to query
@param request_data: dict data to send in request body
@param server_config: Configuration['server']. If None, this will be read
from configuration
@param server_url: Base URL of Hana server to query
@returns: dict response
"""
if not server_config:
from ovos_config.config import Configuration
server_config = Configuration().get("server", {})
if server_config.get("backend_type") == "hana":
backend_address = server_config.get("url")
else:
backend_address = _DEFAULT_BACKEND_URL
_init_client(backend_address)
_init_client(server_url)
if time() >= _client_config.get("expiration", 0):
try:
_refresh_token(backend_address)
_refresh_token(server_url)
except ServerException as e:
LOG.error(e)
_get_token(backend_address)
resp = requests.post(f"{backend_address}/{endpoint.lstrip('/')}",
_get_token(server_url)
resp = requests.post(f"{server_url}/{endpoint.lstrip('/')}",
json=request_data, headers=_headers)
if resp.ok:
return resp.json()
Expand Down
5 changes: 1 addition & 4 deletions tests/hana_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ def test_request_backend(self):
import neon_utils.hana_utils
neon_utils.hana_utils._client_config = valid_config
neon_utils.hana_utils._headers = valid_headers

server_config = {"backend_type": "hana",
"url": self.test_server}
from neon_utils.hana_utils import request_backend
resp = request_backend("/neon/get_response",
{"lang_code": "en-us",
"utterance": "who are you",
"user_profile": {}}, server_config)
"user_profile": {}}, self.test_server)
self.assertEqual(resp['lang_code'], "en-us")
self.assertIsInstance(resp['answer'], str)
# TODO: Test invalid route, invalid request data
Expand Down

0 comments on commit 31a129a

Please sign in to comment.