address-service/address/dadata.py

67 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass
from constance import config
from ninja.errors import HttpError
class DaDataDisabledError(HttpError):
def __init__(self, message: str = "Интеграция с DaData отключена"):
super().__init__(503, message)
@dataclass(frozen=True)
class DaDataStreet:
value: str
fias_id: str
kladr_id: str
street: str
street_type: str
street_with_type: str
city: str
region_code: str
@dataclass(frozen=True)
class DaDataHouse:
value: str
fias_id: str
house: str
house_type: str
block: str
postal_code: str
street: str
city: str
region_code: str
def require_dadata() -> str:
if not config.DADATA_IS_ENABLED:
raise DaDataDisabledError()
token = (config.DADATA_TOKEN or "").strip()
if not token:
raise DaDataDisabledError("Не задан токен DaData")
return token
def suggest_streets(
*,
region_code: str,
city: str,
query: str,
limit: int,
) -> list[DaDataStreet]:
require_dadata()
return []
def suggest_houses(
*,
region_code: str,
city: str,
street: str,
query: str,
limit: int,
) -> list[DaDataHouse]:
require_dadata()
return []