Coverage for src/fluree_py/http/response.py: 85%

27 statements  

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

1from dataclasses import dataclass 

2from typing import TypeVar, cast 

3 

4from httpx import Headers, Response 

5 

6from fluree_py.types.common import JsonArray, JsonObject 

7 

8T = TypeVar("T") 

9 

10 

11@dataclass(frozen=True, kw_only=True) 

12class FlureeResponse: 

13 response: Response 

14 

15 def json(self) -> JsonObject | JsonArray: 

16 """Parse the response as JSON.""" 

17 return self.response.json() 

18 

19 @property 

20 def text(self) -> str: 

21 """Get the response text.""" 

22 return self.response.text 

23 

24 @property 

25 def bytes(self) -> bytes: 

26 """Get the response bytes.""" 

27 return self.response.content 

28 

29 @property 

30 def headers(self) -> Headers: 

31 """Get the response headers.""" 

32 return self.response.headers 

33 

34 @property 

35 def status_code(self) -> int: 

36 """Get the response status code.""" 

37 return self.response.status_code 

38 

39 @property 

40 def is_success(self) -> bool: 

41 """Check if the response was successful.""" 

42 return self.response.is_success 

43 

44 def cast(self, type_: type[T]) -> T: 

45 """Cast the JSON response to a specific type.""" 

46 return cast(T, self.json())