-
-
Notifications
You must be signed in to change notification settings - Fork 103
feat: add get_url method and product_update_url property #391 #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,6 +193,31 @@ def __init__(self, api_config: APIConfig): | |
| environment=api_config.environment, | ||
| country_code=self.api_config.country.name, | ||
| ) | ||
| @property | ||
| def product_update_url(self) -> str: | ||
| return f"{self.base_url}/cgi/product_jqm2.pl" | ||
|
Comment on lines
+196
to
+198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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, | ||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 🤔
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(). |
||
|
|
||
| resp = send_get_request( | ||
| url=url, api_config=self.api_config, return_none_on_404=True | ||
|
|
@@ -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( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.