feat: add get_url method and product_update_url property #391#410
feat: add get_url method and product_update_url property #391#410arybhatt4533 wants to merge 2 commits intoopenfoodfacts:developfrom
Conversation
|
| # See | ||
| # https://github.com/openfoodfacts/openfoodfacts-server/issues/1607 | ||
| url += "?fields={}".format(",".join(fields)) | ||
| url = self.get_url(code, fields) |
There was a problem hiding this comment.
you don’t need a separate function. can the logic go directly inside the existing get() method? 🤔
There was a problem hiding this comment.
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
Freso
left a comment
There was a problem hiding this comment.
Seems alright to me at a glance, just a couple of minor questions/suggestions/comments.
| @property | ||
| def product_update_url(self) -> str: | ||
| return f"{self.base_url}/cgi/product_jqm2.pl" |
There was a problem hiding this comment.
If this always returns a string… why not just set it as a string in __init__() rather than recalculating it every time?
| 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) |
There was a problem hiding this comment.
| return send_form_urlencoded_post_request(self.product_update_url, body, self.api_config) |
There was a problem hiding this comment.
Idk what’s up with GH, but essentially, let’s use this property here now that we have it.
e325b10 to
5b3a680
Compare
|







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.

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.

Related issue(s)