Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not crash on '404 not found' response #7

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions actiapi/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See https://github.com/actigraph/CentrePoint3APIDocumentation.
"""
import logging
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union, Literal

import requests

Expand Down Expand Up @@ -53,6 +53,7 @@ def get_files(
study_id: int,
start: Optional[str] = None,
end: Optional[str] = None,
data_format: Literal["avro", "csv"] = "avro",
) -> List[str]:
"""Return download URLs to raw AVRO files.

Expand All @@ -66,14 +67,18 @@ def get_files(
Start timestamp string in ISO8601 format
end:
End timestamp string in ISO8601 format
data_format:
Raw data file format; avro (default) or csv.
"""
assert data_format in ("avro", "csv")

token = self._get_access_token(
"DataAccess",
)

request_string = (
f"/dataaccess/v3/files/studies/{study_id}/subjects/{user}"
f"/raw-accelerometer?fileFormat=avro"
f"/raw-accelerometer?fileFormat={data_format}"
)
if start is not None:
request_string += f"&startDate={start}"
Expand Down Expand Up @@ -176,7 +181,9 @@ def _get_paginated(self, request: str, token: str):
self.BASE_URL + paginated_request,
headers=headers,
)
reply = response.json()
reply = validate_response(response)
if reply is None:
break
total_count = reply["totalCount"]

for item in reply["items"]:
Expand All @@ -188,3 +195,12 @@ def _get_paginated(self, request: str, token: str):
logging.error("No raw data found.")
return []
return results


def validate_response(response):
if response.status_code == 404:
logging.warning("404 Not Found!")
result = None
else:
result = response.json()
return result
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ def v3_study_id():
def v3_client(v3_access_key, v3_secret_key):
client = ActiGraphClientV3(v3_access_key, v3_secret_key)
return client


@pytest.fixture(scope="session")
def response_404():
"""Simulate an Http response with status code 404."""

class Response:
status_code = 404

return Response()
8 changes: 8 additions & 0 deletions tests/test_v3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from actiapi.v3 import validate_response


def test_metadata(v3_client, v3_study_id):
metadata = v3_client.get_study_metadata(v3_study_id)
assert len(metadata) == 2


def test_validate_empty_response(response_404):
result = validate_response(response_404)
assert result is None


def test_minutes(v3_client, v3_study_id):
metadata = v3_client.get_study_metadata(v3_study_id)
id_ = metadata[0]["id"]
Expand Down