Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 363feed

Browse files
authored
Merge pull request #35 from dhuckins/master
added flexibility to http client
2 parents 52976af + 3ae681f commit 363feed

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ examples/upwork
2020
upwork/__init__.py.back
2121
python_upwork.egg-info
2222
reference-docs
23-
_gh-pages
23+
_gh-pages
24+
.idea

upwork/ca_certs_locater.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# openssl installed using `brew install openssl`
66
OSX_PATH = '/usr/local/etc/openssl/cert.pem'
77

8+
CUSTOM_SSL_CERT_PATH = os.getenv('UPWORK_SSL_CERT', '')
9+
810

911
def get():
1012
"""Return a path to a certificate authority file.
@@ -18,6 +20,9 @@ def get():
1820
if os.path.exists(OSX_PATH):
1921
return OSX_PATH
2022

23+
if CUSTOM_SSL_CERT_PATH and os.path.exists(CUSTOM_SSL_CERT_PATH):
24+
return CUSTOM_SSL_CERT_PATH
25+
2126
# Fall back to the httplib2 default behavior by raising an
2227
# ImportError if we have not found the file.
2328
raise ImportError()

upwork/client.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99

1010
from urllib3 import Retry
1111

12-
from upwork import ca_certs_locater
1312
from upwork.oauth import OAuth
1413
from upwork.http import raise_http_error
1514
from upwork.utils import decimal_default
1615
from upwork.exceptions import IncorrectJsonResponseError
1716

18-
1917
__all__ = ["Client"]
2018

21-
2219
logger = logging.getLogger('python-upwork')
2320

2421
if os.environ.get("PYTHON_UPWORK_DEBUG", False):
2522
if os.environ.get("PYTHON_UPWORK_DEBUG_FILE", False):
2623
fh = logging.FileHandler(filename=os.environ["PYTHON_UPWORK_DEBUG_FILE"]
27-
)
24+
)
2825
fh.setLevel(logging.DEBUG)
2926
logger.addHandler(fh)
3027
else:
@@ -92,13 +89,17 @@ class Client(object):
9289
9390
:timeout: (optional, default ``8 secs``)
9491
Socket operations timeout.
92+
93+
:poolmanager: (optional, default ``None``)
94+
http connection pool manager
95+
from :py:mod:`urllib3.poolmanager`
9596
"""
9697

9798
def __init__(self, public_key, secret_key,
9899
oauth_access_token=None, oauth_access_token_secret=None,
99100
fmt='json', finreport=True, hr=True, messages=True,
100101
offers=True, provider=True, task=True, team=True,
101-
timereport=True, job=True, timeout=8):
102+
timereport=True, job=True, timeout=8, poolmanager=None):
102103

103104
self.public_key = public_key
104105
self.secret_key = secret_key
@@ -114,17 +115,20 @@ def __init__(self, public_key, secret_key,
114115
# """
115116
# The warning will appear only in logs
116117
logging.captureWarnings(True)
117-
self.http = urllib3.PoolManager(
118-
cert_reqs='CERT_REQUIRED',
119-
ca_certs=ca_certs_locater.get(),
120-
timeout=urllib3.Timeout(connect=0.5, read=float(timeout)),
121-
retries=False
122-
)
118+
if poolmanager is None:
119+
from upwork import ca_certs_locater
120+
poolmanager = urllib3.PoolManager(
121+
cert_reqs='CERT_REQUIRED',
122+
ca_certs=ca_certs_locater.get(),
123+
timeout=urllib3.Timeout(connect=0.5, read=float(timeout)),
124+
retries=False
125+
)
126+
self.http = poolmanager
123127

124128
self.oauth_access_token = oauth_access_token
125129
self.oauth_access_token_secret = oauth_access_token_secret
126130

127-
#Namespaces
131+
# Namespaces
128132
self.auth = OAuth(self)
129133

130134
if finreport:
@@ -168,7 +172,7 @@ def __init__(self, public_key, secret_key,
168172
from upwork.routers.job import Job
169173
self.job = Job(self)
170174

171-
#Shortcuts for HTTP methods
175+
# Shortcuts for HTTP methods
172176
def get(self, url, data=None):
173177
return self.read(url, data, method='GET', fmt=self.fmt)
174178

@@ -231,7 +235,7 @@ def urlopen(self, url, data=None, method='GET', headers=None):
231235
return self.http.urlopen(
232236
method, url, body=post_data,
233237
headers={'Content-Type':
234-
'application/x-www-form-urlencoded;charset=UTF-8'})
238+
'application/x-www-form-urlencoded;charset=UTF-8'})
235239
elif method in ('PUT', 'DELETE'):
236240
url = '{0}?{1}'.format(url, post_data)
237241
headers['Content-Type'] = 'application/json'
@@ -307,4 +311,5 @@ def read(self, url, data=None, method='GET', fmt='json'):
307311

308312
if __name__ == "__main__":
309313
import doctest
314+
310315
doctest.testmod()

0 commit comments

Comments
 (0)