-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from dishantsethi/remaining-calls
add remaining calls function
- Loading branch information
Showing
6 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters