Coverage for src\zapy\requests\models.py: 93%

1 from __future__ import annotations

2 from pathlib import Path

3

4 from pydantic import BaseModel, Field

5

6 from zapy.__init__ import __version__

7 from zapy.base import ZapyCell, Metadata

8 from zapy.test import assert_test_result_dict, AssertTestResultMixin

9

10 Code = list[str] | str

11

12 class KeyValueItem(BaseModel):

13 key: str

14 value: str

15 active: bool = True

16

17

18 class RequestMetadata(Metadata):

19 cell_type: str = 'zapy.ZapyRequest'

20 v: str = __version__

21

22

23 class ZapyRequest(BaseModel, ZapyCell):

24 metadata: RequestMetadata = Field(default_factory=RequestMetadata)

25 endpoint: str

26 method: str

27 params: list[KeyValueItem] = Field(default_factory=list)

28 headers: list[KeyValueItem] = Field(default_factory=list)

29 variables: list[KeyValueItem] = Field(default_factory=list)

30 script: Code = ""

31 body_type: str = 'None'

32 body: Code | list[KeyValueItem] | None = None

33

34 @classmethod

35 def from_dict(cls, value: dict) -> ZapyRequest:

36 return cls.model_validate(value)

37

38 @classmethod

39 def from_path(cls, file_path: str | Path) -> ZapyRequest:

40 import json

41 with open(file_path) as f:

42 loaded_json = json.load(f)

43 return cls.from_dict(loaded_json)

44

45 async def send(self, *, raise_assert: AssertTestResultMixin | bool = True, **kwargs):

46 from .requester import send_request

47

48 request_wrapper = await send_request(self, **kwargs)

49

50 if request_wrapper.test_result:

51 if raise_assert is True: 51 ↛ 53line 51 didn't jump to line 53, because the condition on line 51 was never false

52 assert_test_result_dict(request_wrapper.test_result)

53 elif isinstance(raise_assert, AssertTestResultMixin):

54 raise_assert.assertZapyTestResults(request_wrapper.test_result)

55

56 return request_wrapper.response

57

58

59 from httpx._client import *

60 from typing_extensions import TypedDict

61

62

63 class HttpxArguments(TypedDict):

64 method: str

65 url: URLTypes

66 content: typing.Optional[RequestContent]

67 data: typing.Optional[RequestData]

68 files: typing.Optional[RequestFiles]

69 json: typing.Optional[typing.Any]

70 params: typing.Optional[QueryParamTypes]

71 headers: typing.Optional[HeaderTypes]

72 cookies: typing.Optional[CookieTypes]

73 auth: typing.Union[AuthTypes, UseClientDefault, None]

74 follow_redirects: typing.Union[bool, UseClientDefault]

75 timeout: typing.Union[TimeoutTypes, UseClientDefault]

76 extensions: typing.Optional[RequestExtensions]