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