Skip to content
Open
Changes from 2 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
51 changes: 51 additions & 0 deletions openfoodfacts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ def __init__(self, api_config: APIConfig):
environment=api_config.environment,
country_code=self.api_config.country.name,
)
# Handle environment for Search-a-licious (Requested by review)
domain = "net" if self.api_config.environment == Environment.net else "org"
self.base_searchalicious_url = f"https://search.openfoodfacts.{domain}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should probably use URLBuilder here, by adding a search method in a similar way as what's done for Robotoff:

def robotoff(environment: Environment) -> str:
.


def get(
self,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems you indented by mistake these methods. Can you fix it?
You should run black after each edit, to make sure the formatting stays consistent.

Expand Down Expand Up @@ -274,6 +277,54 @@ def text_search(
auth=get_http_auth(self.api_config.environment),
)

def search(
self,
query: str,
page: int = 1,
page_size: int = 24,
sort_by: Optional[str] = None,
**kwargs: Any,
) -> Optional[JSONType]:
"""Search products using the new high-performance Search-a-licious endpoint.
Comment thread
saivats marked this conversation as resolved.

WARNING: This endpoint is currently in alpha version and subjected to
breaking changes.

:param query: the search query
:param page: requested page (starts at 1), defaults to 1
:param page_size: number of items per page, defaults to 24
:param sort_by: result sorting key, defaults to None
:param kwargs: additional filters
:return: the search results (standardized with 'products' list)
"""
# Use the dynamic URL based on environment
url = f"{self.base_searchalicious_url}/search"

params = {
"q": query,
"page": page,
"page_size": page_size,
}

if sort_by:
params["sort_by"] = sort_by

params.update(kwargs)

# Get the raw response
result = send_get_request(
url=url,
api_config=self.api_config,
params=params,
auth=get_http_auth(self.api_config.environment),
)

# Compatibility Fix: Rename 'hits' to 'products' to match existing SDK behavior
if result and "hits" in result:
result["products"] = result.pop("hits")

return result

def update(self, body: Dict[str, Any]):
"""Create a new product or update an existing one."""
if not body.get("code"):
Expand Down