"""Create a new user"""
from http import HTTPStatus
from typing import Any
from typing import Dict
from typing import Union
import httpx
from ... import errors
from ...client import AuthenticatedClient
from ...client import Client
from ...models.new_user import NewUser
from ...models.server_error import ServerError
from ...models.user import User
from ...types import Response
def _get_kwargs(
*,
body: NewUser,
) -> Dict[str, Any]:
headers: Dict[str, Any] = {}
_kwargs: Dict[str, Any] = {
"method": "post",
"url": "/user",
}
_body = body.to_dict()
_kwargs["json"] = _body
headers["Content-Type"] = "application/json"
_kwargs["headers"] = headers
return _kwargs
def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Union[ServerError, User]:
if response.status_code == HTTPStatus.CREATED:
response_201 = User.from_dict(response.json())
return response_201
if response.status_code == HTTPStatus.BAD_REQUEST:
response_400 = ServerError.from_dict(response.json())
return response_400
if response.status_code == HTTPStatus.CONFLICT:
response_409 = ServerError.from_dict(response.json())
return response_409
if response.status_code == HTTPStatus.UNSUPPORTED_MEDIA_TYPE:
response_415 = ServerError.from_dict(response.json())
return response_415
raise errors.UnexpectedStatus(response.status_code, response.content)
def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[Union[ServerError, User]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)
[docs]
def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: NewUser,
) -> Response[Union[ServerError, User]]:
"""Create a new user
Args:
body (NewUser):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[Union[ServerError, User]]
"""
kwargs = _get_kwargs(
body=body,
)
response = client.get_httpx_client().request(
**kwargs,
)
return _build_response(client=client, response=response)
[docs]
def sync(
*,
client: Union[AuthenticatedClient, Client],
body: NewUser,
) -> Union[User]:
"""Create a new user
Args:
body (NewUser):
Raises:
errors.UnexpectedStatus: If the server returns a status code greater than or equal to 300.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Union[User]
"""
response = sync_detailed(
client=client,
body=body,
)
if isinstance(response.parsed, ServerError):
raise errors.UnexpectedStatus(response.status_code, response.content)
return response.parsed
[docs]
async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: NewUser,
) -> Response[Union[ServerError, User]]:
"""Create a new user
Args:
body (NewUser):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[Union[ServerError, User]]
"""
kwargs = _get_kwargs(
body=body,
)
response = await client.get_async_httpx_client().request(**kwargs)
return _build_response(client=client, response=response)
[docs]
async def asyncio(
*,
client: Union[AuthenticatedClient, Client],
body: NewUser,
) -> Union[User]:
"""Create a new user
Args:
body (NewUser):
Raises:
errors.UnexpectedStatus: If the server returns a status code greater than or equal to 300.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Union[User]
"""
response = await asyncio_detailed(
client=client,
body=body,
)
if isinstance(response.parsed, ServerError):
raise errors.UnexpectedStatus(response.status_code, response.content)
return response.parsed