Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion osm_data_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,60 @@ async def request_snapshot(
log.error("Unexpected error in API request: %s", str(ex))
raise APIRequestError(0, {}, str(ex)) from ex

async def request_plain_geojson_snapshot(
self, geometry: GeometryInput, params: RequestParams
) -> dict[str, Any]:
"""
Request a snapshot of OSM geojson data.

Args:
geometry: Validated GeoJSON geometry object
params: Validated request parameters

Returns:
Plain geojson of osm features

Raises:
APIRequestError: If the API request fails
"""
payload = {
**params.to_api_params(),
"geometry": geometry.to_dict(),
}

log.debug("Requesting snapshot with params: %s", json.dumps(payload))

async with ClientSession() as session:
try:
async with session.post(
f"{self.config.base_api_url}/snapshot/plain/",
json=payload,
headers=self.headers,
) as response:
response_data = await response.json()
if response.status >= 400:
log.error(
"API request failed with status %d: %s",
response.status,
response_data,
)
raise APIRequestError(response.status, response_data)

# Log queue information if available
if "queue" in response_data:
queue_position = response_data.get("queue", 0)
if queue_position > 0:
log.info("Request queued at position %d", queue_position)

log.debug("Snapshot request successful: %s", response_data)
return response_data
except ClientResponseError as ex:
log.error("API client error: %s", str(ex))
raise APIRequestError(ex.status, {}, str(ex)) from ex
except Exception as ex:
log.error("Unexpected error in API request: %s", str(ex))
raise APIRequestError(0, {}, str(ex)) from ex

async def poll_task_status(
self, task_link: str, polling_interval: int = 2
) -> dict[str, Any]:
Expand Down Expand Up @@ -250,7 +304,7 @@ async def get_osm_data(
geometry: dict[str, Any] | str,
output_options: RawDataOutputOptions = RawDataOutputOptions.default(),
**kwargs,
) -> RawDataResult:
) -> RawDataResult | dict:
"""
Get OSM data for a specified area.

Expand Down Expand Up @@ -285,6 +339,14 @@ async def get_osm_data(
geometry_input = GeometryInput.from_input(geometry)
params = RequestParams.from_kwargs(**kwargs)

if (
params.output_type == "geojson"
and params.bind_zip
and not output_options.download_file
):
log.info("Requesting OSM geojson data snapshot")
return await self.api.request_plain_geojson_snapshot(geometry_input, params)

# Request snapshot
log.info("Requesting OSM data snapshot for %s", params.file_name)
task_response = await self.api.request_snapshot(geometry_input, params)
Expand Down