From d4bc394d8fa88104de8448332434e03a92b977f2 Mon Sep 17 00:00:00 2001 From: dishantsethi Date: Tue, 23 Jul 2024 14:05:11 +0530 Subject: [PATCH 1/2] add remaining calls function --- tests/test_diarize.py | 0 willisapi_client/services/api.py | 1 + willisapi_client/services/diarize/__init__.py | 6 +++ .../services/diarize/diarize_utils.py | 40 +++++++++++++++++++ .../services/diarize/willisdiarize.py | 29 ++++++++++++++ willisapi_client/willisapi_client.py | 6 +-- 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 tests/test_diarize.py create mode 100644 willisapi_client/services/diarize/__init__.py create mode 100644 willisapi_client/services/diarize/diarize_utils.py create mode 100644 willisapi_client/services/diarize/willisdiarize.py diff --git a/tests/test_diarize.py b/tests/test_diarize.py new file mode 100644 index 0000000..e69de29 diff --git a/willisapi_client/services/api.py b/willisapi_client/services/api.py index 556d70c..7fc44ff 100644 --- a/willisapi_client/services/api.py +++ b/willisapi_client/services/api.py @@ -4,3 +4,4 @@ from willisapi_client.services.auth import login from willisapi_client.services.upload import upload from willisapi_client.services.download import download +from willisapi_client.services.diarize import willis_diarize_call_remaining diff --git a/willisapi_client/services/diarize/__init__.py b/willisapi_client/services/diarize/__init__.py new file mode 100644 index 0000000..7b0f2fa --- /dev/null +++ b/willisapi_client/services/diarize/__init__.py @@ -0,0 +1,6 @@ +# website: https://www.brooklyn.health +from willisapi_client.services.diarize.willisdiarize import ( + willis_diarize_call_remaining, +) + +__all__ = ["willis_diarize_call_remaining"] diff --git a/willisapi_client/services/diarize/diarize_utils.py b/willisapi_client/services/diarize/diarize_utils.py new file mode 100644 index 0000000..31bc09b --- /dev/null +++ b/willisapi_client/services/diarize/diarize_utils.py @@ -0,0 +1,40 @@ +import requests +import json +import time +import random + + +class DiarizeUtils: + def request(url, headers, try_number): + """ + ------------------------------------------------------------------------------------------------------ + Class: DiarizeUtils + + Function: request + + Description: This is an internal diarize function which makes a GET API call to brooklyn.health API server + + Parameters: + ---------- + url: The URL of the API endpoint. + headers: The headers to be sent in the request. + try_number: The number of times the function has been tried. + + Returns: + ---------- + json: The JSON response from the API server. + ------------------------------------------------------------------------------------------------------ + """ + try: + response = requests.get(url, headers=headers) + res_json = response.json() + except ( + requests.exceptions.ConnectionError, + json.decoder.JSONDecodeError, + ) as ex: + if try_number == 3: + raise + time.sleep(random.random() * 2) + return DiarizeUtils.request(url, headers, try_number=try_number + 1) + else: + return res_json diff --git a/willisapi_client/services/diarize/willisdiarize.py b/willisapi_client/services/diarize/willisdiarize.py new file mode 100644 index 0000000..2c6b73c --- /dev/null +++ b/willisapi_client/services/diarize/willisdiarize.py @@ -0,0 +1,29 @@ +from http import HTTPStatus + +from willisapi_client.willisapi_client import WillisapiClient +from willisapi_client.logging_setup import logger as logger +from willisapi_client.services.diarize.diarize_utils import DiarizeUtils + + +def willis_diarize_call_remaining(key: str, **kwargs): + """ + --------------------------------------------------------------------------------------------------- + Function: willis_diarize_call_remaining + + Description: This function returns the number of remaining calls for willisdiarize + + Parameters: + ---------- + key: AWS access id token (str) + + --------------------------------------------------------------------------------------------------- + """ + + wc = WillisapiClient(env=kwargs.get("env")) + url = wc.get_diarize_remaining_calls_url() + headers = wc.get_headers() + headers["Authorization"] = key + + response = DiarizeUtils.request(url, headers, try_number=1) + if response: + logger.info(response["message"]) diff --git a/willisapi_client/willisapi_client.py b/willisapi_client/willisapi_client.py index fbcd119..1cf9ac4 100644 --- a/willisapi_client/willisapi_client.py +++ b/willisapi_client/willisapi_client.py @@ -25,14 +25,14 @@ def get_base_url(self): def get_login_url(self): return self.get_base_url() + "login" - def get_signup_url(self): - return self.get_base_url() + "signup" - def get_upload_url(self): return self.get_base_url() + "upload" def get_download_url(self): return self.get_base_url() + "download" + def get_diarize_remaining_calls_url(self): + return self.get_base_url() + "willis-diarize-call-remaining" + def get_headers(self): return {"Content-Type": "application/json", "Accept": "application/json"} From 5a7d45eafb06be96e18868b794b9dccecadd05de Mon Sep 17 00:00:00 2001 From: dishantsethi Date: Tue, 23 Jul 2024 14:17:44 +0530 Subject: [PATCH 2/2] add tests --- tests/test_diarize.py | 31 +++++++++++++++++++ .../services/diarize/willisdiarize.py | 5 +++ 2 files changed, 36 insertions(+) diff --git a/tests/test_diarize.py b/tests/test_diarize.py index e69de29..4987c58 100644 --- a/tests/test_diarize.py +++ b/tests/test_diarize.py @@ -0,0 +1,31 @@ +from unittest.mock import patch + +from willisapi_client.services.diarize.willisdiarize import ( + willis_diarize_call_remaining, +) + + +class TestDiarizeCallsFunction: + def setup(self): + self.key = "dummy" + + @patch("willisapi_client.services.diarize.diarize_utils.DiarizeUtils.request") + def test_willisdiarize_remaining_calls_failed(self, mocked_data): + mocked_data.return_value = {} + res = willis_diarize_call_remaining(self.key) + assert res == None + + @patch("willisapi_client.services.diarize.diarize_utils.DiarizeUtils.request") + def test_willisdiarize_remaining_calls_missing_auth(self, mocked_data): + mocked_data.return_value = {"status_code": 401, "message": "message"} + res = willis_diarize_call_remaining(self.key) + assert res == "message" + + @patch("willisapi_client.services.diarize.diarize_utils.DiarizeUtils.request") + def test_willisdiarize_remaining_calls_success(self, mocked_data): + mocked_data.return_value = { + "status_code": 401, + "message": "Your account has 10 WillisDiarize API calls remaining.", + } + res = willis_diarize_call_remaining(self.key) + assert res == "Your account has 10 WillisDiarize API calls remaining." diff --git a/willisapi_client/services/diarize/willisdiarize.py b/willisapi_client/services/diarize/willisdiarize.py index 2c6b73c..d908d53 100644 --- a/willisapi_client/services/diarize/willisdiarize.py +++ b/willisapi_client/services/diarize/willisdiarize.py @@ -16,6 +16,9 @@ def willis_diarize_call_remaining(key: str, **kwargs): ---------- key: AWS access id token (str) + Returns: + ---------- + string: String --------------------------------------------------------------------------------------------------- """ @@ -27,3 +30,5 @@ def willis_diarize_call_remaining(key: str, **kwargs): response = DiarizeUtils.request(url, headers, try_number=1) if response: logger.info(response["message"]) + return response["message"] + return None