Skip to content

Commit

Permalink
Merge pull request #99 from dishantsethi/remaining-calls
Browse files Browse the repository at this point in the history
add remaining calls function
  • Loading branch information
dishantsethi authored Jul 23, 2024
2 parents 781c791 + 5a7d45e commit 70662ee
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
31 changes: 31 additions & 0 deletions tests/test_diarize.py
Original file line number Diff line number Diff line change
@@ -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."
1 change: 1 addition & 0 deletions willisapi_client/services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions willisapi_client/services/diarize/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
40 changes: 40 additions & 0 deletions willisapi_client/services/diarize/diarize_utils.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions willisapi_client/services/diarize/willisdiarize.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions willisapi_client/willisapi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

0 comments on commit 70662ee

Please sign in to comment.