|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Any, Dict, Optional, Union |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from ... import errors |
| 7 | +from ...client import AuthenticatedClient, Client |
| 8 | +from ...models.error import Error |
| 9 | +from ...models.streamer_token import StreamerToken |
| 10 | +from ...types import Response |
| 11 | + |
| 12 | + |
| 13 | +def _get_kwargs( |
| 14 | + room_id: str, |
| 15 | +) -> Dict[str, Any]: |
| 16 | + return { |
| 17 | + "method": "post", |
| 18 | + "url": "/room/{room_id}/streamer".format( |
| 19 | + room_id=room_id, |
| 20 | + ), |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | +def _parse_response( |
| 25 | + *, client: Union[AuthenticatedClient, Client], response: httpx.Response |
| 26 | +) -> Optional[Union[Error, StreamerToken]]: |
| 27 | + if response.status_code == HTTPStatus.CREATED: |
| 28 | + response_201 = StreamerToken.from_dict(response.json()) |
| 29 | + |
| 30 | + return response_201 |
| 31 | + if response.status_code == HTTPStatus.BAD_REQUEST: |
| 32 | + response_400 = Error.from_dict(response.json()) |
| 33 | + |
| 34 | + return response_400 |
| 35 | + if response.status_code == HTTPStatus.UNAUTHORIZED: |
| 36 | + response_401 = Error.from_dict(response.json()) |
| 37 | + |
| 38 | + return response_401 |
| 39 | + if response.status_code == HTTPStatus.NOT_FOUND: |
| 40 | + response_404 = Error.from_dict(response.json()) |
| 41 | + |
| 42 | + return response_404 |
| 43 | + if response.status_code == HTTPStatus.SERVICE_UNAVAILABLE: |
| 44 | + response_503 = Error.from_dict(response.json()) |
| 45 | + |
| 46 | + return response_503 |
| 47 | + if client.raise_on_unexpected_status: |
| 48 | + raise errors.UnexpectedStatus(response.status_code, response.content) |
| 49 | + else: |
| 50 | + return None |
| 51 | + |
| 52 | + |
| 53 | +def _build_response( |
| 54 | + *, client: Union[AuthenticatedClient, Client], response: httpx.Response |
| 55 | +) -> Response[Union[Error, StreamerToken]]: |
| 56 | + return Response( |
| 57 | + status_code=HTTPStatus(response.status_code), |
| 58 | + content=response.content, |
| 59 | + headers=response.headers, |
| 60 | + parsed=_parse_response(client=client, response=response), |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def sync_detailed( |
| 65 | + room_id: str, |
| 66 | + *, |
| 67 | + client: Union[AuthenticatedClient, Client], |
| 68 | +) -> Response[Union[Error, StreamerToken]]: |
| 69 | + """Generate a token that can be used by a streamer to start streaming |
| 70 | +
|
| 71 | + Args: |
| 72 | + room_id (str): |
| 73 | +
|
| 74 | + Raises: |
| 75 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 76 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Response[Union[Error, StreamerToken]] |
| 80 | + """ |
| 81 | + |
| 82 | + kwargs = _get_kwargs( |
| 83 | + room_id=room_id, |
| 84 | + ) |
| 85 | + |
| 86 | + response = client.get_httpx_client().request( |
| 87 | + **kwargs, |
| 88 | + ) |
| 89 | + |
| 90 | + return _build_response(client=client, response=response) |
| 91 | + |
| 92 | + |
| 93 | +def sync( |
| 94 | + room_id: str, |
| 95 | + *, |
| 96 | + client: Union[AuthenticatedClient, Client], |
| 97 | +) -> Optional[Union[Error, StreamerToken]]: |
| 98 | + """Generate a token that can be used by a streamer to start streaming |
| 99 | +
|
| 100 | + Args: |
| 101 | + room_id (str): |
| 102 | +
|
| 103 | + Raises: |
| 104 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 105 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + Union[Error, StreamerToken] |
| 109 | + """ |
| 110 | + |
| 111 | + return sync_detailed( |
| 112 | + room_id=room_id, |
| 113 | + client=client, |
| 114 | + ).parsed |
| 115 | + |
| 116 | + |
| 117 | +async def asyncio_detailed( |
| 118 | + room_id: str, |
| 119 | + *, |
| 120 | + client: Union[AuthenticatedClient, Client], |
| 121 | +) -> Response[Union[Error, StreamerToken]]: |
| 122 | + """Generate a token that can be used by a streamer to start streaming |
| 123 | +
|
| 124 | + Args: |
| 125 | + room_id (str): |
| 126 | +
|
| 127 | + Raises: |
| 128 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 129 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 130 | +
|
| 131 | + Returns: |
| 132 | + Response[Union[Error, StreamerToken]] |
| 133 | + """ |
| 134 | + |
| 135 | + kwargs = _get_kwargs( |
| 136 | + room_id=room_id, |
| 137 | + ) |
| 138 | + |
| 139 | + response = await client.get_async_httpx_client().request(**kwargs) |
| 140 | + |
| 141 | + return _build_response(client=client, response=response) |
| 142 | + |
| 143 | + |
| 144 | +async def asyncio( |
| 145 | + room_id: str, |
| 146 | + *, |
| 147 | + client: Union[AuthenticatedClient, Client], |
| 148 | +) -> Optional[Union[Error, StreamerToken]]: |
| 149 | + """Generate a token that can be used by a streamer to start streaming |
| 150 | +
|
| 151 | + Args: |
| 152 | + room_id (str): |
| 153 | +
|
| 154 | + Raises: |
| 155 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 156 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 157 | +
|
| 158 | + Returns: |
| 159 | + Union[Error, StreamerToken] |
| 160 | + """ |
| 161 | + |
| 162 | + return ( |
| 163 | + await asyncio_detailed( |
| 164 | + room_id=room_id, |
| 165 | + client=client, |
| 166 | + ) |
| 167 | + ).parsed |
0 commit comments