Skip to content

Commit

Permalink
Port away from requests -> urllib3 for thread-safety. Resolves python…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaheed Haque committed Jul 11, 2019
1 parent 53eb41c commit 8c03c0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
20 changes: 10 additions & 10 deletions consul/std.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import requests
import urllib3

from consul import base


__all__ = ['Consul']
JSON_HEADER = {'Content-Type': 'application/json'}


class HTTPClient(base.HTTPClient):
def __init__(self, *args, **kwargs):
super(HTTPClient, self).__init__(*args, **kwargs)
self.session = requests.session()
cert_file = kwargs.get('cert', None)
cert_reqs = 'CERT_REQUIRED' if kwargs.get('verify', False) else None
self.session = urllib3.PoolManager(cert_file=cert_file, cert_reqs=cert_reqs)

def response(self, response):
response.encoding = 'utf-8'
return base.Response(
response.status_code, response.headers, response.text)
response.status, response.headers, response.data.decode('utf-8'))

def get(self, callback, path, params=None):
uri = self.uri(path, params)
return callback(self.response(
self.session.get(uri, verify=self.verify, cert=self.cert)))
self.session.request('GET', uri)))

def put(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return callback(self.response(
self.session.put(uri, data=data, verify=self.verify,
cert=self.cert)))
self.session.request('PUT', uri, body=data, headers=JSON_HEADER)))

def delete(self, callback, path, params=None):
uri = self.uri(path, params)
return callback(self.response(
self.session.delete(uri, verify=self.verify, cert=self.cert)))
self.session.request('DELETE', uri)))

def post(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return callback(self.response(
self.session.post(uri, data=data, verify=self.verify,
cert=self.cert)))
self.session.request('POST', uri, body=data, headers=JSON_HEADER)))


class Consul(base.Consul):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
requests>=2.0
urllib3>=1.25
six>=1.4
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ commands =
deps =
pytest
pytest-rerunfailures
requests
commands =
py.test --reruns=3 {posargs:consul tests}

Expand All @@ -35,6 +36,7 @@ deps =
tornado
aiohttp
flake8
requests
commands =
py.test --reruns=3 {posargs:consul tests}
flake8 --exclude=".tox/*,xx/*,__*,docs/*"
Expand All @@ -50,6 +52,7 @@ deps =
tornado
aiohttp
flake8
requests
commands =
py.test --reruns=3 {posargs:consul tests}
flake8 --exclude=".tox/*,xx/*,__*,docs/*"
Expand Down

0 comments on commit 8c03c0f

Please sign in to comment.