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