Skip to content

Pagination support #89

Open
Open
@geri777

Description

@geri777

Currently this API implementation does not offer a pagination feature.
https://woocommerce.github.io/woocommerce-rest-api-docs/#pagination
Would it be possible to perform pagination inside your API class? - And recursively perform the requests until the last page is received?

Activity

changed the title [-]Pagination not supported[/-] [+]Pagination support[/+] on Mar 4, 2023
aapjeisbaas

aapjeisbaas commented on Mar 31, 2023

@aapjeisbaas

I just needed this and a wrote a little pagination inside a product fetch function:

def all_products(wcapi):
    r = wcapi.get("products")
    if r.status_code != 200:
        print("error fetching products")
        return

    # create list with first page results
    products = r.json()

    # pagination
    while 'next' in r.links and r.status_code == 200:
       r = wcapi.get(r.links['next']['url'].split("wp-json/wc/v3/")[1])
       products += r.json()

    if r.status_code > 200:
        print(f"Got an unexpected http response: {r.status_code}, returning what I can...")

    return products

Use it for inspiration maybe someone want to implement this into the package as a wrapper.

Explanation:
The requests object returned from wcapi.get has a links object which represents the Link return header and the named links that were in there.
The links object is an empty dict if there are no link headers defined.

>>> import requests
>>> r = requests.get("https://aapjeisbaas.nl)
>>> type(r.links)
<class 'dict'>
>>> r.links
{}
>>> 

sidenote: requests also has a r.next but it is not implemented to support this type of pagination.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Pagination support · Issue #89 · woocommerce/wc-api-python