Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve resources from API routes ending in / #926

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion chromadb/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ def _uuid(uuid_str: str) -> UUID:
raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")


class APIRouter(fastapi.APIRouter):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - let's not stomp the fastapi name for the class

# A simple subclass of fastapi's APIRouter which treats URLs with a trailing "/" the
# same as URLs without. Docs will only contain URLs without trailing "/"s.
def add_api_route(self, path: str, *args: Any, **kwargs: Any) -> None:
# If kwargs["include_in_schema"] isn't passed OR is True, we should only
HammadB marked this conversation as resolved.
Show resolved Hide resolved
# include the non-"/" path. If kwargs["include_in_schema"] is False, include
# neither.
exclude_from_schema = (
"include_in_schema" in kwargs and not kwargs["include_in_schema"]
)

kwargs["include_in_schema"] = not exclude_from_schema and not path.endswith("/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extreme nit - duplicated logic (feel free to ignore this, just calling it out)

super().add_api_route(path, *args, **kwargs)

if path.endswith("/"):
path = path[:-1]
else:
path = path + "/"

kwargs["include_in_schema"] = not exclude_from_schema and not path.endswith("/")
super().add_api_route(path, *args, **kwargs)


class FastAPI(chromadb.server.Server):
def __init__(self, settings: Settings):
super().__init__(settings)
Expand All @@ -84,7 +107,7 @@ def __init__(self, settings: Settings):
allow_methods=["*"],
)

self.router = fastapi.APIRouter()
self.router = APIRouter()

self.router.add_api_route("/api/v1", self.root, methods=["GET"])
self.router.add_api_route("/api/v1/reset", self.reset, methods=["POST"])
Expand Down