|
| 1 | +from typing import Any, Dict, Optional, Union |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +from ...client import Client |
| 6 | +from ...types import Response |
| 7 | + |
| 8 | + |
| 9 | +def _get_kwargs( |
| 10 | + *, |
| 11 | + client: Client, |
| 12 | + my_token: str, |
| 13 | +) -> Dict[str, Any]: |
| 14 | + url = "{}/auth/token_with_cookie".format(client.base_url) |
| 15 | + |
| 16 | + headers: Dict[str, Any] = client.get_headers() |
| 17 | + cookies: Dict[str, Any] = client.get_cookies() |
| 18 | + |
| 19 | + cookies["MyToken"] = my_token |
| 20 | + |
| 21 | + return { |
| 22 | + "url": url, |
| 23 | + "headers": headers, |
| 24 | + "cookies": cookies, |
| 25 | + "timeout": client.get_timeout(), |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def _parse_response(*, response: httpx.Response) -> Optional[Union[None, None]]: |
| 30 | + if response.status_code == 200: |
| 31 | + response_200 = None |
| 32 | + |
| 33 | + return response_200 |
| 34 | + if response.status_code == 401: |
| 35 | + response_401 = None |
| 36 | + |
| 37 | + return response_401 |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +def _build_response(*, response: httpx.Response) -> Response[Union[None, None]]: |
| 42 | + return Response( |
| 43 | + status_code=response.status_code, |
| 44 | + content=response.content, |
| 45 | + headers=response.headers, |
| 46 | + parsed=_parse_response(response=response), |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def sync_detailed( |
| 51 | + *, |
| 52 | + client: Client, |
| 53 | + my_token: str, |
| 54 | +) -> Response[Union[None, None]]: |
| 55 | + kwargs = _get_kwargs( |
| 56 | + client=client, |
| 57 | + my_token=my_token, |
| 58 | + ) |
| 59 | + |
| 60 | + response = httpx.get( |
| 61 | + **kwargs, |
| 62 | + ) |
| 63 | + |
| 64 | + return _build_response(response=response) |
| 65 | + |
| 66 | + |
| 67 | +def sync( |
| 68 | + *, |
| 69 | + client: Client, |
| 70 | + my_token: str, |
| 71 | +) -> Optional[Union[None, None]]: |
| 72 | + """ Test optional cookie parameters """ |
| 73 | + |
| 74 | + return sync_detailed( |
| 75 | + client=client, |
| 76 | + my_token=my_token, |
| 77 | + ).parsed |
| 78 | + |
| 79 | + |
| 80 | +async def asyncio_detailed( |
| 81 | + *, |
| 82 | + client: Client, |
| 83 | + my_token: str, |
| 84 | +) -> Response[Union[None, None]]: |
| 85 | + kwargs = _get_kwargs( |
| 86 | + client=client, |
| 87 | + my_token=my_token, |
| 88 | + ) |
| 89 | + |
| 90 | + async with httpx.AsyncClient() as _client: |
| 91 | + response = await _client.get(**kwargs) |
| 92 | + |
| 93 | + return _build_response(response=response) |
| 94 | + |
| 95 | + |
| 96 | +async def asyncio( |
| 97 | + *, |
| 98 | + client: Client, |
| 99 | + my_token: str, |
| 100 | +) -> Optional[Union[None, None]]: |
| 101 | + """ Test optional cookie parameters """ |
| 102 | + |
| 103 | + return ( |
| 104 | + await asyncio_detailed( |
| 105 | + client=client, |
| 106 | + my_token=my_token, |
| 107 | + ) |
| 108 | + ).parsed |
0 commit comments