Skip to content

Commit a8826ec

Browse files
committed
feat(bulk-downloads): implement bulk report query API endpoints and models
This: - add `BulkReportQueryEndPoint`, an endpoint class for handling specific bulk report query requests - add `BulkReportQueryParams` for serializing and validating request parameters - add `BulkReportQueryItem` and `BulkReportQueryResult` for deserializing and validating API responses - add `BulkFixedInfrastructureDataQueryEndPoint` for handling fixed infrastructure bulk report query requests - add `fixtures` and `unit tests` for `BulkReportQueryParams`, and `BulkFixedInfrastructureDataQueryEndPoint`
1 parent b805062 commit a8826ec

File tree

20 files changed

+912
-0
lines changed

20 files changed

+912
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report.
2+
3+
This module provides the endpoint and associated functionalities for querying
4+
the previously created bulk report structured data in JSON format.
5+
It defines the `BulkReportQueryEndPoint` class, which handles the construction
6+
and execution of API requests and the parsing of API responses for
7+
Query Bulk Report API endpoint.
8+
9+
For detailed information about the Query Bulk Report API endpoint, please refer to
10+
the official Global Fishing Watch API documentation:
11+
12+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
13+
14+
For more details on the Query Bulk Report data caveats, please refer to the
15+
official Global Fishing Watch API documentation:
16+
17+
See: https://globalfishingwatch.org/our-apis/documentation#sar-fixed-infrastructure-data-caveats
18+
"""
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report API endpoints."""
2+
3+
from typing import Any, Dict, List, Type, Union
4+
5+
from typing_extensions import override
6+
7+
from gfwapiclient.exceptions.validation import ResultValidationError
8+
from gfwapiclient.http.client import HTTPClient
9+
from gfwapiclient.http.endpoints import GetEndPoint
10+
from gfwapiclient.http.models import RequestBody
11+
from gfwapiclient.resources.bulk_downloads.query.models.base.request import (
12+
BulkReportQueryParams,
13+
)
14+
from gfwapiclient.resources.bulk_downloads.query.models.base.response import (
15+
_BulkReportQueryItemT,
16+
_BulkReportQueryResultT,
17+
)
18+
from gfwapiclient.resources.bulk_downloads.query.models.fixed_infrastructure_data.response import (
19+
BulkFixedInfrastructureDataQueryItem,
20+
BulkFixedInfrastructureDataQueryResult,
21+
)
22+
23+
24+
__all__ = ["BulkFixedInfrastructureDataQueryEndPoint", "BulkReportQueryEndPoint"]
25+
26+
27+
class BulkReportQueryEndPoint(
28+
GetEndPoint[
29+
BulkReportQueryParams,
30+
RequestBody,
31+
_BulkReportQueryItemT,
32+
_BulkReportQueryResultT,
33+
],
34+
):
35+
"""Query Bulk Report API endpoint.
36+
37+
This endpoint query the previously created bulk report data in JSON format
38+
based on the provided request parameters.
39+
40+
For more details on the Query Bulk Report API endpoint, please refer to the
41+
official Global Fishing Watch API documentation:
42+
43+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
44+
"""
45+
46+
def __init__(
47+
self,
48+
*,
49+
bulk_report_id: str,
50+
request_params: BulkReportQueryParams,
51+
result_item_class: Type[_BulkReportQueryItemT],
52+
result_class: Type[_BulkReportQueryResultT],
53+
http_client: HTTPClient,
54+
) -> None:
55+
"""Initializes a new `BulkReportQueryEndPoint`.
56+
57+
Args:
58+
bulk_report_id (str):
59+
Unique identifier (ID) of the bulk report.
60+
61+
request_params (BulkReportQueryParams):
62+
The request parameters.
63+
64+
result_item_class (Type[_BulkReportQueryItemT]):
65+
Pydantic model for the expected response item.
66+
67+
result_class (Type[_BulkReportQueryResultT]):
68+
Pydantic model for the expected response result.
69+
70+
http_client (HTTPClient):
71+
The HTTP client used to make the API call.
72+
"""
73+
super().__init__(
74+
path=f"bulk-reports/{bulk_report_id}/query",
75+
request_params=request_params,
76+
result_item_class=result_item_class,
77+
result_class=result_class,
78+
http_client=http_client,
79+
)
80+
81+
@override
82+
def _transform_response_data(
83+
self,
84+
*,
85+
body: Union[List[Dict[str, Any]], Dict[str, Any]],
86+
) -> Union[List[Dict[str, Any]], Dict[str, Any]]:
87+
"""Transform and reshape response body and yield data.
88+
89+
This method transforms the raw response body from the API into a format
90+
suitable for the `BulkReportQueryResult` model.
91+
92+
The expected response structure is: `{"entries": [{...}]}`.
93+
94+
Args:
95+
body (Union[List[Dict[str, Any]], Dict[str, Any]]):
96+
The raw response body.
97+
98+
Returns:
99+
Union[List[Dict[str, Any]], Dict[str, Any]]:
100+
The transformed response data.
101+
102+
Raises:
103+
ResultValidationError:
104+
If the response body does not match the expected format.
105+
"""
106+
# expected: {"entries": [{"key": ...}, ...], ...}
107+
if not isinstance(body, dict) or "entries" not in body:
108+
raise ResultValidationError(
109+
message="Expected a list of entries, but got an empty list.",
110+
body=body,
111+
)
112+
113+
# Transforming and reshaping entries
114+
bulk_report_data_entries: List[Dict[str, Any]] = body.get("entries", [])
115+
transformed_data: List[Dict[str, Any]] = []
116+
117+
# Loop through "entries" list i.e [{"key": ..., ...}, ...]
118+
for bulk_report_data_entry in bulk_report_data_entries:
119+
# Append extracted dictionaries, if not empty
120+
if bulk_report_data_entry:
121+
transformed_data.append(dict(**bulk_report_data_entry))
122+
123+
return transformed_data
124+
125+
126+
class BulkFixedInfrastructureDataQueryEndPoint(
127+
BulkReportQueryEndPoint[
128+
BulkFixedInfrastructureDataQueryItem,
129+
BulkFixedInfrastructureDataQueryResult,
130+
],
131+
):
132+
"""Query Bulk fixed infrastructure data API endpoint.
133+
134+
This endpoint query the previously created fixed infrastructure data (i.e.,
135+
`public-fixed-infrastructure-data:latest` dataset) bulk report data in JSON format
136+
based on the provided request parameters.
137+
138+
For more details on the Query Bulk Report API endpoint, please refer to the
139+
official Global Fishing Watch API documentation:
140+
141+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
142+
"""
143+
144+
def __init__(
145+
self,
146+
*,
147+
bulk_report_id: str,
148+
request_params: BulkReportQueryParams,
149+
http_client: HTTPClient,
150+
) -> None:
151+
"""Initializes a new `BulkFixedInfrastructureDataQueryEndPoint`.
152+
153+
Args:
154+
bulk_report_id (str):
155+
Unique identifier (ID) of the bulk report.
156+
157+
request_params (BulkReportQueryParams):
158+
The request parameters.
159+
160+
http_client (HTTPClient):
161+
The HTTP client used to make the API call.
162+
"""
163+
super().__init__(
164+
bulk_report_id=bulk_report_id,
165+
request_params=request_params,
166+
result_item_class=BulkFixedInfrastructureDataQueryItem,
167+
result_class=BulkFixedInfrastructureDataQueryResult,
168+
http_client=http_client,
169+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report Data Models.
2+
3+
This module defines Pydantic data models used for interacting with the
4+
Query Bulk Report Data API endpoint. These models are used to represent
5+
request parameters, and response data when querying data in JSON format of the
6+
previously created bulk report.
7+
8+
For detailed information about the Query Bulk Report Data API endpoint, please refer to
9+
the official Global Fishing Watch API documentation:
10+
11+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
12+
13+
For more details on the Query Bulk Report Data data caveats, please refer to the
14+
official Global Fishing Watch API documentation:
15+
16+
See: https://globalfishingwatch.org/our-apis/documentation#sar-fixed-infrastructure-data-caveats
17+
"""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report Base Models.
2+
3+
This module defines base Pydantic models used across the Query Bulk Report API
4+
endpoint. These models provide common structures for request parameters, and response
5+
data when querying data in JSON format of the previously created bulk report.
6+
7+
For detailed information about the Query Bulk Report API endpoint, please refer to
8+
the official Global Fishing Watch API documentation:
9+
10+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
11+
12+
For more details on the Query Bulk Report data caveats, please refer to the
13+
official Global Fishing Watch API documentation:
14+
15+
See: https://globalfishingwatch.org/our-apis/documentation#sar-fixed-infrastructure-data-caveats
16+
"""
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report Base Request Models."""
2+
3+
from typing import ClassVar, Final, List, Optional
4+
5+
from pydantic import Field
6+
7+
from gfwapiclient.http.models import RequestParams
8+
9+
10+
__all__ = ["BulkReportQueryParams"]
11+
12+
13+
BULK_REPORT_QUERY_PARAMS_VALIDATION_ERROR_MESSAGE: Final[str] = (
14+
"Query bulk report request parameters validation failed."
15+
)
16+
17+
18+
class BulkReportQueryParams(RequestParams):
19+
"""Request query parameters for Query Bulk Report API endpoint.
20+
21+
Represents pagination, sorting, filtering parameters etc. for querying previously
22+
created bulk report data.
23+
24+
For more details on the Query Bulk Report API endpoint supported request parameters,
25+
please refer to the official Global Fishing Watch API documentation:
26+
27+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
28+
29+
Attributes:
30+
limit (Optional[int]):
31+
Maximum number of bulk report records to return.
32+
Defaults to `99999`.
33+
34+
offset (Optional[int]):
35+
Number of bulk report records to skip before returning results.
36+
Used for pagination. Defaults to `0`.
37+
38+
sort (Optional[str]):
39+
Property to sort the bulk report records by (e.g.
40+
`"-structure_start_date"`).
41+
42+
includes (Optional[List[str]]):
43+
List of bulk report record fields to include in the result.
44+
"""
45+
46+
indexed_fields: ClassVar[Optional[List[str]]] = ["includes"]
47+
48+
limit: Optional[int] = Field(99999, ge=0, alias="limit")
49+
offset: Optional[int] = Field(0, ge=0, alias="offset")
50+
sort: Optional[str] = Field(None, alias="sort")
51+
includes: Optional[List[str]] = Field(None, alias="includes")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Report Base Response Models."""
2+
3+
from typing import Any, List, Type, TypeVar
4+
5+
from gfwapiclient.http.models import Result, ResultItem
6+
7+
8+
__all__ = [
9+
"BulkReportQueryItem",
10+
"BulkReportQueryResult",
11+
"_BulkReportQueryItemT",
12+
"_BulkReportQueryResultT",
13+
]
14+
15+
16+
class BulkReportQueryItem(ResultItem):
17+
"""Result item for the Query Bulk Report API endpoint.
18+
19+
Represents a data record of a previously created bulk report.
20+
21+
For more details on the Query Bulk Report API endpoint supported response bodies,
22+
please refer to the official Global Fishing Watch API documentation:
23+
24+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
25+
"""
26+
27+
pass
28+
29+
30+
_BulkReportQueryItemT = TypeVar("_BulkReportQueryItemT", bound=BulkReportQueryItem)
31+
32+
33+
class BulkReportQueryResult(Result[_BulkReportQueryItemT]):
34+
"""Result for the Query Bulk Report API endpoint.
35+
36+
Represents data records of a previously created bulk report.
37+
38+
For more details on the Query Bulk Report API endpoint supported response bodies,
39+
please refer to the official Global Fishing Watch API documentation:
40+
41+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
42+
43+
Attributes:
44+
_result_item_class (Type[_BulkReportQueryItemT]):
45+
The model used for individual result items.
46+
47+
_data (List[_BulkReportQueryItemT]):
48+
The bulk report data item returned in the response.
49+
"""
50+
51+
_result_item_class: Type[_BulkReportQueryItemT]
52+
_data: List[_BulkReportQueryItemT]
53+
54+
def __init__(self, data: List[_BulkReportQueryItemT]) -> None:
55+
"""Initializes a new `BulkReportQueryResult`.
56+
57+
Args:
58+
data (List[_BulkReportQueryItemT]):
59+
The list of bulk report data items.
60+
"""
61+
super().__init__(data=data)
62+
63+
64+
_BulkReportQueryResultT = TypeVar(
65+
"_BulkReportQueryResultT", bound=BulkReportQueryResult[Any]
66+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Global Fishing Watch (GFW) API Python Client - Query Bulk Fixed Infrastructure Data Models.
2+
3+
This module defines Pydantic data models used for interacting with the Query Bulk
4+
Report API endpoint. These models are used to represent request parameters, and
5+
response data when querying data in JSON format of the previously created
6+
fixed infrastructure data (i.e `public-fixed-infrastructure-data:latest` dataset) bulk report.
7+
8+
For detailed information about the Query Bulk Report - API endpoint, please refer to
9+
the official Global Fishing Watch API documentation:
10+
11+
See: https://globalfishingwatch.org/our-apis/documentation#get-data-in-json-format
12+
13+
For more details on the Query Bulk Report - data caveats, please refer to the
14+
official Global Fishing Watch API documentation:
15+
16+
See: https://globalfishingwatch.org/our-apis/documentation#sar-fixed-infrastructure-data-caveats
17+
"""

0 commit comments

Comments
 (0)