From f3a96fc30cfcdc87d575fd19f0d505a358b2e760 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 8 Jul 2019 23:54:07 -0500 Subject: [PATCH] Post and get now return a tuple with a full requests response object and the response data Fixed broken max_size validation --- CHANGELOG.md | 12 +++++++++++- setup.py | 2 +- streamedrequests/dodownload.py | 6 +++++- streamedrequests/get.py | 4 +--- streamedrequests/post.py | 3 +-- streamedrequests/responsesize.py | 2 +- tests/test.py | 13 ++++++++++++- 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e17f444..1d7c110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,14 @@ This project uses Semantic Versioning -## 0.0.0 \ No newline at end of file +## 1.0.0 + +Post and get now return a tuple with a full requests response object and the response data + +## 0.0.1 + +Fixed broken max_size validator, added tests for max_size validation + +## 0.0.0 + +Initial release \ No newline at end of file diff --git a/setup.py b/setup.py index 6c888a6..d6b6333 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from distutils.core import setup setup(name='streamedrequests', - version='0.0.0', + version='1.0.0', description='Library for streaming http get or post request\'s responses', author='Kevin Froman', author_email='beardog@mailbox.org', diff --git a/streamedrequests/dodownload.py b/streamedrequests/dodownload.py index baa9606..2bb0d59 100644 --- a/streamedrequests/dodownload.py +++ b/streamedrequests/dodownload.py @@ -16,6 +16,7 @@ along with this program. If not, see . ''' import threading +from . import responsesize def __run_callback(data, sync, callback=None): if callback is None: # Do nothing if there is no callback @@ -26,6 +27,7 @@ def __run_callback(data, sync, callback=None): threading.Thread(target=callback, args=(data,)).start() def __do_download(req, max_size, chunk_size, callback, sync): + chunk_count = responsesize.SizeValidator(max_size) # Class to verify if the stream is staying within the max_size ret_data = b'' if chunk_size == 0: raise ValueError("Chunk size cannot be zero") @@ -35,4 +37,6 @@ def __do_download(req, max_size, chunk_size, callback, sync): if not callback is None: __run_callback(chunk, sync, callback) ret_data += chunk - return ret_data \ No newline at end of file + if 'text' in req.headers['content-type']: + ret_data = ret_data.decode('utf-8') + return (req, ret_data) \ No newline at end of file diff --git a/streamedrequests/get.py b/streamedrequests/get.py index 6b6aead..252a746 100644 --- a/streamedrequests/get.py +++ b/streamedrequests/get.py @@ -16,13 +16,11 @@ along with this program. If not, see . ''' import requests -from . import exceptions, responsesize, dodownload, setuptimeout +from . import exceptions, dodownload, setuptimeout def get(url, query_parameters=None, request_headers=None, sync=True, max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, proxy={}, callback=None, allow_redirects=True): - chunk_count = responsesize.SizeValidator(max_size) # Class to verify if the stream is staying within the max_size - timeouts = setuptimeout.__setup_timeout(connect_timeout, stream_timeout) # Get a timeout int or tuple # Requests uses separate value for connect vs stream timeout diff --git a/streamedrequests/post.py b/streamedrequests/post.py index 687e26c..f9c5a11 100644 --- a/streamedrequests/post.py +++ b/streamedrequests/post.py @@ -16,11 +16,10 @@ along with this program. If not, see . ''' import requests -from . import exceptions, responsesize, dodownload, setuptimeout +from . import exceptions, dodownload, setuptimeout def post(url, post_data=None, request_headers=None, sync=True, max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, proxy={}, callback=None, allow_redirects=True): - chunk_count = responsesize.SizeValidator(max_size) # Class to verify if the stream is staying within the max_size timeouts = setuptimeout.__setup_timeout(connect_timeout, stream_timeout) diff --git a/streamedrequests/responsesize.py b/streamedrequests/responsesize.py index 1171f5d..e808e94 100644 --- a/streamedrequests/responsesize.py +++ b/streamedrequests/responsesize.py @@ -18,7 +18,7 @@ from . import exceptions class SizeValidator: def __init__(self, max_size): - self.max_size = 0 + self.max_size = max_size self.size = 0 def add(self, amount): diff --git a/tests/test.py b/tests/test.py index 1feb022..53b047e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -70,7 +70,18 @@ class TestInit(unittest.TestCase): raise ValueError("test not found in post test data") def test_basic(self): - streamedrequests.get('http://127.0.0.1:8000/') + if "test" not in streamedrequests.get('http://127.0.0.1:8000/')[1]: + raise ValueError("test not found in response text") + + def test_max_size_fail(self): + with self.assertRaises(streamedrequests.exceptions.ResponseLimitReached): + streamedrequests.get('http://127.0.0.1:8000/', max_size=10) + with self.assertRaises(streamedrequests.exceptions.ResponseLimitReached): + streamedrequests.post('http://127.0.0.1:%s/' % (POST_PORT,), post_data='test', max_size=1) + + def test_max_size_pass(self): + streamedrequests.get('http://127.0.0.1:8000/', max_size=100000) + streamedrequests.post('http://127.0.0.1:%s/' % (POST_PORT,), post_data='test', max_size=100000) def test_fail(self): with self.assertRaises(requests.exceptions.ConnectionError):