Skip to content

Commit

Permalink
Issue #677 introduce limit argument in Connection.list_jobs()
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Dec 6, 2024
1 parent 179a852 commit 76bac3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Automatically use `load_url` when providing a URL as geometries to `DataCube.aggregate_spatial()`, `DataCube.mask_polygon()`, etc. ([#104](https://github.com/Open-EO/openeo-python-client/issues/104), [#457](https://github.com/Open-EO/openeo-python-client/issues/457))
- Allow specifying `limit` when listing batch jobs with `Connection.list_jobs()` ([#677](https://github.com/Open-EO/openeo-python-client/issues/677))

### Changed

Expand Down
38 changes: 31 additions & 7 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def request(
method: str,
path: str,
*,
params: Optional[dict] = None,
headers: Optional[dict] = None,
auth: Optional[AuthBase] = None,
check_error: bool = True,
Expand All @@ -159,13 +160,21 @@ def request(
auth = auth or (self.auth if not self._is_external(url) else None)
slow_response_threshold = kwargs.pop("slow_response_threshold", self.slow_response_threshold)
if _log.isEnabledFor(logging.DEBUG):
_log.debug("Request `{m} {u}` with headers {h}, auth {a}, kwargs {k}".format(
m=method.upper(), u=url, h=headers and headers.keys(), a=type(auth).__name__, k=list(kwargs.keys()))
_log.debug(
"Request `{m} {u}` with params {p}, headers {h}, auth {a}, kwargs {k}".format(
m=method.upper(),
u=url,
p=params,
h=headers and headers.keys(),
a=type(auth).__name__,
k=list(kwargs.keys()),
)
)
with ContextTimer() as timer:
resp = self.session.request(
method=method,
url=url,
params=params,
headers=self._merged_headers(headers),
auth=auth,
timeout=kwargs.pop("timeout", self.default_timeout),
Expand Down Expand Up @@ -227,16 +236,25 @@ def _raise_api_error(self, response: requests.Response):

raise OpenEoApiPlainError(message=text, http_status_code=status_code, error_message=error_message)

def get(self, path: str, stream: bool = False, auth: Optional[AuthBase] = None, **kwargs) -> Response:
def get(
self,
path: str,
*,
params: Optional[dict] = None,
stream: bool = False,
auth: Optional[AuthBase] = None,
**kwargs,
) -> Response:
"""
Do GET request to REST API.
:param path: API path (without root url)
:param params: Additional query parameters
:param stream: True if the get request should be streamed, else False
:param auth: optional custom authentication to use instead of the default one
:return: response: Response
"""
return self.request("get", path=path, stream=stream, auth=auth, **kwargs)
return self.request("get", path=path, params=params, stream=stream, auth=auth, **kwargs)

def post(self, path: str, json: Optional[dict] = None, **kwargs) -> Response:
"""
Expand Down Expand Up @@ -1047,18 +1065,24 @@ def describe_process(self, id: str, namespace: Optional[str] = None) -> dict:

raise OpenEoClientException("Process does not exist.")

def list_jobs(self) -> List[dict]:
def list_jobs(self, limit: Union[int, None] = None) -> List[dict]:
"""
Lists all jobs of the authenticated user.
:param limit: maximum number of jobs to return. Setting this limit enables pagination.
:return: job_list: Dict of all jobs of the user.
.. versionadded:: 0.36.0
Added ``limit`` argument
"""
# TODO: Parse the result so that there get Job classes returned?
resp = self.get('/jobs', expected_status=200).json()
# TODO: Parse the result so that Job classes returned?
resp = self.get("/jobs", params={"limit": limit}, expected_status=200).json()
if resp.get("federation:missing"):
_log.warning("Partial user job listing due to missing federation components: {c}".format(
c=",".join(resp["federation:missing"])
))
# TODO: when pagination is enabled: how to expose link to next page?
jobs = resp["jobs"]
return VisualList("data-table", data=jobs, parameters={'columns': 'jobs'})

Expand Down
12 changes: 10 additions & 2 deletions tests/rest/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,14 @@ def download_tiff(request, context):
assert f.read() == TIFF_CONTENT


def test_list_jobs(con100, requests_mock):
@pytest.mark.parametrize(
["list_jobs_kwargs", "expected_qs"],
[
({}, {}),
({"limit": 123}, {"limit": ["123"]}),
],
)
def test_list_jobs(con100, requests_mock, list_jobs_kwargs, expected_qs):
username = "john"
password = "j0hn!"
access_token = "6cc35!"
Expand All @@ -700,6 +707,7 @@ def test_list_jobs(con100, requests_mock):

def get_jobs(request, context):
assert request.headers["Authorization"] == f"Bearer basic//{access_token}"
assert request.qs == expected_qs
return {
"jobs": [
{
Expand All @@ -718,7 +726,7 @@ def get_jobs(request, context):
requests_mock.get(API_URL + "/jobs", json=get_jobs)

con100.authenticate_basic(username, password)
jobs = con100.list_jobs()
jobs = con100.list_jobs(**list_jobs_kwargs)
assert jobs == [
{"id": "job123", "status": "running", "created": "2021-02-22T09:00:00Z"},
{"id": "job456", "status": "created", "created": "2021-03-22T10:00:00Z"},
Expand Down

0 comments on commit 76bac3b

Please sign in to comment.