Merge pull request #4 from bkorty/master

Expose additional tornado request features.
This commit is contained in:
dave-shawley 2017-08-07 13:54:07 -04:00 committed by GitHub
commit c9ed904bd5
3 changed files with 42 additions and 3 deletions

View file

@ -84,6 +84,15 @@ As with Tornado, to use the curl client which has numerous benefits:
app.listen(8000)
ioloop.IOLoop.current().start()
Environment Variables
---------------------
+------------------+----------------------------------------------------------+
| HTTP_MAX_CLIENTS | An optional setting that specifies the maximum number of |
| | simultaneous asynchronous HTTP requests. If not |
| | specified, the default Tornado value of 10 will be used. |
+------------------+----------------------------------------------------------+
License
-------
``sprockets.mixins.http`` is released under the `3-Clause BSD license <https://github.com/sprockets/sprockets.mixins.http/blob/master/LICENSE>`_.

View file

@ -8,6 +8,7 @@ requests.
import collections
import json
import logging
import os
import socket
import time
@ -66,7 +67,8 @@ class HTTPClientMixin(object):
request_timeout=DEFAULT_REQUEST_TIMEOUT,
auth_username=None,
auth_password=None,
user_agent=None):
user_agent=None,
allow_nonstandard_methods=False):
"""Perform a HTTP request
Will retry up to ``self.MAX_HTTP_RETRIES`` times.
@ -88,6 +90,8 @@ class HTTPClientMixin(object):
:param str auth_password: Password for HTTP authentication
:param str user_agent: The str used for the ``User-Agent`` header,
default used if unspecified.
:param bool allow_nonstardard_methods: Allow methods that don't adhere
to the HTTP spec.
:rtype: HTTPResponse
"""
@ -96,7 +100,13 @@ class HTTPClientMixin(object):
if body:
body = self._http_req_body_serialize(
body, request_headers['Content-Type'])
client = httpclient.AsyncHTTPClient()
# Workaround for Tornado defect.
if hasattr(client, 'max_clients') and os.getenv('HTTP_MAX_CLIENTS'):
client.max_clients = int(os.getenv('HTTP_MAX_CLIENTS'))
response, start_time = None, time.time()
for attempt in range(0, self.MAX_HTTP_RETRIES):
LOGGER.debug('%s %s (Attempt %i of %i) %r',
@ -114,7 +124,8 @@ class HTTPClientMixin(object):
request_timeout=request_timeout,
user_agent=user_agent or self._http_req_user_agent(),
follow_redirects=follow_redirects,
raise_error=False)
raise_error=False,
allow_nonstandard_methods=allow_nonstandard_methods)
except (OSError, socket.gaierror) as error:
LOGGER.debug('HTTP Request Error for %s to %s'
'attempt %i of %i: %s',

View file

@ -1,8 +1,9 @@
import json
import logging
import os
import uuid
from tornado import httputil, testing, web
from tornado import httpclient, httputil, testing, web
import mock
import umsgpack
@ -393,4 +394,22 @@ class MixinTestCase(testing.AsyncHTTPTestCase):
self.assertEqual(response.headers['Content-Type'], 'text/html')
self.assertEqual(response.body.decode('utf-8'), expectation)
@testing.gen_test()
def test_allow_nonstardard_methods(self):
response = yield self.mixin.http_fetch(
self.get_url('/test'),
method='DELETE',
body={'foo': 'bar', 'status_code': 200},
allow_nonstandard_methods=True)
self.assertTrue(response.ok)
@testing.gen_test()
def test_max_clients_settings_supported(self):
os.environ['HTTP_MAX_CLIENTS'] = '25'
response = yield self.mixin.http_fetch(
self.get_url('/test?foo=bar&status_code=200'))
self.assertTrue(response.ok)
del os.environ['HTTP_MAX_CLIENTS']
client = httpclient.AsyncHTTPClient()
self.assertEqual(client.max_clients, 25)