Skip to content
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

api.facet.get returns single page #292

Open
benbenben2 opened this issue Dec 6, 2024 · 0 comments
Open

api.facet.get returns single page #292

benbenben2 opened this issue Dec 6, 2024 · 0 comments

Comments

@benbenben2
Copy link
Contributor

Problem

>>> results = api.facet.get('country')
>>> print(results['count'])
575
>>> print(len(results['tags']))
100

Get a facet returns only a single page (equivalent of https://world.openfoodfacts.net/countries.json)

Proposed solution

something like that maybe?

  def get(self, facet_name: Union[Facet, str], full_load: bool = False, unknown: bool = False) -> JSONType:
      facet = Facet.from_str_or_enum(facet_name)
      facet_plural = facet.value.replace("_", "-")
      all_results = []
      page = 1
      page_size = 100

      params = {"json": "1"}
      if unknown:
          params["status"] = "unknown"

      while True:
          url = f"{self.base_url}/{facet_plural}"
          if page > 1:
              url = f"{url}/{page}"

          resp = send_get_request(
              url=url,
              params=params,
              api_config=self.api_config,
              auth=get_http_auth(self.api_config.environment),
          )
          resp = cast(JSONType, resp)
          results = resp.get("tags", [])

          all_results.extend(results)

          if not full_load or len(results) < page_size:
              break

          page += 1

      return {"tags": all_results}

It will probably face the request limitation after 5 or 10 pages.

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

No branches or pull requests

1 participant