Coverage for src/kwai/core/domain/presenter.py: 100%
11 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for defining a presenter."""
3from abc import ABC, abstractmethod
4from dataclasses import dataclass
5from typing import AsyncIterator
8@dataclass(frozen=True, kw_only=True, slots=True)
9class IterableResult[T]:
10 """A dataclass used to represent a result with multiple entities."""
12 count: int
13 offset: int = 0
14 limit: int = 0
15 iterator: AsyncIterator[T]
18class Presenter[T](ABC):
19 """An interface for a presenter.
21 A presenter is used to transform an entity into another object that can be used
22 in a view.
24 An example: convert to a JSON:API resource for returning the entity in a restful
25 API.
26 """
28 @abstractmethod
29 def present(self, use_case_result: T) -> None:
30 """Present the entity.
32 This method is responsible for converting the entity.
33 """
36class AsyncPresenter[T](ABC):
37 """An interface for an async presenter.
39 A presenter is used to transform an entity into another object that can be used
40 in a view.
42 An example: convert to a JSON:API resource for returning the entity in a restful
43 API.
44 """
46 @abstractmethod
47 async def present(self, use_case_result: T) -> None:
48 """Present the entity.
50 This method is responsible for converting the entity.
51 """