Source code for action0.open_meteo.geocoding.models
# generated by action0-client-openapi v0.1.2 from geocoding.yml — do not edit
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
[docs]
@dataclass
class ErrorResponse:
"""The error answer of a rejected request."""
#: Always true.
error: bool | None = None
#: What was wrong with the request.
reason: str | None = None
[docs]
def error_response_from_json(data: Any) -> ErrorResponse:
"""
Build a :py:class:`ErrorResponse` from one decoded JSON object.
:param data: the decoded JSON object
:return: the ErrorResponse
"""
return ErrorResponse(
error=data.get("error"),
reason=data.get("reason"),
)
[docs]
@dataclass
class Location:
"""One geocoded location."""
#: The location's GeoNames id, usable with /v1/get.
id: int
#: The location name, translated when possible.
name: str
#: WGS84 latitude.
latitude: float
#: WGS84 longitude.
longitude: float
#: Elevation above mean sea level, in metres.
elevation: float | None = None
#: The GeoNames feature code (PPLC is a capital, ...).
feature_code: str | None = None
#: The ISO-3166-1 alpha-2 country code.
country_code: str | None = None
#: The country name.
country: str | None = None
#: The country's GeoNames id.
country_id: int | None = None
#: The number of inhabitants.
population: int | None = None
#: The postal codes attached to the location.
postcodes: list[str] | None = None
#: The IANA time zone of the location.
timezone: str | None = None
#: The first-level administrative area (state, ...).
admin1: str | None = None
#: The second-level administrative area (district, ...).
admin2: str | None = None
#: The third-level administrative area.
admin3: str | None = None
#: The fourth-level administrative area.
admin4: str | None = None
#: The GeoNames id of the first-level area.
admin1_id: int | None = None
#: The GeoNames id of the second-level area.
admin2_id: int | None = None
#: The GeoNames id of the third-level area.
admin3_id: int | None = None
#: The GeoNames id of the fourth-level area.
admin4_id: int | None = None
[docs]
def location_from_json(data: Any) -> Location:
"""
Build a :py:class:`Location` from one decoded JSON object.
:param data: the decoded JSON object
:return: the Location
"""
return Location(
id=data["id"],
name=data["name"],
latitude=data["latitude"],
longitude=data["longitude"],
elevation=data.get("elevation"),
feature_code=data.get("feature_code"),
country_code=data.get("country_code"),
country=data.get("country"),
country_id=data.get("country_id"),
population=data.get("population"),
postcodes=data.get("postcodes"),
timezone=data.get("timezone"),
admin1=data.get("admin1"),
admin2=data.get("admin2"),
admin3=data.get("admin3"),
admin4=data.get("admin4"),
admin1_id=data.get("admin1_id"),
admin2_id=data.get("admin2_id"),
admin3_id=data.get("admin3_id"),
admin4_id=data.get("admin4_id"),
)
[docs]
@dataclass
class SearchLocationsResponse:
"""The ``SearchLocationsResponse`` model."""
#: The matching locations, best match first. Absent when nothing matched.
results: list[Location] | None = None
generationtime_ms: float | None = None
[docs]
def search_locations_response_from_json(data: Any) -> SearchLocationsResponse:
"""
Build a :py:class:`SearchLocationsResponse` from one decoded JSON object.
:param data: the decoded JSON object
:return: the SearchLocationsResponse
"""
return SearchLocationsResponse(
results=(
[location_from_json(item) for item in data["results"]]
if data.get("results") is not None
else None
),
generationtime_ms=data.get("generationtime_ms"),
)