Skip to content

Commit

Permalink
feat: add 'PUT /tools/{id}'
Browse files Browse the repository at this point in the history
  • Loading branch information
krish8484 committed Oct 22, 2020
1 parent 5a933bc commit fc27d0e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
29 changes: 29 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,35 @@ def test_success_ValidationError(self):
)


class TestPutTool:
"""Test putter for tools."""

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

def test_success(self, requests_mock):
"""Returns 200 response."""
requests_mock.put(self.endpoint, json=MOCK_ID)
r = self.cli.put_tool(
id=MOCK_ID,
payload=MOCK_TOOL_POST
)
assert r == MOCK_ID

def test_success_ValidationError(self):
"""Raises validation error when incorrect input is provided"""
with pytest.raises(ValidationError):
self.cli.put_tool(
id=MOCK_ID,
payload=MOCK_RESPONSE_INVALID
)


class TestDeleteTool:
"""Test delete for tool."""

Expand Down
62 changes: 61 additions & 1 deletion trs_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,74 @@ def post_tool(
)
return response # type: ignore

def put_tool(
self,
id: str,
payload: Dict,
accept: str = 'application/json',
token: Optional[str] = None,
) -> str:
"""
Create a tool object with a predefined ID.
Overwrites any existing tool object with the same ID.
Arguments:
id: Tool id to be added.
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.
pydantic.ValidationError: 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}"
logger.info(f"Connecting to '{url}'...")

# validate payload
ToolRegister(**payload).dict()

# send request
response = self._send_request_and_validate_response(
url=url,
method='put',
payload=payload,
)
logger.info(
f"Registered tool with id: {id}"
)
return response # type: ignore

def delete_tool(
self,
id: str,
accept: str = 'application/json',
token: Optional[str] = None,
) -> str:
"""Delete a tool.
TRS URI pointing to a given tool to be deleted
id: TRS URI pointing to a given tool to be deleted
accept: Requested content type.
token: Bearer token for authentication. Set if required by TRS
implementation and if not provided when instatiating client or
Expand Down

0 comments on commit fc27d0e

Please sign in to comment.