Skip to content

Commit

Permalink
HARMONY-1898: Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ygliuvt committed Feb 7, 2025
1 parent 7b2fff5 commit 956807b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The classes in the ``harmony`` package that are used for crafting a request, sub

.. autoclass:: harmony.CapabilitiesRequest

.. autoclass:: harmony.LabelsRequest
.. autoclass:: harmony.AddLabelsRequest

.. autoclass:: harmony.Client

Expand Down
7 changes: 3 additions & 4 deletions examples/labels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"# Install harmony-py requirements. Not necessary if you ran `pip install harmony-py` in your kernel\n",
"helper.install_project_and_dependencies('..')\n",
"\n",
"from harmony import BBox, Client, Collection, HttpMethod, Request, LabelsRequest, Environment"
"from harmony import BBox, Client, Collection, Request, AddLabelsRequest, Environment"
]
},
{
Expand Down Expand Up @@ -85,7 +85,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create labels on jobs"
"#### Add labels on jobs"
]
},
{
Expand All @@ -96,8 +96,7 @@
},
"outputs": [],
"source": [
"request = LabelsRequest(\n",
" http_method=HttpMethod.PUT,\n",
"request = AddLabelsRequest(\n",
" labels=['foo', 'bar'],\n",
" job_ids=[job_1, job_2])\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion harmony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

from harmony.config import Environment
from harmony.harmony import BBox, WKT, Client, Collection, LinkType, Dimension
from harmony.harmony import HttpMethod, Request, CapabilitiesRequest, LabelsRequest
from harmony.harmony import HttpMethod, Request, CapabilitiesRequest, AddLabelsRequest
from harmony.util import s3_components
10 changes: 4 additions & 6 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,28 +586,26 @@ def error_messages(self) -> List[str]:
return error_msgs


class LabelsRequest(BaseRequest):
class AddLabelsRequest(BaseRequest):
"""A Harmony request to create or delete labels for jobs.
This request allows users to associate labels with Harmony jobs or remove
existing labels, facilitating job management and categorization.
Args:
http_method (HttpMethod): The HTTP method to use for the request (e.g., PUT, DELETE).
labels (List[str]): A list of labels to be added or removed.
job_ids (List[str]): A list of job IDs to which the labels apply.
Returns:
LabelsRequest: An instance of the request configured with the provided parameters.
AddLabelsRequest: An instance of the request configured with the provided parameters.
"""

def __init__(self,
*,
http_method: HttpMethod,
labels: List[str],
job_ids: List[str]
):
super().__init__(http_method=http_method)
super().__init__(http_method=HttpMethod.PUT)
self.labels = labels
self.job_ids = job_ids

Expand All @@ -633,7 +631,7 @@ class LinkType(Enum):
# Uses lambda functions to dynamically construct URLs based on the request type
request_url_map = {
CapabilitiesRequest: lambda self: f'{self.config.root_url}/capabilities',
LabelsRequest: lambda self: f'{self.config.root_url}/labels',
AddLabelsRequest: lambda self: f'{self.config.root_url}/labels',
}


Expand Down
5 changes: 2 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import responses

from harmony.harmony import BBox, Client, Collection, LinkType, ProcessingFailedException, Dimension
from harmony.harmony import Request, CapabilitiesRequest, DEFAULT_JOB_LABEL, HttpMethod, LabelsRequest
from harmony.harmony import Request, CapabilitiesRequest, DEFAULT_JOB_LABEL, AddLabelsRequest


@pytest.fixture()
Expand Down Expand Up @@ -1719,8 +1719,7 @@ def test_collection_capabilities_with_shortname_version():

@responses.activate
def test_create_labels_on_jobs():
request = LabelsRequest(
http_method=HttpMethod.PUT,
request = AddLabelsRequest(
labels=['foo', 'bar'],
job_ids=['job_1', 'job_2'],
)
Expand Down
45 changes: 14 additions & 31 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from harmony.harmony import BBox, WKT, Collection, OgcBaseRequest, Request, CapabilitiesRequest, Dimension
from harmony.harmony import HttpMethod, LabelsRequest
from harmony.harmony import AddLabelsRequest


def test_request_has_collection_with_id():
Expand Down Expand Up @@ -284,44 +284,27 @@ def test_collection_capabilities_request_shortname_version():
assert request.is_valid()


def test_valid_create_labels_request():
request = LabelsRequest(http_method=HttpMethod.PUT,
labels=['label1', 'label2'],
def test_valid_add_labels_request():
request = AddLabelsRequest(labels=['label1', 'label2'],
job_ids=['job_1', 'job_2'],)
assert request.is_valid()


def test_create_labels_request_missing_labels():
with pytest.raises(TypeError, match=".*missing 1 required keyword-only argument: 'labels'"):
LabelsRequest(
http_method=HttpMethod.PUT,
job_ids=['job_123']
)
def test_valid_add_labels_request_invalid_arguments():
with pytest.raises(TypeError, match=".*got an unexpected keyword argument 'job_labels'"):
AddLabelsRequest(job_labels=['label1'])


def test_create_labels_request_missing_job_ids():
with pytest.raises(TypeError, match=".*missing 1 required keyword-only argument: 'job_ids'"):
LabelsRequest(
http_method=HttpMethod.PUT,
labels=['label1']
)
def test_add_labels_request_missing_labels():
with pytest.raises(TypeError, match=".*missing 1 required keyword-only argument: 'labels'"):
AddLabelsRequest(job_ids=['job_123'])


def test_create_labels_request_missing_http_method():
with pytest.raises(TypeError, match=".*missing 1 required keyword-only argument: 'http_method'"):
LabelsRequest(
labels=['label1'],
job_ids=['job_123']
)
def test_add_labels_request_missing_job_ids():
with pytest.raises(TypeError, match=".*missing 1 required keyword-only argument: 'job_ids'"):
AddLabelsRequest(labels=['label1'])


def test_create_labels_request_missing_labels_and_job_ids():
def test_add_labels_request_missing_all_arguments():
with pytest.raises(TypeError, match=".*missing 2 required keyword-only arguments: 'labels' and 'job_ids'"):
LabelsRequest(
http_method=HttpMethod.PUT
)


def test_create_labels_request_missing_all_arguments():
with pytest.raises(TypeError, match=".*missing 3 required keyword-only arguments: 'http_method', 'labels', and 'job_ids'"):
LabelsRequest()
AddLabelsRequest()

0 comments on commit 956807b

Please sign in to comment.