Source code for aymara_ai.resources.files

# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import Mapping, Iterable, Optional, cast

import httpx

from ..types import file_create_params, file_upload_params
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven, FileTypes
from .._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from .._base_client import make_request_options
from ..types.file_upload import FileUpload
from ..types.file_create_response import FileCreateResponse

__all__ = ["FilesResource", "AsyncFilesResource"]


[docs] class FilesResource(SyncAPIResource): @cached_property def with_raw_response(self) -> FilesResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/aymara-ai/aymara-sdk-python#accessing-raw-response-data-eg-headers """ return FilesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> FilesResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/aymara-ai/aymara-sdk-python#with_streaming_response """ return FilesResourceWithStreamingResponse(self)
[docs] def create( self, *, files: Iterable[file_create_params.File], workspace_uuid: Optional[str] | NotGiven = NOT_GIVEN, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, ) -> FileCreateResponse: """ Requests to upload one or more files to be used in an eval run. Args: upload_request (FileUploadRequest): Contains the files to upload and the workspace UUID. Returns: FileUploadResponse: Contains presigned URLs and metadata to upload each file. Raises: AymaraAPIError: If the organization is missing or file upload fails. Example: POST /api/files { "workspace_uuid": "...", "files": [ {"local_file_path": "path/to/file1.csv"}, {"local_file_path": "path/to/file2.csv"} ] } Args: files: List of files to upload. workspace_uuid: UUID of the workspace to associate with the upload, if any. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return self._post( "/v2/files", body=maybe_transform( { "files": files, "workspace_uuid": workspace_uuid, }, file_create_params.FileCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileCreateResponse, )
[docs] def upload( self, *, file: FileTypes, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, ) -> FileUpload: """ Receives a file and streams it to S3 using multipart upload in a single request. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ body = deepcopy_minimal({"file": file}) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be # sent to the server will contain a `boundary` parameter, e.g. # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return self._post( "/v2/files/-/uploads", body=maybe_transform(body, file_upload_params.FileUploadParams), files=files, options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileUpload, )
[docs] class AsyncFilesResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncFilesResourceWithRawResponse: """ This property can be used as a prefix for any HTTP method call to return the raw response object instead of the parsed content. For more information, see https://www.github.com/aymara-ai/aymara-sdk-python#accessing-raw-response-data-eg-headers """ return AsyncFilesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncFilesResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/aymara-ai/aymara-sdk-python#with_streaming_response """ return AsyncFilesResourceWithStreamingResponse(self)
[docs] async def create( self, *, files: Iterable[file_create_params.File], workspace_uuid: Optional[str] | NotGiven = NOT_GIVEN, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, ) -> FileCreateResponse: """ Requests to upload one or more files to be used in an eval run. Args: upload_request (FileUploadRequest): Contains the files to upload and the workspace UUID. Returns: FileUploadResponse: Contains presigned URLs and metadata to upload each file. Raises: AymaraAPIError: If the organization is missing or file upload fails. Example: POST /api/files { "workspace_uuid": "...", "files": [ {"local_file_path": "path/to/file1.csv"}, {"local_file_path": "path/to/file2.csv"} ] } Args: files: List of files to upload. workspace_uuid: UUID of the workspace to associate with the upload, if any. extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ return await self._post( "/v2/files", body=await async_maybe_transform( { "files": files, "workspace_uuid": workspace_uuid, }, file_create_params.FileCreateParams, ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileCreateResponse, )
[docs] async def upload( self, *, file: FileTypes, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, extra_query: Query | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, ) -> FileUpload: """ Receives a file and streams it to S3 using multipart upload in a single request. Args: extra_headers: Send extra headers extra_query: Add additional query parameters to the request extra_body: Add additional JSON properties to the request timeout: Override the client-level default timeout for this request, in seconds """ body = deepcopy_minimal({"file": file}) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be # sent to the server will contain a `boundary` parameter, e.g. # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return await self._post( "/v2/files/-/uploads", body=await async_maybe_transform(body, file_upload_params.FileUploadParams), files=files, options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileUpload, )
class FilesResourceWithRawResponse: def __init__(self, files: FilesResource) -> None: self._files = files self.create = to_raw_response_wrapper( files.create, ) self.upload = to_raw_response_wrapper( files.upload, ) class AsyncFilesResourceWithRawResponse: def __init__(self, files: AsyncFilesResource) -> None: self._files = files self.create = async_to_raw_response_wrapper( files.create, ) self.upload = async_to_raw_response_wrapper( files.upload, ) class FilesResourceWithStreamingResponse: def __init__(self, files: FilesResource) -> None: self._files = files self.create = to_streamed_response_wrapper( files.create, ) self.upload = to_streamed_response_wrapper( files.upload, ) class AsyncFilesResourceWithStreamingResponse: def __init__(self, files: AsyncFilesResource) -> None: self._files = files self.create = async_to_streamed_response_wrapper( files.create, ) self.upload = async_to_streamed_response_wrapper( files.upload, )