feat(weather): implement provider in weather api endpoints
This commit is contained in:
0
gallery/easel/depends/__init__.py
Normal file
0
gallery/easel/depends/__init__.py
Normal file
13
gallery/easel/depends/weather.py
Normal file
13
gallery/easel/depends/weather.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import Depends
|
||||||
|
|
||||||
|
from gallery.easel.core import AppRequest
|
||||||
|
from gallery.sketch.weather.api import WeatherApi
|
||||||
|
|
||||||
|
|
||||||
|
async def get_weather_api(request: AppRequest, provider: str | None = None) -> WeatherApi:
|
||||||
|
return request.app.state.api.get_weather(provider)
|
||||||
|
|
||||||
|
|
||||||
|
WeatherApiDepends = Annotated[WeatherApi, Depends(get_weather_api)]
|
||||||
@@ -3,6 +3,7 @@ import datetime
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from gallery.easel.core import AppRequest
|
from gallery.easel.core import AppRequest
|
||||||
|
from gallery.easel.depends.weather import WeatherApiDepends
|
||||||
from gallery.sketch.weather.model import Location, WeatherResponse
|
from gallery.sketch.weather.model import Location, WeatherResponse
|
||||||
|
|
||||||
router = APIRouter(prefix="/weather")
|
router = APIRouter(prefix="/weather")
|
||||||
@@ -13,19 +14,16 @@ async def get_api_weather_providers(request: AppRequest) -> list[str]:
|
|||||||
return request.app.state.api.get_api_providers("weather")
|
return request.app.state.api.get_api_providers("weather")
|
||||||
|
|
||||||
|
|
||||||
@router.get("/locations")
|
@router.get("/{provider}/locations")
|
||||||
async def get_api_weather_locations(request: AppRequest, query: str) -> list[Location]:
|
async def get_api_weather_locations(weather_api: WeatherApiDepends, query: str) -> list[Location]:
|
||||||
weather_api = request.app.state.api.weather
|
|
||||||
return await weather_api.find_locations(query)
|
return await weather_api.find_locations(query)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{location}/day/{date}")
|
@router.get("/{provider}/{location}/day/{date}")
|
||||||
async def get_api_weather_day(request: AppRequest, location: str, date: datetime.date) -> WeatherResponse:
|
async def get_api_weather_day(weather_api: WeatherApiDepends, location: str, date: datetime.date) -> WeatherResponse:
|
||||||
weather_api = request.app.state.api.weather
|
|
||||||
return await weather_api.get_day(location, date)
|
return await weather_api.get_day(location, date)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{location}/days/{days}")
|
@router.get("/{provider}/{location}/days/{days}")
|
||||||
async def get_api_weather_days(request: AppRequest, location: str, days: int) -> WeatherResponse:
|
async def get_api_weather_days(weather_api: WeatherApiDepends, location: str, days: int) -> WeatherResponse:
|
||||||
weather_api = request.app.state.api.weather
|
|
||||||
return await weather_api.get_days(location, days)
|
return await weather_api.get_days(location, days)
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter
|
||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||||
|
|
||||||
from gallery.easel.core import AppRequest
|
from gallery.easel.core import AppRequest
|
||||||
|
from gallery.easel.depends.weather import WeatherApiDepends
|
||||||
from gallery.sketch.weather.api import WeatherApi
|
from gallery.sketch.weather.api import WeatherApi
|
||||||
from gallery.sketch.weather.model import WeatherResponse
|
from gallery.sketch.weather.model import WeatherResponse
|
||||||
|
|
||||||
@@ -14,13 +14,6 @@ from ..common.utils.template import build_templates
|
|||||||
from .filters import cloudness_icon, wind_direction_icon
|
from .filters import cloudness_icon, wind_direction_icon
|
||||||
|
|
||||||
|
|
||||||
async def get_weather_api(request: AppRequest, provider: str | None = None) -> WeatherApi:
|
|
||||||
return request.app.state.api.get_weather(provider)
|
|
||||||
|
|
||||||
|
|
||||||
WeatherApiDepends = Annotated[WeatherApi, Depends(get_weather_api)]
|
|
||||||
|
|
||||||
|
|
||||||
def context_procesor(request: AppRequest) -> dict:
|
def context_procesor(request: AppRequest) -> dict:
|
||||||
return {
|
return {
|
||||||
"providers": request.app.state.api.get_api_providers(WeatherApi.TYPE),
|
"providers": request.app.state.api.get_api_providers(WeatherApi.TYPE),
|
||||||
@@ -94,3 +87,10 @@ async def get_weather_tag(request: AppRequest, weather_api: WeatherApiDepends, l
|
|||||||
else:
|
else:
|
||||||
raise ValueError(tag)
|
raise ValueError(tag)
|
||||||
return build_weather_response(request, response)
|
return build_weather_response(request, response)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/zmiyevka-184640/tag/{tag}", response_class=RedirectResponse)
|
||||||
|
async def get_zmiyevka_weather_compat(request: AppRequest):
|
||||||
|
path_parts = request.url.path.split("/")
|
||||||
|
path_parts.insert(2, "gismeteo")
|
||||||
|
return RedirectResponse("/".join(path_parts))
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class Forecast(Model):
|
|||||||
|
|
||||||
class Location(Model):
|
class Location(Model):
|
||||||
name: str
|
name: str
|
||||||
local_names: dict[str, str]
|
local_names: dict[str, str] | None = None
|
||||||
lat: float
|
lat: float
|
||||||
lon: float
|
lon: float
|
||||||
country: str
|
country: str
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ class WeatherLocationElement extends HTMLElement {
|
|||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
this.removeEventListener("click", this.handleClick);
|
this.removeEventListener("click", this.handleClick);
|
||||||
|
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleClick(event: MouseEvent) {
|
handleClick(event: MouseEvent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user