|
| 1 | +from collections.abc import AsyncGenerator |
| 2 | + |
| 3 | +from .abstract_base_client import AbstractBaseClient |
| 4 | +from .client_configuration import ClientConfiguration |
| 5 | +from .client_options import ClientOptions |
| 6 | +from .event.event_candidate import EventCandidate |
| 7 | +from .event.event_context import EventContext |
| 8 | +from .handlers.observe_events.observe_events import observe_events |
| 9 | +from .handlers.observe_events.observe_events_options import ObserveEventsOptions |
| 10 | +from .handlers.read_event_types.event_type import EventType |
| 11 | +from .handlers.read_event_types.read_event_types import read_event_types |
| 12 | +from .handlers.register_event_schema.register_event_schema import register_event_schema |
| 13 | +from .http_client.http_client import HttpClient |
| 14 | +from .handlers.ping import ping |
| 15 | +from .handlers.read_events import read_events, ReadEventsOptions |
| 16 | +from .handlers.read_subjects import read_subjects |
| 17 | +from .handlers.store_item import StoreItem |
| 18 | +from .handlers.write_events import Precondition, write_events |
| 19 | + |
| 20 | + |
| 21 | +# pylint: disable=R6007 |
| 22 | +# Reason: This class explicitly specifies the return type as None |
| 23 | +# for better readability. Even though it is not necessary, |
| 24 | +# it makes the return type clear without needing to read any |
| 25 | +# documentation or code. |
| 26 | +class Client(AbstractBaseClient): |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + base_url: str, |
| 30 | + access_token: str, |
| 31 | + options: ClientOptions = ClientOptions() |
| 32 | + ): |
| 33 | + configuration = ClientConfiguration( |
| 34 | + base_url=base_url, |
| 35 | + timeout_seconds=options.timeout_seconds, |
| 36 | + access_token=access_token, |
| 37 | + protocol_version=options.protocol_version, |
| 38 | + max_tries=options.max_tries |
| 39 | + ) |
| 40 | + |
| 41 | + self.__http_client = HttpClient(configuration) |
| 42 | + |
| 43 | + async def initialize(self) -> None: |
| 44 | + await self.__http_client.initialize() |
| 45 | + |
| 46 | + async def close(self) -> None: |
| 47 | + await self.__http_client.close() |
| 48 | + |
| 49 | + @property |
| 50 | + def http_client(self) -> HttpClient: |
| 51 | + return self.__http_client |
| 52 | + |
| 53 | + async def ping(self) -> None: |
| 54 | + return await ping(self) |
| 55 | + |
| 56 | + async def read_subjects( |
| 57 | + self, |
| 58 | + base_subject: str |
| 59 | + ) -> AsyncGenerator[str, None]: |
| 60 | + async for subject in read_subjects(self, base_subject): |
| 61 | + yield subject |
| 62 | + |
| 63 | + async def read_events( |
| 64 | + self, |
| 65 | + subject: str, |
| 66 | + options: ReadEventsOptions |
| 67 | + ) -> AsyncGenerator[StoreItem, None]: |
| 68 | + async for event in read_events(self, subject, options): |
| 69 | + yield event |
| 70 | + |
| 71 | + async def read_event_types(self) -> AsyncGenerator[EventType, None]: |
| 72 | + async for event_type in read_event_types(self): |
| 73 | + yield event_type |
| 74 | + |
| 75 | + async def register_event_schema(self, event_type: str, json_schema: str) -> None: |
| 76 | + await register_event_schema(self, event_type, json_schema) |
| 77 | + |
| 78 | + async def observe_events( |
| 79 | + self, |
| 80 | + subject: str, |
| 81 | + options: ObserveEventsOptions |
| 82 | + ) -> AsyncGenerator[StoreItem, None]: |
| 83 | + async for event in observe_events(self, subject, options): |
| 84 | + yield event |
| 85 | + |
| 86 | + async def write_events( |
| 87 | + self, |
| 88 | + event_candidates: list[EventCandidate], |
| 89 | + preconditions: list[Precondition] = None |
| 90 | + ) -> list[EventContext]: |
| 91 | + if preconditions is None: |
| 92 | + preconditions = [] |
| 93 | + return await write_events(self, event_candidates, preconditions) |
0 commit comments