Skip to content

Commit

Permalink
Changed annotation syntax: from 'field: Annotated[int, Query()]' to f…
Browse files Browse the repository at this point in the history
…ield: int = Query(), simpler and more consistent
  • Loading branch information
josvandervelde committed Nov 29, 2023
1 parent 293fbca commit 15368cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 68 deletions.
22 changes: 6 additions & 16 deletions src/routers/resource_ai_asset_router.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Annotated

import requests
from fastapi import APIRouter, HTTPException, status, Path
from fastapi.responses import Response
Expand Down Expand Up @@ -50,16 +48,10 @@ def get_resource_content_func(self, default: bool):
"""

def get_resource_content(
identifier: Annotated[
str, Path(description=f"The identifier of the {self.resource_name}") # type: ignore
],
distribution_idx: Annotated[
int,
Path(
description=f"The index of the distribution within the " f"{self.resource_name}"
), # type: ignore
],
default: bool = False,
identifier: str = Path(description=f"The identifier of the {self.resource_name}"),
distribution_idx: int = Path(
description=f"The index of the distribution within the {self.resource_name}"
),
):
metadata: AIAsset = self.get_resource(
identifier=identifier, schema="aiod", platform=None
Expand Down Expand Up @@ -106,11 +98,9 @@ def get_resource_content(
raise _wrap_as_http_exception(exc)

def get_resource_content_default(
identifier: Annotated[
str, Path(description=f"The identifier of the {self.resource_name}") # type: ignore
]
identifier: str = Path(description=f"The identifier of the {self.resource_name}"),
):
return get_resource_content(identifier=identifier, distribution_idx=0, default=True)
return get_resource_content(identifier=identifier, distribution_idx=0)

if default:
return get_resource_content_default
Expand Down
28 changes: 11 additions & 17 deletions src/routers/resource_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ def get_resource_count_func(self):
"""

def get_resource_count(
detailed: Annotated[
bool, Query(description="If true, a more detailed output is returned.")
] = False
detailed: bool = Query(
description="If true, a more detailed output is returned.", default=False
)
):
try:
with DbSession() as session:
Expand Down Expand Up @@ -308,13 +308,10 @@ def get_platform_resources_func(self):
"""

def get_resources(
platform: Annotated[
str,
Path(
description="Return resources of this platform",
example="huggingface",
),
],
platform: str = Path(
description="Return resources of this platform",
example="huggingface",
),
pagination: Pagination = Depends(Pagination),
schema: self._possible_schemas_type = "aiod", # type:ignore
):
Expand Down Expand Up @@ -348,13 +345,10 @@ def get_platform_resource_func(self):

def get_resource(
identifier: str,
platform: Annotated[
str,
Path(
description="Return resources of this platform",
example="huggingface",
),
],
platform: str = Path(
description="Return resources of this platform",
example="huggingface",
),
schema: self._possible_schemas_type = "aiod", # type:ignore
):
return self.get_resource(identifier=identifier, schema=schema, platform=platform)
Expand Down
61 changes: 27 additions & 34 deletions src/routers/search_router.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from typing import TypeVar, Generic, Any, Type, Annotated, Literal
from typing import TypeVar, Generic, Any, Type, Literal

from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
Expand Down Expand Up @@ -76,39 +76,32 @@ def create(self, url_prefix: str) -> APIRouter:
# response_model=SearchResult[read_class], # This gives errors, so not used.
)
def search(
search_query: Annotated[
str,
Query(
description="Text you wish to find. It is used in an ElasticSearch match "
"query.",
examples=["Name of the resource"],
),
],
search_fields: Annotated[ # type: ignore
list[indexed_fields] | None,
Query(
description="Search in these fields. If empty, the query will be matched "
"against all fields. Do not use the '--' option in Swagger, it is a Swagger "
"artifact.",
),
] = None,
platforms: Annotated[
list[str] | None,
Query(
description="Search for resources of these platforms. If empty, results from "
"all platforms will be returned.",
examples=["huggingface", "openml"],
),
] = None,
limit: Annotated[int | None, Query(ge=1, le=LIMIT_MAX)] = 10,
offset: Annotated[int | None, Query(ge=0)] = 0,
get_all: Annotated[
bool,
Query(
description="If true, a request to the database is made to retrieve all data. "
"If false, only the indexed information is returned."
),
] = False,
search_query: str = Query(
...,
description="Text you wish to find. It is used in an ElasticSearch match " "query.",
examples=["Name of the resource"],
),
search_fields: list[indexed_fields] # type: ignore
| None = Query(
description="Search in these fields. If empty, the query will be matched "
"against all fields. Do not use the '--' option in Swagger, it is a Swagger "
"artifact.",
default=None,
),
platforms: list[str]
| None = Query(
description="Search for resources of these platforms. If empty, results from "
"all platforms will be returned.",
examples=["huggingface", "openml"],
default=None,
),
limit: int | None = Query(ge=1, le=LIMIT_MAX, default=10),
offset: int | None = Query(ge=0, default=0),
get_all: bool = Query(
description="If true, a request to the database is made to retrieve all data. "
"If false, only the indexed information is returned.",
default=False,
),
):
try:
with DbSession() as session:
Expand Down
1 change: 0 additions & 1 deletion src/routers/upload_router_huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def create(self, url_prefix: str) -> APIRouter:
@router.post(url_prefix + "/upload/datasets/{identifier}/huggingface", tags=["upload"])
def huggingFaceUpload(
identifier: int = Path(
...,
description="The AIoD dataset identifier",
),
file: UploadFile = File(
Expand Down

0 comments on commit 15368cb

Please sign in to comment.