Skip to content

Commit

Permalink
Add extensions to aaq, flow_results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Schalk1e committed Oct 3, 2024
1 parent 78ae179 commit 751d69e
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 87 deletions.
35 changes: 0 additions & 35 deletions rdw_ingestion_tools/api/aaq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections.abc import Iterator

from httpx import Client

from .. import config_from_env
Expand All @@ -8,39 +6,6 @@
BASE_URL = config_from_env("AAQ_API_BASE_URL")


def get_paginated(
client: Client,
url: str,
**kwargs: str | int,
) -> Iterator[dict]:
"""Paginate over pages in an AAQ endpoint up to a limit."""

limit: int = 100
offset: int = 0

params = {"offset": offset, "limit": limit}

while True:
print(
"Retrieving results for offsets: ",
params["offset"],
"to",
params["offset"] + limit,
sep=" ",
)
# Need {**params, **kwargs}. mypy dislikes str|int for lines 27, 40.
response = client.get(url, params={**params, **kwargs})
response.raise_for_status()

result: list[dict] = response.json()["result"]
yield from result

if len(result) < limit:
return
else:
params["offset"] += limit


headers = {
"Authorization": f"Bearer {API_KEY}",
"Accept": "application/vnd.v1+json",
Expand Down
36 changes: 36 additions & 0 deletions rdw_ingestion_tools/api/aaq/extensions/httpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections.abc import Iterator

from httpx import Client


def get_paginated(
client: Client,
url: str,
**kwargs: str | int,
) -> Iterator[dict]:
"""Paginate over pages in an AAQ endpoint up to a limit."""

limit: int = 100
offset: int = 0

params = {"offset": offset, "limit": limit}

while True:
print(
"Retrieving results for offsets: ",
params["offset"],
"to",
params["offset"] + limit,
sep=" ",
)
# Need {**params, **kwargs}. mypy dislikes str|int for lines 27, 40.
response = client.get(url, params={**params, **kwargs})
response.raise_for_status()

result: list[dict] = response.json()["result"]
yield from result

if len(result) < limit:
return
else:
params["offset"] += limit
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/aaq/requests/faqmatches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from httpx import Client
from pandas import DataFrame

from .. import get_paginated
from ..extensions.httpx import get_paginated


@define
Expand Down
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/aaq/requests/inbounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpx import Client
from pandas import DataFrame

from .. import get_paginated
from ..extensions.httpx import get_paginated


class FAQModel(TypedDict):
Expand Down
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/aaq/requests/inbounds_ud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from httpx import Client
from pandas import DataFrame

from .. import get_paginated
from ..extensions.httpx import get_paginated


@define
Expand Down
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/aaq/requests/urgency_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from httpx import Client
from pandas import DataFrame

from .. import get_paginated
from ..extensions.httpx import get_paginated


@define
Expand Down
46 changes: 0 additions & 46 deletions rdw_ingestion_tools/api/flow_results/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections.abc import Iterator

from httpx import Client

from .. import config_from_env
Expand All @@ -8,50 +6,6 @@
BASE_URL = config_from_env("FLOW_RESULTS_API_BASE_URL")


def get_ids(client: Client, **kwargs: str | int) -> Iterator[str]:
"""Returns a list of flow id's.
These id's are required in order to get responses from the Flow
Results API.
"""

params = {**kwargs}
url = ""

response = client.get(url, params=params)
response.raise_for_status()

for flow in response.json()["data"]:
yield flow["id"]


def get_paginated(
client: Client, url: str, **kwargs: str | int
) -> Iterator[list]:
"""Paginate over the Flow Results Responses Endpoint.
Each response returns a next link which is followed until
the full result set is returned.
"""

while True:
response = client.get(url, params={**kwargs})
response.raise_for_status()

data: dict = response.json()["data"]

results: list = data["attributes"]["responses"]
yield from results

try:
full_url = data["relationships"]["links"]["next"]
url = full_url.split("packages/")[-1]
except AttributeError:
break


headers = {"Authorization": f"Token {API_KEY}"}

client: Client = Client(base_url=BASE_URL, headers=headers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Directory ‘/home/schalk/reach/rdw-ingestion-tools/rdw_ingestion_tools/api/flow_results/extensions/’ does not exist; create? (y or n)
47 changes: 47 additions & 0 deletions rdw_ingestion_tools/api/flow_results/extensions/httpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from collections.abc import Iterator

from httpx import Client


def get_ids(client: Client, **kwargs: str | int) -> Iterator[str]:
"""Returns a list of flow id's.
These id's are required in order to get responses from the Flow
Results API.
"""

params = {**kwargs}
url = ""

response = client.get(url, params=params)
response.raise_for_status()

for flow in response.json()["data"]:
yield flow["id"]


def get_paginated(
client: Client, url: str, **kwargs: str | int
) -> Iterator[list]:
"""Paginate over the Flow Results Responses Endpoint.
Each response returns a next link which is followed until
the full result set is returned.
"""

while True:
response = client.get(url, params={**kwargs})
response.raise_for_status()

data: dict = response.json()["data"]

results: list = data["attributes"]["responses"]
yield from results

try:
full_url = data["relationships"]["links"]["next"]
url = full_url.split("packages/")[-1]
except AttributeError:
break
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/flow_results/requests/flows.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from attrs import define
from httpx import Client

from .. import get_ids
from ..extensions.httpx import get_ids


@define
Expand Down
2 changes: 1 addition & 1 deletion rdw_ingestion_tools/api/flow_results/requests/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from attrs import define
from httpx import Client

from .. import get_ids, get_paginated
from ..extensions.httpx import get_ids, get_paginated


@define
Expand Down

0 comments on commit 751d69e

Please sign in to comment.