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

Make cursor based pagination return relative uri's when fetching next and previous pages #726

Merged
merged 1 commit into from May 8, 2020

Conversation

forsbergplustwo
Copy link
Contributor

Fixes #713

Currently, when using REST based collection pagination, the request made using .fetch_next_page and fetch_previous_page actually includes the base site twice.

Normal request is fine:

prods = ShopifyAPI::Product.find(:all, params: {limit: 5})
GET https://myshop.myshopify.com:443/admin/api/2020-01/products.json?limit=5

but then fetching next or previous page results in the following request:

=> GET https://myshop.myshopify.com:443https://myshop.myshopify.com/admin/api/2020-01/products.json?limit=5&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6MjA3MDE5MjE2MDgzMywibGFzdF92YWx1ZSI6IlByaW1lIHRpbWUifQ

Strangely, Shopify works when calling the api like that most of the time.. but sometimes it randomly fails with a "522 - Bad Gateway" error.

This happens because the absolute url is passed from the pagination link headers, through to ActiveResource, which then merges the 'site' into the url again internally before making the request.

This pull request ensures we return a relative path (request_uri) instead of the absolute path when parsing the pagination link headers.

This ensures correctly formatted requests each time.

prods = ShopifyAPI::Product.find(:all, params: {limit: 5})

=> GET https://myshop.myshopify.com:443/admin/api/2020-01/products.json?limit=5

prods.fetch_next_page

=> GET https://myshop.myshopify.com/admin/api/2020-01/products.json?limit=5&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6MjA3MDE5MjE2MDgzMywibGFzdF92YWx1ZSI6IlByaW1lIHRpbWUifQ

@forsbergplustwo forsbergplustwo changed the title Make cursor based pagination return relative uri's when fetching next and previous pages- Fixes #713 Make cursor based pagination return relative uri's when fetching next and previous pages May 7, 2020
…gination

Fixes Shopify#713 - Use relative_uri when parsing link headers for pagination

Update version and changelog
@forsbergplustwo forsbergplustwo marked this pull request as ready for review May 7, 2020 18:12
@forsbergplustwo forsbergplustwo requested a review from a team as a code owner May 7, 2020 18:12
Copy link
Contributor

@tanema tanema left a comment

Choose a reason for hiding this comment

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

Thanks for the fix.

@tanema tanema merged commit af01894 into Shopify:master May 8, 2020
@paulomarg paulomarg temporarily deployed to rubygems July 14, 2020 18:37 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cursor based pagination - URI::InvalidComponentError (bad component(expected absolute path component)
3 participants