Post and get now return a tuple with a full requests response object and the response data

Fixed broken max_size validation
This commit is contained in:
Kevin Froman 2019-07-08 23:54:07 -05:00
parent a71ac1d046
commit f3a96fc30c
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
7 changed files with 32 additions and 10 deletions

View File

@ -2,4 +2,14 @@
This project uses Semantic Versioning This project uses Semantic Versioning
## 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 ## 0.0.0
Initial release

View File

@ -1,7 +1,7 @@
from distutils.core import setup from distutils.core import setup
setup(name='streamedrequests', setup(name='streamedrequests',
version='0.0.0', version='1.0.0',
description='Library for streaming http get or post request\'s responses', description='Library for streaming http get or post request\'s responses',
author='Kevin Froman', author='Kevin Froman',
author_email='beardog@mailbox.org', author_email='beardog@mailbox.org',

View File

@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import threading import threading
from . import responsesize
def __run_callback(data, sync, callback=None): def __run_callback(data, sync, callback=None):
if callback is None: # Do nothing if there is no callback 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() threading.Thread(target=callback, args=(data,)).start()
def __do_download(req, max_size, chunk_size, callback, sync): 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'' ret_data = b''
if chunk_size == 0: if chunk_size == 0:
raise ValueError("Chunk size cannot be zero") 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: if not callback is None:
__run_callback(chunk, sync, callback) __run_callback(chunk, sync, callback)
ret_data += chunk ret_data += chunk
return ret_data if 'text' in req.headers['content-type']:
ret_data = ret_data.decode('utf-8')
return (req, ret_data)

View File

@ -16,13 +16,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import requests import requests
from . import exceptions, responsesize, dodownload, setuptimeout from . import exceptions, dodownload, setuptimeout
def get(url, query_parameters=None, request_headers=None, sync=True, def get(url, query_parameters=None, request_headers=None, sync=True,
max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0,
proxy={}, callback=None, allow_redirects=True): 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 timeouts = setuptimeout.__setup_timeout(connect_timeout, stream_timeout) # Get a timeout int or tuple
# Requests uses separate value for connect vs stream timeout # Requests uses separate value for connect vs stream timeout

View File

@ -16,11 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import requests import requests
from . import exceptions, responsesize, dodownload, setuptimeout from . import exceptions, dodownload, setuptimeout
def post(url, post_data=None, request_headers=None, sync=True, def post(url, post_data=None, request_headers=None, sync=True,
max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0,
proxy={}, callback=None, allow_redirects=True): 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) timeouts = setuptimeout.__setup_timeout(connect_timeout, stream_timeout)

View File

@ -18,7 +18,7 @@
from . import exceptions from . import exceptions
class SizeValidator: class SizeValidator:
def __init__(self, max_size): def __init__(self, max_size):
self.max_size = 0 self.max_size = max_size
self.size = 0 self.size = 0
def add(self, amount): def add(self, amount):

View File

@ -70,7 +70,18 @@ class TestInit(unittest.TestCase):
raise ValueError("test not found in post test data") raise ValueError("test not found in post test data")
def test_basic(self): 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): def test_fail(self):
with self.assertRaises(requests.exceptions.ConnectionError): with self.assertRaises(requests.exceptions.ConnectionError):