Coverage for src/fluree_py/http/mixin/commit.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-02 03:03 +0000

1"""Mixins for committing transactions to the Fluree ledger.""" 

2 

3from typing import Generic, TypeVar 

4 

5from httpx import AsyncClient, Client 

6 

7from fluree_py.http.protocol.mixin.commit import SupportsAsyncCommit, SupportsCommit 

8from fluree_py.http.protocol.mixin.request import SupportsRequestCreation 

9from fluree_py.http.response import FlureeResponse 

10 

11T = TypeVar("T", bound=SupportsRequestCreation) 

12 

13 

14class CommitMixin(SupportsCommit, Generic[T]): 

15 """Synchronous commit functionality for Fluree transactions.""" 

16 

17 def commit(self: T) -> FlureeResponse: 

18 """Executes the transaction synchronously. 

19 

20 Exceptions: 

21 httpx.RequestError: If the HTTP request fails. 

22 TypeError: If the type parameter cannot be resolved. 

23 """ 

24 request = self.get_request() 

25 with Client() as client: 

26 response = client.send(request) 

27 return FlureeResponse(response=response) 

28 

29 

30class AsyncCommitMixin(SupportsAsyncCommit, Generic[T]): 

31 """Asynchronous commit functionality for Fluree transactions.""" 

32 

33 async def acommit(self: T) -> FlureeResponse: 

34 """Executes the transaction asynchronously. 

35 

36 Exceptions: 

37 httpx.RequestError: If the HTTP request fails. 

38 TypeError: If the type parameter cannot be resolved. 

39 """ 

40 request = self.get_request() 

41 async with AsyncClient() as client: 

42 response = await client.send(request) 

43 return FlureeResponse(response=response) 

44 

45 

46class CommitableMixin(CommitMixin[T], AsyncCommitMixin[T], Generic[T]): 

47 """Combines synchronous and asynchronous commit capabilities."""