|
| 1 | +import dataclasses |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from hyperleda import common, config |
| 5 | +from hyperleda.data import model |
| 6 | + |
| 7 | + |
| 8 | +class HyperLedaDataClient: |
| 9 | + """ |
| 10 | + Client for the HyperLeda data API, providing access to public query endpoints. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self, endpoint: str = config.DEFAULT_ENDPOINT): |
| 14 | + self.endpoint = endpoint |
| 15 | + |
| 16 | + def _request(self, method: str, path: str, query: dict[str, Any] | None = None, stream: bool = False) -> Any: |
| 17 | + return common.request(method, f"{self.endpoint}{path}", query=query, stream=stream) |
| 18 | + |
| 19 | + def query_simple(self, req: model.QuerySimpleRequestSchema) -> model.QuerySimpleResponseSchema: |
| 20 | + """ |
| 21 | + Query data about objects using simple parameters (AND logic). |
| 22 | + """ |
| 23 | + response = self._request( |
| 24 | + "GET", |
| 25 | + "/api/v1/query/simple", |
| 26 | + query=dataclasses.asdict(req), |
| 27 | + ) |
| 28 | + data = response.json() |
| 29 | + return model.QuerySimpleResponseSchema(**data["data"]) |
| 30 | + |
| 31 | + def query(self, req: model.QueryRequestSchema) -> model.QueryResponseSchema: |
| 32 | + """ |
| 33 | + Query data about objects using a query string (functions and operators). |
| 34 | + """ |
| 35 | + response = self._request( |
| 36 | + "GET", |
| 37 | + "/api/v1/query", |
| 38 | + query=dataclasses.asdict(req), |
| 39 | + ) |
| 40 | + data = response.json() |
| 41 | + return model.QueryResponseSchema(**data["data"]) |
| 42 | + |
| 43 | + def query_fits(self, req: model.FITSRequestSchema) -> bytes: |
| 44 | + """ |
| 45 | + Query data about objects and return as FITS file (binary). |
| 46 | + """ |
| 47 | + response = self._request( |
| 48 | + "GET", |
| 49 | + "/api/v1/query/fits", |
| 50 | + query=dataclasses.asdict(req), |
| 51 | + stream=True, |
| 52 | + ) |
| 53 | + return response.content |
0 commit comments