forked from phoenix-oss/llama-stack-mirror
Summary: Adds spec definitions for file uploads operations. This API focuses around two high level operations: * Initiating and managing upload session * Accessing uploaded file information Usage examples: To start a file upload session: ``` curl -X POST https://localhost:8321/v1/files \ -d '{ "key": "image123.jpg', "bucket": "images", "mime_type": "image/jpg", "size": 12345 }' # Returns { “id”: <session_id> “url”: “https://localhost:8321/v1/files/session:<session_id>”, "offset": 0, "size": 12345 } ``` To upload file content to an existing session ``` curl -i -X POST "https://localhost:8321/v1/files/session:<session_id> \ --data-binary @<path_to_local_file> # Returns { "key": "image123.jpg", "bucket": "images", "mime_type": "image/jpg", "bytes": 12345, "created_at": 1737492240 } # Implementing on server side (Flask example for simplicity): @app.route('/uploads/{upload_id}', methods=['POST']) def upload_content_to_session(upload_id): try: # Get the binary file data from the request body file_data = request.data # Save the file to disk save_path = f"./uploads/{upload_id}" with open(save_path, 'wb') as f: f.write(file_data) return {__uploaded_file_json__}, 200 except Exception as e: return 500 ``` To read information about an existing upload session ``` curl -i -X GET "https://localhost:8321/v1/files/session:<session_id> # Returns { “id”: <session_id> “url”: “https://localhost:8321/v1/files/session:<session_id>”, "offset": 1024, "size": 12345 } ``` To list buckets ``` GET /files # Returns { "data": [ {"name": "bucket1"}, {"name": "bucket2"}, ] } ``` To list all files in a bucket ``` GET /files/{bucket} # Returns { "data": [ { "key": "shiba.jpg", "bucket": "dogs", "mime_type": "image/jpg", "bytes": 82334, "created_at": 1737492240, }, { "key": "persian_cat.jpg", "mime_type": "image/jpg", "bucket": "cats", "bytes": 39924, "created_at": 1727493440, }, ] } ``` To get specific file info ``` GET /files/{bucket}/{key} { "key": "shiba.jpg", "bucket": "dogs", "mime_type": "image/jpg", "bytes": 82334, "created_at": 1737492240, } ``` To delete specific file ``` DELETE /files/{bucket}/{key} { "key": "shiba.jpg", "bucket": "dogs", "mime_type": "image/jpg", "bytes": 82334, "created_at": 1737492240, } ```
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, Callable, List, Optional, TypeVar
|
|
|
|
from .strong_typing.schema import json_schema_type, register_schema # noqa: F401
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
@dataclass
|
|
class WebMethod:
|
|
route: Optional[str] = None
|
|
public: bool = False
|
|
request_examples: Optional[List[Any]] = None
|
|
response_examples: Optional[List[Any]] = None
|
|
method: Optional[str] = None
|
|
raw_bytes_request_body: Optional[bool] = False
|
|
|
|
|
|
def webmethod(
|
|
route: Optional[str] = None,
|
|
method: Optional[str] = None,
|
|
public: Optional[bool] = False,
|
|
request_examples: Optional[List[Any]] = None,
|
|
response_examples: Optional[List[Any]] = None,
|
|
raw_bytes_request_body: Optional[bool] = False,
|
|
) -> Callable[[T], T]:
|
|
"""
|
|
Decorator that supplies additional metadata to an endpoint operation function.
|
|
|
|
:param route: The URL path pattern associated with this operation which path parameters are substituted into.
|
|
:param public: True if the operation can be invoked without prior authentication.
|
|
:param request_examples: Sample requests that the operation might take. Pass a list of objects, not JSON.
|
|
:param response_examples: Sample responses that the operation might produce. Pass a list of objects, not JSON.
|
|
"""
|
|
|
|
def wrap(cls: T) -> T:
|
|
cls.__webmethod__ = WebMethod(
|
|
route=route,
|
|
method=method,
|
|
public=public or False,
|
|
request_examples=request_examples,
|
|
response_examples=response_examples,
|
|
raw_bytes_request_body=raw_bytes_request_body,
|
|
)
|
|
return cls
|
|
|
|
return wrap
|