Source code for action0.open_meteo.geocoding.operations
# generated by action0-client-openapi v0.1.2 from geocoding.yml — do not edit
from __future__ import annotations
from typing import Any
from action0.client import JsonOperation
from action0.client import query
from action0.req import Method
from action0.req import Response
from .errors import BadRequestError
from .errors import decode_error
from .models import Location
from .models import SearchLocationsResponse
from .models import error_response_from_json
from .models import location_from_json
from .models import search_locations_response_from_json
[docs]
class SearchLocations(JsonOperation[SearchLocationsResponse]):
"""
``GET /v1/search`` — Search for locations by name
Search for locations by name or postal code. Matching is fuzzy for names of three characters or more; the result is ranked by population. When nothing matches, the response carries no results.
"""
method = Method.GET
path = "/v1/search"
#: The location name to search for. Two characters match exactly, three or more match fuzzily;
#: postal codes work too.
name: str = query()
#: The number of results to return, up to 100.
count: int = query(default=10)
#: A lowercase two-letter language code; names are returned translated when a translation
#: exists.
language: str = query(default="en")
#: Only required for commercial subscriptions.
apikey: str | None = query(default=None)
[docs]
def check(self, response: Response) -> None:
"""
:param response: the response the backend produced
:raises BadRequestError: for the documented ``400`` answer
:raises APIError: for any other non-2xx status
"""
if response.status == 400:
data = decode_error(response)
if isinstance(data, dict):
message = f"{type(self).__name__}: unexpected status {response.status}"
raise BadRequestError(
f"{message} {response.phrase}".rstrip(),
response=response,
error=error_response_from_json(data),
)
super().check(response)
[docs]
def load_json(self, data: Any) -> SearchLocationsResponse:
"""
:param data: the decoded JSON payload
:return: the parsed result
"""
return search_locations_response_from_json(data)
[docs]
class GetLocation(JsonOperation[Location]):
"""
``GET /v1/get`` — Look a location up by id
Return a single location by its (GeoNames) id.
"""
method = Method.GET
path = "/v1/get"
#: The location id, as returned by the search.
id: int = query()
#: Only required for commercial subscriptions.
apikey: str | None = query(default=None)
[docs]
def check(self, response: Response) -> None:
"""
:param response: the response the backend produced
:raises BadRequestError: for the documented ``400`` answer
:raises APIError: for any other non-2xx status
"""
if response.status == 400:
data = decode_error(response)
if isinstance(data, dict):
message = f"{type(self).__name__}: unexpected status {response.status}"
raise BadRequestError(
f"{message} {response.phrase}".rstrip(),
response=response,
error=error_response_from_json(data),
)
super().check(response)
[docs]
def load_json(self, data: Any) -> Location:
"""
:param data: the decoded JSON payload
:return: the parsed result
"""
return location_from_json(data)