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

added pagination to listJobs endpoint #96

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions maap/maap.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,24 @@ def cancelJob(self, jobid):
job.id = jobid
return job.cancel_job()

def listJobs(self, username=None):
def listJobs(self, username=None, page_size=None, offset=None):
if username==None and self.profile is not None and 'username' in self.profile.account_info().keys():
username = self.profile.account_info()['username']
url = os.path.join(self.config.dps_job, username, endpoints.DPS_JOB_LIST)

query_params = []
if page_size is not None:
query_params.append(f"page_size={page_size}")
if offset is not None:
query_params.append(f"offset={offset}")

query_string = "&".join(query_params)

if query_string:
endpoint = endpoints.DPS_JOB_LIST + "?" + query_string
url = os.path.join(self.config.dps_job, username, endpoint)
else:
url = os.path.join(self.config.dps_job, username, endpoints.DPS_JOB_LIST)

Copy link
Contributor

Choose a reason for hiding this comment

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

You should not be manually adding query params to a URL. The requests library properly deals with them, including doing proper encoding. Simply construct a params dict and pass it as an additional named argument to requests.get:

Suggested change
query_params = []
if page_size is not None:
query_params.append(f"page_size={page_size}")
if offset is not None:
query_params.append(f"offset={offset}")
query_string = "&".join(query_params)
if query_string:
endpoint = endpoints.DPS_JOB_LIST + "?" + query_string
url = os.path.join(self.config.dps_job, username, endpoint)
else:
url = os.path.join(self.config.dps_job, username, endpoints.DPS_JOB_LIST)
url = os.path.join(self.config.dps_job, username, endpoints.DPS_JOB_LIST)
params = {k: v for k, v in (("page_size", page_size), ("offset", offset)) if v}

And then, change the call to requests.get that follows, to this:

        response = requests.get(
            url=url,
            headers=headers,
            params=params,
        )

headers = self._get_api_header()
logger.debug('GET request sent to {}'.format(url))
logger.debug('headers:')
Expand Down