Skip to content

Commit

Permalink
🖤
Browse files Browse the repository at this point in the history
  • Loading branch information
renae-r committed Jan 10, 2025
1 parent e80340d commit 87dffdb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/ipumspy/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def request(self, method: str, *args, **kwargs) -> requests.Response:
"Page not found. Perhaps you passed the wrong extract id or an invalid page size?"
)
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
raise IpumsApiRateLimitException("You have exceeded the API rate limit.")
raise IpumsApiRateLimitException(
"You have exceeded the API rate limit."
)
else:
error_details = _prettify_message(response.json()["detail"])
raise IpumsApiException(error_details)
Expand Down
3 changes: 2 additions & 1 deletion src/ipumspy/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ class BadIpumsApiRequest(IpumsApiException):
class IpumsExtractNotSubmitted(IpumsApiException):
"""Represents the case when an extract needs to be submitted before the operation can be performed"""


class IpumsApiRateLimitException(IpumsApiException):
"""Represents a request that exceeds the IPUMS API rate limit"""
"""Represents a request that exceeds the IPUMS API rate limit"""
25 changes: 12 additions & 13 deletions src/ipumspy/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class IpumsMetadata(ABC):
Class to request and store metadata for an arbitrary IPUMS resource. Use a subclass to request
metadata for a particular type of resource.
"""

def populate(self, metadata_response_dict: dict):
"""
Update IpumsMetadata objects with attributes from API response.
Expand All @@ -26,20 +26,20 @@ def populate(self, metadata_response_dict: dict):
setattr(self, attribute, metadata_response_dict[attribute])
else:
raise KeyError(f"{type(self).__name__} has no attribute '{attribute}'.")

@property
@abstractmethod
def supported_collections(self):
"""
Collections that support this metadata class
"""
pass

def _validate_collection(self):
if self.collection not in self.supported_collections:
raise ValueError(f"{type(self).__name__} is not a valid metadata type for the {self.collection} collection.")


raise ValueError(
f"{type(self).__name__} is not a valid metadata type for the {self.collection} collection."
)


@dataclass
Expand Down Expand Up @@ -93,13 +93,12 @@ class DatasetMetadata(IpumsMetadata):
def __post_init__(self):
self._path = f"metadata/datasets/{self.name}"
self._validate_collection()

@property
def supported_collections(self):
return ["nhgis"]



@dataclass
class TimeSeriesTableMetadata(IpumsMetadata):
"""
Expand Down Expand Up @@ -134,12 +133,10 @@ class TimeSeriesTableMetadata(IpumsMetadata):
def __post_init__(self):
self._path = f"metadata/time_series_tables/{self.name}"
self._validate_collection()

@property
def supported_collections(self):
return ["nhgis"]




@dataclass
Expand Down Expand Up @@ -174,9 +171,11 @@ class DataTableMetadata(IpumsMetadata):
"""Dictionary containing variable descriptions and codes for the variables included in the data table"""

def __post_init__(self):
self._path = self._path = f"metadata/datasets/{self.dataset_name}/data_tables/{self.name}"
self._path = self._path = (
f"metadata/datasets/{self.dataset_name}/data_tables/{self.name}"
)
self._validate_collection()

@property
def supported_collections(self):
return ["nhgis"]
12 changes: 9 additions & 3 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ipumspy.api.exceptions import IpumsApiRateLimitException


@pytest.fixture(scope="function")
def live_api_client(environment_variables) -> IpumsApiClient:
live_client = IpumsApiClient(os.environ.get("IPUMS_API_KEY"))
Expand Down Expand Up @@ -63,19 +64,24 @@ def test_get_metadata(live_api_client: IpumsApiClient):

assert len(ds.data_tables) == 100
assert len(dt.variables) == 49


def test_collection_validity():
with pytest.raises(ValueError) as exc_info:
ds = DatasetMetadata("usa", "1990_STF1")
assert exc_info.value.args[0] == "DatasetMetadata is not a valid metadata type for the usa collection."
assert (
exc_info.value.args[0]
== "DatasetMetadata is not a valid metadata type for the usa collection."
)


@pytest.mark.vcr
@pytest.mark.slow
def test_ipums_api_rate_limit_exception(live_api_client: IpumsApiClient):
with pytest.raises(IpumsApiRateLimitException) as exc_info:
for page in live_api_client.get_metadata_catalog("nhgis", metadata_type="data_tables", page_size=5):
for page in live_api_client.get_metadata_catalog(
"nhgis", metadata_type="data_tables", page_size=5
):
for dt in page["data"]:
continue
assert exc_info.value.args[0] == "You have exceeded the API rate limit."

0 comments on commit 87dffdb

Please sign in to comment.