Skip to content

Commit

Permalink
feat: add 'POST /tools'
Browse files Browse the repository at this point in the history
  • Loading branch information
krish8484 committed Oct 13, 2020
1 parent 048c271 commit 531d8ec
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
61 changes: 61 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 All @@ -95,6 +113,22 @@
MOCK_VERSION
]
}
MOCK_TOOL_POST = {
"aliases": [
"alias_1",
"alias_2",
"alias_3",
],
"checker_url": "checker_url",
"description": "description",
"has_checker": True,
"name": "name",
"organization": "organization",
"toolclass": MOCK_TOOL_CLASS_WITH_ID,
"versions": [
MOCK_VERSION_POST
]
}
MOCK_TOOL_CLASS = {
"description": "string",
"id": "string",
Expand Down Expand Up @@ -166,6 +200,33 @@ def test_success_InvalidPayload(self, requests_mock):
)


class TestPostTool:
"""Test poster for tools."""

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

def test_success(self, monkeypatch, requests_mock):
"""Returns 200 response."""
requests_mock.post(self.endpoint, json=MOCK_ID)
r = self.cli.post_tool(
payload=MOCK_TOOL_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(
payload=MOCK_RESPONSE_INVALID
)


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

Expand Down
62 changes: 62 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,
ToolRegister,
)

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

def post_tool(
self,
payload: Dict,
accept: str = 'application/json',
token: Optional[str] = None,
) -> str:
"""Register a tool.
Arguments:
payload: Tool 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 tool 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"
logger.info(f"Connecting to '{url}'...")

# validate payload
try:
ToolRegister(**payload).dict()
except pydantic.ValidationError:
raise InvalidPayload(
"The tool 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"
)
return response # type: ignore

def get_tool(
self,
id: str,
Expand Down

0 comments on commit 531d8ec

Please sign in to comment.