Skip to content

Commit

Permalink
fix: create new /images/logos/search endpoint from /images/logos
Browse files Browse the repository at this point in the history
The endpoint performed two different tasks depending on whether
logo_ids parameter was provided, change this
  • Loading branch information
raphael0202 committed Oct 13, 2022
1 parent e3d84ae commit 20034d8
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions robotoff/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,16 +622,25 @@ def image_response(image: Image.Image, resp: falcon.Response) -> None:

class ImageLogoResource:
def on_get(self, req: falcon.Request, resp: falcon.Response):
logo_ids: Optional[List[str]] = req.get_param_as_list(
"logo_ids", required=False
)
logo_ids: List[str] = req.get_param_as_list("logo_ids", required=True)
logos = []
for logo in (
LogoAnnotation.select()
.join(ImagePrediction)
.join(ImageModel)
.where(LogoAnnotation.id.in_(logo_ids))
.iterator()
):
logo_dict = logo.to_dict()
image_prediction = logo_dict.pop("image_prediction")
logo_dict["image"] = image_prediction["image"]
logos.append(logo_dict)

resp.media = {"logos": logos, "count": len(logos)}

if logo_ids is not None:
self.fetch_logos(logo_ids, resp)
else:
self.search(req, resp)

def search(self, req: falcon.Request, resp: falcon.Response):
class ImageLogoSearchResource:
def on_get(self, req: falcon.Request, resp: falcon.Response):
count: int = req.get_param_as_int(
"count", min_value=1, max_value=2000, default=25
)
Expand Down Expand Up @@ -694,22 +703,6 @@ def search(self, req: falcon.Request, resp: falcon.Response):

resp.media = {"logos": items, "count": query_count}

def fetch_logos(self, logo_ids: List[str], resp: falcon.Response):
logos = []
for logo in (
LogoAnnotation.select()
.join(ImagePrediction)
.join(ImageModel)
.where(LogoAnnotation.id.in_(logo_ids))
.iterator()
):
logo_dict = logo.to_dict()
image_prediction = logo_dict.pop("image_prediction")
logo_dict["image"] = image_prediction["image"]
logos.append(logo_dict)

resp.media = {"logos": logos, "count": len(logos)}


class ImageLogoDetailResource:
def on_get(self, req: falcon.Request, resp: falcon.Response, logo_id: int):
Expand Down Expand Up @@ -1319,6 +1312,7 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
api.add_route("/api/v1/images/predictions", ImagePredictionFetchResource())
api.add_route("/api/v1/images/predict", ImagePredictorResource())
api.add_route("/api/v1/images/logos", ImageLogoResource())
api.add_route("/api/v1/images/logos/search", ImageLogoSearchResource())
api.add_route("/api/v1/images/logos/{logo_id:int}", ImageLogoDetailResource())
api.add_route("/api/v1/images/logos/annotate", ImageLogoAnnotateResource())
api.add_route("/api/v1/images/logos/update", ImageLogoUpdateResource())
Expand Down

0 comments on commit 20034d8

Please sign in to comment.