Coverage for acspsuedo / source / low / var_fetch.py: 100%
27 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-11 16:02 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-11 16:02 +0000
1"""
2Fetch handler for variable/table metadata.
3"""
5import typing as t
7from acspsuedo.source.low.protocols import fetch_content
11class VariableFetchMixin:
12 """
13 Mix-in to handle fetching metadata on variables and tables
14 in ACS datasets.
15 """
16 @staticmethod
17 def _fetch_json_content(dataset: str, year: int, variable: t.Optional[str] = None):
18 """
19 Fetch the JSON content for a dataset variable.
20 """
21 url = VariableFetchMixin.dataset_variables_url(dataset, year, variable)
22 json_content = fetch_content(url)
24 return json_content
26 @staticmethod
27 def _fetch_table_json_content(dataset: str, year: int, table: t.Optional[str] = None):
28 """
29 Fetch the JSON content for a dataset table.
30 """
31 url = VariableFetchMixin.dataset_tables_url(dataset, year, table)
32 json_content = fetch_content(url)
34 return json_content
37 @staticmethod
38 def dataset_variables_url(dataset: str, year: int, variable: t.Optional[str] = None):
39 """
40 URL constructor to fetch a variable's metadata.
41 """
42 variable = f'/{variable}' if variable else ''
43 url = f'{VariableFetchMixin._base_url_comp}/{VariableFetchMixin._dataset_year_url_comp(dataset, year)}/variables{variable}.json'
45 return url
47 @staticmethod
48 def dataset_tables_url(dataset: str, year: int, table: t.Optional[str] = None):
49 """
50 URL constructor to fetch a table's metadata.
51 """
52 table = f'/{table}' if table else ''
53 url = f'{VariableFetchMixin._base_url_comp}/{VariableFetchMixin._dataset_year_url_comp(dataset, year)}/groups{table}.json'
55 return url
57 _base_url_comp = 'https://api.census.gov/data'
59 @classmethod
60 def _dataset_year_url_comp(cls, dataset: str, year) -> str:
61 return f'{year}/{dataset}'