Source code for britecore_sdk.api.api_calls.v2.uploads

"""BriteCore v2 Uploads API endpoint wrappers.

This module provides the SDK wrapper for associating uploaded files with policy
records through the BriteCore v2 uploads API.
"""

from logging import Logger
from typing import Any, Unpack, cast

from urllib3 import BaseHTTPResponse, HTTPResponse

from britecore_sdk import logger
from britecore_sdk.api.api_calls import (
    BritecoreAPIClient,
    RequestParameters,
    api_client,
)

LOGGER: Logger = logger

API_CLIENT: BritecoreAPIClient = api_client


def build_payload(**fields: Any) -> dict[str, Any]:
    """Build a JSON payload, omitting keys whose value is ``None``."""
    return {key: value for key, value in fields.items() if value is not None}


def post(
    path: str,
    payload: dict[str, Any] | None = None,
    **kwargs: Unpack[RequestParameters],
) -> Any:
    """Send an uploads request and normalize the response."""
    LOGGER.debug("Calling uploads endpoint %s", path)
    request_result: BaseHTTPResponse | HTTPResponse | None = API_CLIENT.do_request(
        path=path,
        json=payload if payload is not None else {},
        **kwargs,
    )
    return API_CLIENT.process_result(cast(Any, request_result))


[docs] def attach_file_to_policy( payload: dict | None = None, **kwargs: Unpack[RequestParameters], ) -> Any: """Attach an uploaded file to a policy record. POST /api/v2/uploads/attach_file_to_policy """ return post( "/api/v2/uploads/attach_file_to_policy", build_payload(payload=payload), **kwargs, )
__all__ = [ "attach_file_to_policy", ] # --- Autogenerated spec wrappers ---
[docs] def attach_file( entity_id: str | None = None, set_folder_private_if_new: bool | None = None, title: str | None = None, entity_type: str | None = None, set_file_private: bool | None = None, attachment: str | None = None, date_added: str | None = None, mime_type: str | None = None, folder_name: str | None = None, **kwargs: Unpack[RequestParameters], ) -> Any: """Attach File. POST /api/v2/uploads/attach_file """ request_json: dict[str, Any] = { "entity_id": entity_id, "set_folder_private_if_new": set_folder_private_if_new, "title": title, "entity_type": entity_type, "set_file_private": set_file_private, "attachment": attachment, "date_added": date_added, "mime_type": mime_type, "folder_name": folder_name, } filtered_json = {k: v for k, v in request_json.items() if v is not None} request_result = API_CLIENT.do_request( path="/api/v2/uploads/attach_file", json=filtered_json, method="POST", **kwargs, ) return API_CLIENT.process_result( request_result, endpoint="/api/v2/uploads/attach_file" )
__all__.extend( [ "attach_file", ] )