Skip to content

Commit

Permalink
finalize PR
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueg committed Oct 9, 2020
1 parent abe54ab commit 02c0a32
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tests/test_client.py
Expand Up @@ -363,7 +363,7 @@ def test_no_success_InvalidResponseError(self, requests_mock):


class TestGetToolClasses:
"""Test getter for primary descriptor of a given descriptor type."""
"""Test getter for tool classes."""

cli = TRSClient(
uri=MOCK_TRS_URI,
Expand Down Expand Up @@ -408,7 +408,7 @@ def test_no_success_valid_error_response(self, requests_mock):
status_code=400,
)
r = self.cli.get_tool_classes()
assert r.dict() == MOCK_ERROR
assert r.dict() == MOCK_ERROR # type: ignore

def test_no_success_InvalidResponseError(self, requests_mock):
"""Returns no 200 and error schema validation fails."""
Expand Down
43 changes: 34 additions & 9 deletions trs_cli/client.py
Expand Up @@ -123,6 +123,7 @@ def get_descriptor(
registry. It is optional if a TRS URI is passed and includes
version information. If provided nevertheless, then the
`version_id` retrieved from the TRS URI is overridden.
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.
Expand Down Expand Up @@ -188,7 +189,10 @@ def get_descriptor(
else:
try:
response_val = FileWrapper(**response.json())
except pydantic.ValidationError:
except (
json.decoder.JSONDecodeError,
pydantic.ValidationError,
):
raise InvalidResponseError(
"Response could not be validated against API schema"
)
Expand Down Expand Up @@ -230,6 +234,7 @@ def get_descriptor_by_path(
version information. If provided nevertheless, then the
`version_id` retrieved from the TRS URI is overridden.
is_encoded: Value of `path` is already percent/URL-encoded.
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.
Expand Down Expand Up @@ -296,7 +301,10 @@ def get_descriptor_by_path(
else:
try:
response_val = FileWrapper(**response.json())
except pydantic.ValidationError:
except (
json.decoder.JSONDecodeError,
pydantic.ValidationError,
):
raise InvalidResponseError(
"Response could not be validated against API schema"
)
Expand Down Expand Up @@ -407,7 +415,10 @@ def get_files(
response_val = [
ToolFile(**tool) for tool in response.json()
]
except pydantic.ValidationError:
except (
json.decoder.JSONDecodeError,
pydantic.ValidationError,
):
raise InvalidResponseError(
"Response could not be validated against API schema"
)
Expand All @@ -420,19 +431,21 @@ def get_files(

def get_tool_classes(
self,
accept: str = 'application/json',
token: Optional[str] = None
) -> Union[List[ToolClass], Error]:
"""Retrieve all tool types
"""Retrieve tool classes.
Arguments:
token: Bearer token for authentication. Set if required by TRS
implementation and if not provided when instatiating client or
if expired.
accept: Requested content type.
Returns:
Unmarshalled TRS response as either an instance of `ToolClass` in
case of a `200` response, or an instance of `Error` for all other
JSON reponses.
Unmarshalled TRS response as either a list of `ToolClass` objects
in case of a `200` response, or an instance of `Error` for all
other JSON reponses.
Raises:
requests.exceptions.ConnectionError: A connection to the provided
Expand All @@ -441,6 +454,15 @@ def get_tool_classes(
validated against the API schema.
"""
# validate requested content type, set token and get request headers
self._validate_content_type(
requested_type=accept,
available_types=['application/json', 'text/plain'],
)
if token:
self.token = token
self._get_headers(content_accept=accept)

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

Expand Down Expand Up @@ -474,12 +496,15 @@ def get_tool_classes(
response_val = [
ToolClass(**toolClass) for toolClass in response.json()
]
except pydantic.ValidationError:
except (
json.decoder.JSONDecodeError,
pydantic.ValidationError,
):
raise InvalidResponseError(
"Response could not be validated against API schema"
)
logger.info(
"Retrieved all tool types"
"Retrieved tool classes"
)

return response_val
Expand Down

0 comments on commit 02c0a32

Please sign in to comment.