Skip to content

Commit

Permalink
Add test for invalid API key (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybchris authored Aug 24, 2022
1 parent e0887b8 commit 4a8a390
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
24 changes: 19 additions & 5 deletions hume/batch/batch_job_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ def download_errors(self, filepath: Optional[Union[str, Path]] = None) -> None:
urlretrieve(self.errors_url, filepath)

@classmethod
def from_response(cls, response: Dict[str, Any]) -> "BatchJobResult":
def from_response(cls, response: Any) -> "BatchJobResult":
"""Construct a `BatchJobResult` from a batch API job response.
Args:
response (Dict[str, Any]): Batch API job response.
response (Any): Batch API job response.
Returns:
BatchJobResult: A `BatchJobResult` based on a batch API job response.
Expand All @@ -102,6 +102,20 @@ def from_response(cls, response: Dict[str, Any]) -> "BatchJobResult":
status=BatchJobStatus.from_str(response["status"]),
**kwargs,
)
except KeyError as e:
response_str = json.dumps(response)
raise HumeClientError(f"Could not parse response into BatchJobResult: {response_str}") from e
# pylint: disable=broad-except
except Exception as exc:
message = cls._get_invalid_response_message(response)
raise HumeClientError(message) from exc

@classmethod
def _get_invalid_response_message(cls, response: Any) -> str:
response_str = json.dumps(response)
message = f"Could not parse response into BatchJobResult: {response_str}"

# Check for invalid API key
if "fault" in response and "faultstring" in response["fault"]:
fault_string = response["fault"]["faultstring"]
if fault_string == "Invalid ApiKey":
message = "Client initialized with invalid API key"

return message
4 changes: 2 additions & 2 deletions hume/common/retry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def func_wrapper(*args: P.args, **kwargs: P.kwargs) -> R:

try:
return decorated_func(*args, **kwargs)
except error_type as e:
logger.info(f"Retry iteration {attempt} failed: {str(e)}")
except error_type as exc:
logger.info(f"Retry iteration {attempt} failed: {str(exc)}")

if total_await_time >= inner_timeout:
raise HumeClientError(f"Request timed out after {inner_timeout}s")
Expand Down
14 changes: 1 addition & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ version = "0.1.0"
[tool.poetry.dependencies]
python = ">=3.8,<3.10"
requests = "^2.26.0"
backoff = "^2.1.2"
typing-extensions = "^4.3.0"

[tool.poetry.dev-dependencies]
covcheck = "^0.3.0"
flake8 = "^4.0.1"
mkdocs = "^1.3.1"
mkdocs-material = "^8.4.1"
mkdocstrings = { extras = ["python"], version = "^0.19.0" }
mypy = "^0.960"
pydocstyle = "^6.1.1"
pylint = "^2.12.1"
Expand All @@ -46,17 +48,14 @@ semver = "^2.13.0"
types-requests = "^2.25.11"
types-setuptools = "^57.4.2"
yapf = "^0.32.0"
mkdocs-material = "^8.4.1"
mkdocs = "^1.3.1"
mkdocstrings = { extras = ["python"], version = "^0.19.0" }

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0"]

[tool.covcheck]
branch = 68.0
line = 77.0
branch = 67.0
line = 75.0

[tool.flake8]
ignore = "" # Required to disable default ignores
Expand All @@ -69,7 +68,7 @@ disallow_untyped_defs = true
ignore_missing_imports = true

[tool.pylint.basic]
good-names = ["e", "id"]
good-names = ["id"]
max-args = 12
max-locals = 25
notes = ["FIXME"]
Expand Down
15 changes: 12 additions & 3 deletions tests/batch/test_batch_client_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ def test_language(self, eval_data: EvalData, batch_client: HumeBatchClient, tmp_
with download_filepath.open() as f:
json.load(f)

def test_invalid_api_key(self, eval_data: EvalData):
client = HumeBatchClient("invalid-api-key")
def test_client_invalid_api_key(self, eval_data: EvalData):
invalid_client = HumeBatchClient("invalid-api-key")
data_url = eval_data["image-obama-face"]
message = "Could not start batch job: Invalid ApiKey"
with pytest.raises(HumeClientError, match=message):
client.submit_face([data_url])
invalid_client.submit_face([data_url])

def test_job_invalid_api_key(self, eval_data: EvalData, batch_client: HumeBatchClient):
data_url = eval_data["image-obama-face"]
job = batch_client.submit_face([data_url])
invalid_client = HumeBatchClient("invalid-api-key")
message = "Client initialized with invalid API key"
with pytest.raises(HumeClientError, match=message):
rehydrated_job = BatchJob(invalid_client, job.id)
rehydrated_job.await_complete(10)

0 comments on commit 4a8a390

Please sign in to comment.