diff --git a/tests/test_diarize.py b/tests/test_diarize.py new file mode 100644 index 0000000..4987c58 --- /dev/null +++ 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/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..d908d53 --- /dev/null +++ b/willisapi_client/services/diarize/willisdiarize.py @@ -0,0 +1,34 @@ +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) + + Returns: + ---------- + string: String + --------------------------------------------------------------------------------------------------- + """ + + 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"]) + return response["message"] + return None 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"}