Skip to content

Commit

Permalink
SUSHI 5: raise exception (Sushi5Error) for exceptions from server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wooble committed Jun 18, 2019
1 parent 7155eca commit 829a91d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pycounter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ class RequestorNotAuthorizedError(SushiException):
pass


class Sushi5Error(SushiException):
"""Error from SUSHI release 5.
Attributes:
message: error message from the server
"""

def __init__(self, message, severity, code):
self.message = message
self.severity = severity
self.code = code


class ReportNotSupportedError(SushiException):
"""Server cannot serve the requested report name or version."""

Expand Down
11 changes: 10 additions & 1 deletion pycounter/sushi5.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,16 @@ def get_sushi_stats_raw(
response.content,
)

return response.json()
response_data = response.json()

if "Exceptions" in response_data["Report_Header"]:
raise pycounter.exceptions.Sushi5Error(
message=response_data["Report_Header"]["Exceptions"][0]["Message"],
severity=response_data["Report_Header"]["Exceptions"][0]["Severity"],
code=response_data["Report_Header"]["Exceptions"][0]["Code"],
)

return response_data


def _check_params(kwargs, release):
Expand Down
18 changes: 18 additions & 0 deletions pycounter/test/counter5/data/not_authorized.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Report_Header": {
"Created": "2019-06-18T12:53:32Z",
"Created_By": null,
"Customer_ID": null,
"Exceptions": [
{
"Code": 2000,
"Message": "Requestor Not Authorized to Access Service",
"Severity": "Error"
}
],
"Release": "5",
"Report_Filters": [],
"Report_ID": "TR_J1",
"Report_Name": "Journal Requests (Excluding OA_Gold)"
}
}
29 changes: 29 additions & 0 deletions pycounter/test/counter5/test_sushi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
"""Tests for COUNTER 5 SUSHI support."""

import io
import os

from httmock import HTTMock, all_requests
import pytest

import pycounter.exceptions
import pycounter.sushi5


def test_report_type(sushi5_report):
assert sushi5_report.report_type == u"TR_J1"
Expand Down Expand Up @@ -28,3 +37,23 @@ def test_metric(sushi5_report):
def test_doi(sushi5_report):
publication = next(iter(sushi5_report))
assert publication.doi == "some.fake.doi"


@all_requests
def not_authorized(url_unused, request_unused):
"""Mocked SUSHI service."""
path = os.path.join(os.path.dirname(__file__), "data", "not_authorized.json")
with io.open(path, "r", encoding="utf-8") as datafile:
return datafile.read()


def test_error_not_authorized():
with pytest.raises(pycounter.exceptions.Sushi5Error) as exception:
with HTTMock(not_authorized):
pycounter.sushi5.get_sushi_stats_raw(
url="https://example.com/sushi", release=5
)
exc = exception.value
assert exc.message == u"Requestor Not Authorized to Access Service"
assert exc.severity == "Error"
assert exc.code == 2000

0 comments on commit 829a91d

Please sign in to comment.