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

1"""Module for defining a presenter.""" 

2 

3from abc import ABC, abstractmethod 

4from dataclasses import dataclass 

5from typing import AsyncIterator 

6 

7 

8@dataclass(frozen=True, kw_only=True, slots=True) 

9class IterableResult[T]: 

10 """A dataclass used to represent a result with multiple entities.""" 

11 

12 count: int 

13 offset: int = 0 

14 limit: int = 0 

15 iterator: AsyncIterator[T] 

16 

17 

18class Presenter[T](ABC): 

19 """An interface for a presenter. 

20 

21 A presenter is used to transform an entity into another object that can be used 

22 in a view. 

23 

24 An example: convert to a JSON:API resource for returning the entity in a restful 

25 API. 

26 """ 

27 

28 @abstractmethod 

29 def present(self, use_case_result: T) -> None: 

30 """Present the entity. 

31 

32 This method is responsible for converting the entity. 

33 """ 

34 

35 

36class AsyncPresenter[T](ABC): 

37 """An interface for an async presenter. 

38 

39 A presenter is used to transform an entity into another object that can be used 

40 in a view. 

41 

42 An example: convert to a JSON:API resource for returning the entity in a restful 

43 API. 

44 """ 

45 

46 @abstractmethod 

47 async def present(self, use_case_result: T) -> None: 

48 """Present the entity. 

49 

50 This method is responsible for converting the entity. 

51 """