2025-10-15 20:53:52 +02:00
|
|
|
import logging
|
2025-11-03 13:17:19 +01:00
|
|
|
from typing import TYPE_CHECKING, AsyncIterator, List
|
2025-10-03 22:27:30 +02:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
2025-10-15 20:53:52 +02:00
|
|
|
import httpx
|
2025-10-03 22:27:30 +02:00
|
|
|
import requests
|
2025-11-03 13:17:19 +01:00
|
|
|
from pydantic import ValidationError
|
2025-10-03 22:27:30 +02:00
|
|
|
|
|
|
|
|
from node.api.base import NodeApi
|
2025-10-30 11:48:34 +01:00
|
|
|
from node.api.serializers.block import BlockSerializer
|
|
|
|
|
from node.api.serializers.health import HealthSerializer
|
2025-10-15 20:53:52 +02:00
|
|
|
|
2025-11-03 13:17:19 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from core.app import NBESettings
|
|
|
|
|
|
|
|
|
|
|
2025-10-15 20:53:52 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2025-10-03 22:27:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class HttpNodeApi(NodeApi):
|
2025-10-15 20:53:52 +02:00
|
|
|
ENDPOINT_INFO = "/cryptarchia/info"
|
|
|
|
|
ENDPOINT_TRANSACTIONS = "/cryptarchia/transactions"
|
|
|
|
|
ENDPOINT_BLOCKS = "/cryptarchia/blocks"
|
|
|
|
|
ENDPOINT_BLOCKS_STREAM = "/cryptarchia/blocks/stream"
|
2025-10-03 22:27:30 +02:00
|
|
|
|
2025-11-03 13:17:19 +01:00
|
|
|
def __init__(self, settings: "NBESettings"):
|
|
|
|
|
self.host: str = settings.node_api_host
|
|
|
|
|
self.port: int = settings.node_api_port
|
|
|
|
|
self.protocol: str = settings.node_api_protocol or "http"
|
|
|
|
|
self.timeout: int = settings.node_api_timeout or 60
|
2025-10-03 22:27:30 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def base_url(self):
|
|
|
|
|
return f"{self.protocol}://{self.host}:{self.port}"
|
|
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
async def get_health(self) -> HealthSerializer:
|
2025-10-15 20:53:52 +02:00
|
|
|
url = urljoin(self.base_url, self.ENDPOINT_INFO)
|
2025-10-03 22:27:30 +02:00
|
|
|
response = requests.get(url, timeout=60)
|
2025-10-15 20:53:52 +02:00
|
|
|
if response.status_code == 200:
|
2025-10-30 11:48:34 +01:00
|
|
|
return HealthSerializer.from_healthy()
|
2025-10-15 20:53:52 +02:00
|
|
|
else:
|
2025-10-30 11:48:34 +01:00
|
|
|
return HealthSerializer.from_unhealthy()
|
2025-10-03 22:27:30 +02:00
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
async def get_blocks(self, slot_from: int, slot_to: int) -> List[BlockSerializer]:
|
2025-10-15 20:53:52 +02:00
|
|
|
query_string = f"slot_from={slot_from}&slot_to={slot_to}"
|
|
|
|
|
endpoint = urljoin(self.base_url, self.ENDPOINT_BLOCKS)
|
|
|
|
|
url = f"{endpoint}?{query_string}"
|
2025-10-03 22:27:30 +02:00
|
|
|
response = requests.get(url, timeout=60)
|
2025-10-15 20:53:52 +02:00
|
|
|
python_json = response.json()
|
2025-10-30 11:48:34 +01:00
|
|
|
blocks = [BlockSerializer.model_validate(item) for item in python_json]
|
2025-10-15 20:53:52 +02:00
|
|
|
return blocks
|
|
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
async def get_blocks_stream(self) -> AsyncIterator[BlockSerializer]:
|
2025-10-15 20:53:52 +02:00
|
|
|
url = urljoin(self.base_url, self.ENDPOINT_BLOCKS_STREAM)
|
|
|
|
|
|
|
|
|
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
|
|
|
|
async with client.stream("GET", url) as response:
|
|
|
|
|
response.raise_for_status() # TODO: Result
|
|
|
|
|
|
|
|
|
|
async for line in response.aiter_lines():
|
|
|
|
|
if not line:
|
|
|
|
|
continue
|
2025-10-30 11:48:34 +01:00
|
|
|
try:
|
|
|
|
|
block = BlockSerializer.model_validate_json(line)
|
2025-11-03 13:17:19 +01:00
|
|
|
except ValidationError as error:
|
|
|
|
|
logger.exception(error)
|
|
|
|
|
continue
|
2025-10-30 11:48:34 +01:00
|
|
|
|
2025-10-15 20:53:52 +02:00
|
|
|
logger.debug(f"Received new block from Node: {block}")
|
|
|
|
|
yield block
|