diff --git a/actiapi/v3.py b/actiapi/v3.py index daab8d4..f653eb8 100644 --- a/actiapi/v3.py +++ b/actiapi/v3.py @@ -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 @@ -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. @@ -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}" @@ -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"]: @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index b2d3348..5eeb31f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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() diff --git a/tests/test_v3.py b/tests/test_v3.py index 66e7745..c43a10d 100644 --- a/tests/test_v3.py +++ b/tests/test_v3.py @@ -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"]