Skip to content

feat: add get_url method and product_update_url property #391#410

Open
arybhatt4533 wants to merge 2 commits intoopenfoodfacts:developfrom
arybhatt4533:feature/expose-api-url
Open

feat: add get_url method and product_update_url property #391#410
arybhatt4533 wants to merge 2 commits intoopenfoodfacts:developfrom
arybhatt4533:feature/expose-api-url

Conversation

@arybhatt4533
Copy link
Copy Markdown

Description

Added get_url method to ProductResource and exposed product_update_url.

Solution

The goal of this PR is to allow users to inspect the API URLs without actually triggering a network request. Here’s how I handled it:

Extracted URL Logic: I created a new get_url method. Previously, the URL construction was hardcoded inside the get method. Now, it lives in its own reusable function.

Refactored get method: I updated the existing get method to call get_url internally. This removes code duplication and keeps the logic consistent.
image

Exposed Update URL: I added a product_update_url property. This gives users a direct way to see the endpoint used for product updates (/cgi/product_jqm2.pl).

Improved Flexibility: Users can now pass a list of fields to get_url to see exactly what the final query string will look like before fetching data.
image

Related issue(s)

  • Fixes #[391]

@sonarqubecloud
Copy link
Copy Markdown

Comment thread openfoodfacts/api.py
# 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

Copy link
Copy Markdown
Contributor

@Freso Freso left a comment

Choose a reason for hiding this comment

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

Seems alright to me at a glance, just a couple of minor questions/suggestions/comments.

Comment thread openfoodfacts/api.py
Comment on lines +196 to +198
@property
def product_update_url(self) -> str:
return f"{self.base_url}/cgi/product_jqm2.pl"
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?

Comment thread openfoodfacts/api.py
Comment on lines 282 to 297
params["sort_by"] = sort_by

return send_get_request(
url=f"{self.base_url}/cgi/search.pl",
api_config=self.api_config,
params=params,
auth=get_http_auth(self.api_config.environment),
)

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

url = f"{self.base_url}/cgi/product_jqm2.pl"
return send_form_urlencoded_post_request(url, body, self.api_config)
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.

Suggested change
return send_form_urlencoded_post_request(self.product_update_url, body, self.api_config)

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.

Idk what’s up with GH, but essentially, let’s use this property here now that we have it.

Comment thread openfoodfacts/api.py
@arybhatt4533 arybhatt4533 force-pushed the feature/expose-api-url branch from e325b10 to 5b3a680 Compare February 22, 2026 02:56
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Is it possible to get the url to be called before posting data

3 participants