-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from ral-facilities/DSEGOG-311-user-office-re…
…st-api DSEGOG-311 Move User Office API from SOAP to REST
- Loading branch information
Showing
7 changed files
with
174 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
operationsgateway_api/src/experiments/rest_error_handling.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from functools import wraps | ||
|
||
from requests import HTTPError, RequestException, Timeout | ||
from requests.exceptions import ConnectionError | ||
|
||
from operationsgateway_api.src.exceptions import ExperimentDetailsError | ||
|
||
|
||
def rest_error_handling(endpoint_name): | ||
""" | ||
Parameterised decorator to handle errors raised during the use of the REST API for | ||
the User Office (used to login where the session ID is used to access the Scheduler) | ||
""" | ||
|
||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except ConnectionError as exc: | ||
raise ExperimentDetailsError("Connection error occurred") from exc | ||
except HTTPError as exc: | ||
raise ExperimentDetailsError( | ||
"A HTTP error occurred when trying to retrieve experiment data from" | ||
f" {endpoint_name} on the external system", | ||
) from exc | ||
except Timeout as exc: | ||
raise ExperimentDetailsError( | ||
f"Request to {endpoint_name} to retrieve experiment data from the" | ||
" external system has timed out", | ||
) from exc | ||
except RequestException as exc: | ||
raise ExperimentDetailsError( | ||
f"Something went wrong when accessing {endpoint_name} to retrieve" | ||
" experiment data from an external system", | ||
) from exc | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import requests | ||
from requests import ConnectionError, HTTPError, RequestException, Timeout | ||
|
||
from operationsgateway_api.src.exceptions import ExperimentDetailsError | ||
from operationsgateway_api.src.experiments.rest_error_handling import ( | ||
rest_error_handling, | ||
) | ||
|
||
|
||
class TestRESTErrorHandling: | ||
@pytest.mark.parametrize( | ||
"raised_exception, expected_exception", | ||
[ | ||
pytest.param(ConnectionError, ExperimentDetailsError, id="ConnectionError"), | ||
pytest.param(HTTPError(), ExperimentDetailsError, id="HTTPError"), | ||
pytest.param(Timeout(), ExperimentDetailsError, id="Timeout"), | ||
pytest.param( | ||
RequestException, | ||
ExperimentDetailsError, | ||
id="RequestException (base class for Requests exceptions)", | ||
), | ||
], | ||
) | ||
def test_correct_error_raised(self, raised_exception, expected_exception): | ||
@rest_error_handling("Testing") | ||
def raise_exception(): | ||
with patch("requests.get", side_effect=raised_exception): | ||
requests.get("Test URL") | ||
|
||
with pytest.raises(expected_exception): | ||
raise_exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters