moonstream/moonstreamapi/moonstreamapi/routes/whales.py

68 wiersze
2.1 KiB
Python
Czysty Zwykły widok Historia

2021-10-01 14:07:42 +00:00
"""
Moonstream's /whales endpoints.
These endpoints provide public access to whale watch summaries. No authentication required.
"""
import logging
from typing import Optional
2021-10-11 12:12:19 +00:00
from fastapi import APIRouter, Depends, Query
2021-10-01 14:07:42 +00:00
from sqlalchemy.orm import Session
2021-12-16 13:26:04 +00:00
from moonstreamdb import db
2021-10-01 14:07:42 +00:00
from .. import data
2021-12-02 13:22:48 +00:00
from ..providers.bugout import ethereum_whalewatch_provider
2021-10-01 14:07:42 +00:00
from ..settings import (
MOONSTREAM_ADMIN_ACCESS_TOKEN,
MOONSTREAM_DATA_JOURNAL_ID,
2021-12-16 13:26:04 +00:00
bugout_client,
2021-10-01 14:07:42 +00:00
)
from ..stream_queries import StreamQuery
logger = logging.getLogger(__name__)
2021-10-11 12:12:19 +00:00
router = APIRouter(prefix="/whales")
2021-10-01 14:07:42 +00:00
2021-10-11 12:12:19 +00:00
@router.get("/", tags=["whales"], response_model=data.GetEventsResponse)
2021-10-01 14:07:42 +00:00
async def stream_handler(
start_time: int = Query(0),
end_time: Optional[int] = Query(None),
include_start: bool = Query(False),
include_end: bool = Query(False),
db_session: Session = Depends(db.yield_db_session),
) -> data.GetEventsResponse:
"""
Retrieves the list of whales spotted over given stream boundary
- **start_time**: Timestamp. Must be provided otherwise this request will hang
- **end_time**: Timestamp. Optional.
- **include_start** (string): is start_time inclusive or not
- **include_end** (string): is end_time inclusive or not
"""
stream_boundary = data.StreamBoundary(
start_time=start_time,
end_time=end_time,
include_start=include_start,
include_end=include_end,
)
2021-12-02 13:22:48 +00:00
result = ethereum_whalewatch_provider.get_events(
2021-10-01 14:07:42 +00:00
db_session=db_session,
bugout_client=bugout_client,
data_journal_id=MOONSTREAM_DATA_JOURNAL_ID,
data_access_token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
stream_boundary=stream_boundary,
2021-12-02 13:22:48 +00:00
user_subscriptions={ethereum_whalewatch_provider.event_type: []},
query=StreamQuery(subscription_types=[ethereum_whalewatch_provider.event_type]),
2021-10-01 14:07:42 +00:00
)
if result is None:
return data.GetEventsResponse(stream_boundary=stream_boundary, events=[])
provider_stream_boundary, events = result
return data.GetEventsResponse(
stream_boundary=provider_stream_boundary, events=events
)