Skip to content

Commit

Permalink
Merge pull request #146 from rosh-n-ix/feature/optional_last_response…
Browse files Browse the repository at this point in the history
…_preserving

Feature/optional last response preserving
  • Loading branch information
glow-mdsol authored Feb 4, 2025
2 parents 1be86ee + 85726f0 commit aa32295
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rwslib"
version = "1.2.12"
version = "1.2.13"
description = "Rave Web Services for Python"
authors = ["Ian Sparks <isparks@trialgrid.com>"]
maintainers = ["Geoff Low <geoff.low@3ds.com>"]
Expand Down
28 changes: 13 additions & 15 deletions rwslib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ def __init__(
# Time taken to process last request
self.request_time = None

def send_request(self, request_object, timeout=None, retries=1, **kwargs):
def send_request(self, request_object, timeout=None, retries=1, preserve_last_response=True, **kwargs):
"""Send request to RWS endpoint. The request object passed provides the URL endpoint and the HTTP method.
Takes the text response from RWS and allows the request object to modify it for return. This allows the request
object to return text, an XML document object, a CSV file or anything else that can be generated from the text
response from RWS.
A timeout, in seconds, can be optionally passed into send_request.
Preserving response in class can be disabled by setting `preserve_last_response=False`.
"""
if not isinstance(request_object, RWSRequest):
raise ValueError("Request object must be a subclass of RWSRequest")
Expand Down Expand Up @@ -98,22 +99,19 @@ def send_request(self, request_object, timeout=None, retries=1, **kwargs):

try:
r = action(full_url, **kwargs) # type: requests.models.Response
except (
requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout,
) as exc:
if isinstance(exc, (requests.exceptions.ConnectTimeout,)):
raise RWSException(
"Server Connection Timeout",
"Connection timeout for {}".format(full_url),
)
elif isinstance(exc, (requests.exceptions.ReadTimeout,)):
raise RWSException(
"Server Read Timeout", "Read timeout for {}".format(full_url)
)
except requests.exceptions.ConnectTimeout:
raise RWSException(
"Server Connection Timeout",
"Connection timeout for {}".format(full_url),
)
except requests.exceptions.ReadTimeout:
raise RWSException(
"Server Read Timeout", "Read timeout for {}".format(full_url)
)

self.request_time = time.time() - start_time
self.last_result = r # see also r.elapsed for timedelta object.
if preserve_last_response:
self.last_result = r # see also r.elapsed for timedelta object.

if r.status_code in [400, 404]:
# Is it a RWS response?
Expand Down
18 changes: 18 additions & 0 deletions tests/rwslib/test_rwslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ def test_virtual_directory(self):
self.assertEqual(v, "1.0.0")
self.assertEqual(rave.last_result.status_code, 200)

@httpretty.activate
def test_disable_response_preserving(self):
"""A simple test, patching the get request so that it does not hit a website"""

httpretty.register_uri(
httpretty.GET,
"https://innovate.mdsol.com/RaveWebServices/version",
status=200,
body="1.0.0",
)

# Now my test
rave = rwslib.RWSConnection("https://innovate.mdsol.com")
v = rave.send_request(rwslib.rws_requests.VersionRequest(), preserve_last_response=False)

self.assertEqual(v, "1.0.0")
self.assertEqual(rave.last_result, None)


class TestMustBeRWSRequestSubclass(unittest.TestCase):
"""Test that request object passed must be RWSRequest subclass"""
Expand Down

0 comments on commit aa32295

Please sign in to comment.