Skip to content

Commit

Permalink
feat: add 'POST ../tools/{id}/versions'
Browse files Browse the repository at this point in the history
  • Loading branch information
krish8484 committed Oct 13, 2020
1 parent 048c271 commit 752205f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@
"verified_source",
]
}
MOCK_VERSION_POST = {
"author": [
"author"
],
'descriptor_type': None,
"images": None,
"included_apps": [
"https://bio.tools/tool/mytum.de/SNAP2/1",
"https://bio.tools/bioexcel_seqqc"
],
"is_production": True,
"name": "name",
"signed": True,
"verified": None,
"verified_source": [
"verified_source",
]
}
MOCK_TOOL_CLASS_WITH_ID = {
"description": "description",
"id": "234561",
Expand Down Expand Up @@ -166,6 +184,35 @@ def test_success_InvalidPayload(self, requests_mock):
)


class TestPostToolVersion:
"""Test poster for tool versions."""

cli = TRSClient(
uri=MOCK_TRS_URI,
token=MOCK_TOKEN,
)
endpoint = (
f"{cli.uri}/tools/{MOCK_ID}/versions"
)

def test_success(self, monkeypatch, requests_mock):
"""Returns 200 response."""
requests_mock.post(self.endpoint, json=MOCK_ID)
r = self.cli.post_tool_version(
id=MOCK_ID,
payload=MOCK_VERSION_POST
)
assert r == MOCK_ID

def test_success_InvalidPayload(self, requests_mock):
"""Raises InvalidPayload when incorrect input is provided"""
with pytest.raises(InvalidPayload):
self.cli.post_tool_version(
id=MOCK_ID,
payload=MOCK_RESPONSE_INVALID
)


class TestGetTool:
"""Test getter for tool with a given id."""

Expand Down
63 changes: 63 additions & 0 deletions trs_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Tool,
ToolClass,
ToolClassRegister,
ToolVersionRegister
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -162,6 +163,68 @@ def post_tool_class(
)
return response # type: ignore

def post_tool_version(
self,
id: str,
payload: Dict,
accept: str = 'application/json',
token: Optional[str] = None,
) -> str:
"""Register a tool version.
Arguments:
payload: Tool version data.
accept: Requested content type.
token: Bearer token for authentication. Set if required by TRS
implementation and if not provided when instatiating client or
if expired.
Returns:
ID of registered TRS toolVersion in case of a `200` response, or an
instance of `Error` for all other responses.
Raises:
requests.exceptions.ConnectionError: A connection to the provided
TRS instance could not be established.
trs_cli.errors.InvalidPayload: The object data payload could not
be validated against the API schema.
trs_cli.errors.InvalidResponseError: The response could not be
validated against the API schema.
"""
# validate requested content type and get request headers
self._validate_content_type(
requested_type=accept,
available_types=['application/json'],
)
self._get_headers(
content_accept=accept,
content_type='application/json',
token=token,
)

# build request URL
url = f"{self.uri}/tools/{id}/versions"
logger.info(f"Connecting to '{url}'...")

# validate payload
try:
ToolVersionRegister(**payload).dict()
except pydantic.ValidationError:
raise InvalidPayload(
"The tool version data could not be validated against API "
"schema."
)

# send request
response = self._send_request_and_validate_response(
url=url,
method='post',
payload=payload,
)
logger.info(
"Registered tool version"
)
return response # type: ignore

def get_tool(
self,
id: str,
Expand Down

0 comments on commit 752205f

Please sign in to comment.