Coverage for src/kwai/api/v1/auth/presenters.py: 75%

16 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2024-01-01 00:00 +0000

1"""Module that defines presenters for the auth api.""" 

2 

3from kwai.api.v1.auth.schemas.revoked_user import RevokedUserDocument 

4from kwai.api.v1.auth.schemas.user_account import ( 

5 UserAccountDocument, 

6 UserAccountResource, 

7 UserAccountsDocument, 

8) 

9from kwai.core.domain.presenter import AsyncPresenter, IterableResult, Presenter 

10from kwai.core.json_api import JsonApiPresenter, Meta 

11from kwai.modules.identity.users.user_account import UserAccountEntity 

12 

13 

14class JsonApiUserAccountsPresenter( 

15 JsonApiPresenter[UserAccountsDocument], 

16 AsyncPresenter[IterableResult[UserAccountEntity]], 

17): 

18 """A presenter that transform an iterable list of user account entities into a JSON:API document.""" 

19 

20 async def present(self, result: IterableResult[UserAccountEntity]) -> None: 

21 self._document = UserAccountsDocument( 

22 meta=Meta(count=result.count, offset=result.offset, limit=result.limit), 

23 data=[], 

24 ) 

25 async for user_account in result.iterator: 

26 self._document.merge( 

27 UserAccountDocument(data=UserAccountResource.create(user_account)) 

28 ) 

29 

30 

31class JsonApiUserAccountPresenter( 

32 JsonApiPresenter[UserAccountDocument], Presenter[UserAccountEntity] 

33): 

34 """A presenter that transforms a user account entity into a JSON:API document.""" 

35 

36 def present(self, result: UserAccountEntity) -> None: 

37 self._document = UserAccountDocument(data=UserAccountResource.create(result)) 

38 

39 

40class JsonApiRevokedUserPresenter( 

41 JsonApiPresenter[RevokedUserDocument], 

42 Presenter[UserAccountEntity], 

43): 

44 """A presenter that transform a user account entity into a JSON:API document. 

45 

46 The document will be a [RevokedUserDocument]. 

47 """ 

48 

49 def present(self, use_case_result: UserAccountEntity) -> None: 

50 self._document = RevokedUserDocument.create(use_case_result)