Skip to content

Commit 01e6992

Browse files
jecarrKadeMorton
authored andcommitted
web_svc.get_response_from_url requests update
1 parent 9e04286 commit 01e6992

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

threadcomponents/service/web_svc.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ def get_route(self, route_key, param=None):
105105
except KeyError:
106106
return None
107107

108-
def clear_cached_responses(self): # TODO consider how often to call this
109-
self.cached_responses = dict()
108+
def check_and_clear_cached_responses(self):
109+
if len(self.cached_responses) > 100:
110+
self.cached_responses = dict()
110111

111112
async def action_allowed(self, request, action, context=None):
112113
"""Function to check an action is permitted given a request."""
@@ -300,41 +301,28 @@ async def get_url(self, url, returned_format=None):
300301
def get_response_from_url(self, url, log_errors=True, allow_error=True):
301302
"""Function to return a request Response object from a given URL."""
302303
# Retrieve a cached response for this URL
304+
self.check_and_clear_cached_responses()
303305
cached = self.cached_responses.get(url)
304306
if cached is not None:
305307
return cached
306-
# Specify if we retry retrieving a response on failure
307-
retry_on_fail = True
308-
# Flag if we can close the connection once we are finished
309-
close_conn = True
308+
310309
try:
311-
r = requests.get(url)
312-
except requests.exceptions.ConnectionError as conn_error:
313-
# Log error if requested
310+
with requests.get(url) as response:
311+
response_clone = requests.Response()
312+
response_clone.status_code = response.status_code
313+
response_clone.headers = response.headers
314+
response_clone._content = response.content
315+
response_clone.encoding = response.encoding
316+
response_clone.url = response.url
317+
318+
self.cached_responses[url] = response_clone
319+
return response_clone
320+
except requests.exceptions.ConnectionError as e:
314321
if log_errors:
315-
logging.error("URL connection failure: " + str(conn_error))
316-
# Raise the error if requested
322+
logging.error(f"URL retrieval failure: {e}")
323+
317324
if not allow_error:
318-
raise conn_error
319-
# If the URL could not be retrieved due to a raised Error, build a new Response object and skip retrying
320-
r = requests.models.Response()
321-
r.status_code = 418
322-
retry_on_fail = False
323-
close_conn = False
324-
if retry_on_fail and not r.ok:
325-
# If the request response is not good, close the current connection and replace with a prepared request
326-
r.close()
327-
sess = requests.Session()
328-
r = requests.Request("GET", url)
329-
prep = r.prepare()
330-
r = sess.send(prep)
331-
if not r.ok and log_errors:
332-
logging.error("URL retrieval failed with code " + str(r.status_code))
333-
if close_conn:
334-
r.close()
335-
# Cache the response object for this URL
336-
self.cached_responses[url] = r
337-
return r
325+
raise e
338326

339327
def urls_match(self, testing_url="", matches_with=""):
340328
"""Function to check if two URLs are the same."""

0 commit comments

Comments
 (0)