Coverage for human_requests/abstraction/response.py: 96%

27 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-13 21:41 +0000

1from dataclasses import dataclass 

2from typing import AsyncContextManager, Callable, Literal, Optional 

3 

4from playwright.async_api import Page 

5 

6from .cookies import Cookie 

7from .http import URL 

8from .request import Request 

9 

10 

11@dataclass(frozen=True) 

12class Response: 

13 """Represents the response of a request.""" 

14 

15 request: Request 

16 """The request that was made.""" 

17 

18 url: URL 

19 """The URL of the response. Due to redirects, it can differ from `request.url`.""" 

20 

21 headers: dict 

22 """The headers of the response.""" 

23 

24 cookies: list[Cookie] 

25 """The cookies of the response.""" 

26 

27 body: str 

28 """The body of the response.""" 

29 

30 status_code: int 

31 """The status code of the response.""" 

32 

33 duration: float 

34 """The duration of the request in seconds.""" 

35 

36 _render_callable: Optional[Callable[..., AsyncContextManager[Page]]] = None 

37 

38 def render( 

39 self, 

40 wait_until: Literal["commit", "load", "domcontentloaded", "networkidle"] = "commit", 

41 retry: int = 2, 

42 ) -> AsyncContextManager[Page]: 

43 """Renders the response content in the current browser. 

44 It will look like we requested it through the browser from the beginning. 

45 

46 Recommended to use in cases when the server returns a JS challenge instead of a response.""" 

47 if self._render_callable: 

48 return self._render_callable(self, wait_until=wait_until, retry=retry) 

49 raise ValueError("Not set render callable for Response")