Source code for maptekblastlogic.api.loaded_decks.create_loaded_deck

from http import HTTPStatus
from typing import Any, cast
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.error_model import ErrorModel
from ...models.loaded_deck_model import LoadedDeckModel
from ...types import Response


def _get_kwargs(
    site_code: str,
    *,
    body: LoadedDeckModel,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/site/{site_code}/loadedDecks".format(
            site_code=quote(str(site_code), safe=""),
        ),
    }

    _kwargs["json"] = body.to_dict()

    headers["Content-Type"] = "application/json"

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> ErrorModel | int | None:
    if response.status_code == 201:
        response_201 = cast(int, response.json())
        return response_201

    if response.status_code == 400:
        response_400 = ErrorModel.from_dict(response.json())

        return response_400

    if response.status_code == 401:
        response_401 = ErrorModel.from_dict(response.json())

        return response_401

    if response.status_code == 403:
        response_403 = ErrorModel.from_dict(response.json())

        return response_403

    if response.status_code == 404:
        response_404 = ErrorModel.from_dict(response.json())

        return response_404

    if response.status_code == 503:
        response_503 = ErrorModel.from_dict(response.json())

        return response_503

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[ErrorModel | int]:
    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( site_code: str, *, client: AuthenticatedClient, body: LoadedDeckModel, ) -> Response[ErrorModel | int]: """Create a new loaded deck. Requires permissions: Edit Charging Events Args: site_code (str): body (LoadedDeckModel): Describes the loading of an individual deck. This is raw data collected from the field that has not been validated and reconciled in BlastLogic Desktop. As such, decks may overlap with each other. This data should not be relied upon as the current state of the hole. 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[ErrorModel | int] """ kwargs = _get_kwargs( site_code=site_code, body=body, ) response = client.get_httpx_client().request( **kwargs, ) return _build_response(client=client, response=response)
[docs] def sync( site_code: str, *, client: AuthenticatedClient, body: LoadedDeckModel, ) -> ErrorModel | int | None: """Create a new loaded deck. Requires permissions: Edit Charging Events Args: site_code (str): body (LoadedDeckModel): Describes the loading of an individual deck. This is raw data collected from the field that has not been validated and reconciled in BlastLogic Desktop. As such, decks may overlap with each other. This data should not be relied upon as the current state of the hole. 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: ErrorModel | int """ return sync_detailed( site_code=site_code, client=client, body=body, ).parsed
[docs] async def asyncio_detailed( site_code: str, *, client: AuthenticatedClient, body: LoadedDeckModel, ) -> Response[ErrorModel | int]: """Create a new loaded deck. Requires permissions: Edit Charging Events Args: site_code (str): body (LoadedDeckModel): Describes the loading of an individual deck. This is raw data collected from the field that has not been validated and reconciled in BlastLogic Desktop. As such, decks may overlap with each other. This data should not be relied upon as the current state of the hole. 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[ErrorModel | int] """ kwargs = _get_kwargs( site_code=site_code, body=body, ) response = await client.get_async_httpx_client().request(**kwargs) return _build_response(client=client, response=response)
[docs] async def asyncio( site_code: str, *, client: AuthenticatedClient, body: LoadedDeckModel, ) -> ErrorModel | int | None: """Create a new loaded deck. Requires permissions: Edit Charging Events Args: site_code (str): body (LoadedDeckModel): Describes the loading of an individual deck. This is raw data collected from the field that has not been validated and reconciled in BlastLogic Desktop. As such, decks may overlap with each other. This data should not be relied upon as the current state of the hole. 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: ErrorModel | int """ return ( await asyncio_detailed( site_code=site_code, client=client, body=body, ) ).parsed