TODO: Content for this topic coming soon.
1 Like
Will be super helpful! Thanks as always
CC Community - See below code chunk that builds upon @maikel.ruiz and team’s work using Python client package:
resultsList = []
def process_page(response_json):
results = response_json["results"]
resultsList.extend(results)
has_more = True
while has_more:
response = requests.request("GET", urltwo, headers=auth_headers)
urltwo = get_next_page_url(response.headers)
process_page(response.json())
has_more = urltwo is not None```
1 Like
Building a bit more upon that, I would put that into a helper method that allows me to separate the logic for fetching and processing, and can be reused for other URLs. Something like this:
import requests
from combocurve_api_v1.pagination import get_next_page_url
# Helper method that returns an iterator with the documents from all pages
def get_all(url, headers):
# First request
has_more = True
# Keep fetching while there are more records to be returned
while has_more:
response = requests.get(url, headers=headers)
# Return the records to be processed by the caller instead of doing the processing here
yield from response.json()
url = get_next_page_url(response.headers)
has_more = url is not None
Afterwards it can be used like this:
url = 'https://api.combocurve.com/v1/wells?take=200'
auth_headers = combocurve_auth.get_auth_headers()
for record in get_all(url, auth_headers):
# Process each record (each well in this case) here
print(record)
2 Likes
Oh that even better!