Skip to content
Open
Changes from all 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
40 changes: 27 additions & 13 deletions openfoodfacts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ def __init__(self, api_config: APIConfig):
environment=api_config.environment,
country_code=self.api_config.country.name,
)
@property
Comment thread
arybhatt4533 marked this conversation as resolved.
def product_update_url(self) -> str:
return f"{self.base_url}/cgi/product_jqm2.pl"
Comment on lines +196 to +198
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.

If this always returns a string… why not just set it as a string in __init__() rather than recalculating it every time?


def get_url(self, code: str, fields: Optional[List[str]] = None) -> str:
"""Return the URL used to fetch a product without making a request.

:param code: barcode of the product
:param fields: a list of fields to return. If None, all fields are
returned.
:return: the URL to fetch the product
"""
if len(code) == 0:
raise ValueError("code must be a non-empty string")

fields = fields or []
url = f"{self.base_url}/api/{self.api_config.version.value}/product/{code}"

if fields:
# requests escape comma in URLs, as expected, but openfoodfacts
# server does not recognize escaped commas.
# See
# https://github.com/openfoodfacts/openfoodfacts-server/issues/1607
url += "?fields={}".format(",".join(fields))
return url

def get(
self,
Expand All @@ -211,18 +236,7 @@ def get(
barcode is invalid, defaults to False.
:return: the API response
"""
if len(code) == 0:
raise ValueError("code must be a non-empty string")

fields = fields or []
url = f"{self.base_url}/api/{self.api_config.version.value}/product/{code}"

if fields:
# requests escape comma in URLs, as expected, but openfoodfacts
# server does not recognize escaped commas.
# See
# https://github.com/openfoodfacts/openfoodfacts-server/issues/1607
url += "?fields={}".format(",".join(fields))
url = self.get_url(code, fields)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you don’t need a separate function. can the logic go directly inside the existing get() method? 🤔

Copy link
Copy Markdown
Author

@arybhatt4533 arybhatt4533 Dec 20, 2025

Choose a reason for hiding this comment

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

The URL construction was extracted into get_url() so it can be reused and inspected independently of get().
This allows users to log or debug the exact endpoint without triggering a request, which aligns with the original goal of exposing the URL.
If you prefer keeping the logic inline for simplicity, I can move it back into get().
@areebahmeddd


resp = send_get_request(
url=url, api_config=self.api_config, return_none_on_404=True
Expand Down Expand Up @@ -279,7 +293,7 @@ def update(self, body: Dict[str, Any]):
if not body.get("code"):
raise ValueError("missing code from body")

url = f"{self.base_url}/cgi/product_jqm2.pl"
url = self.product_update_url
return send_form_urlencoded_post_request(url, body, self.api_config)

def select_image(
Expand Down