Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion taskcluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,21 @@
}


def createSession(*args, **kwargs):
""" Create a new requests session. This passes through all positional and
keyword arguments to the requests.Session() constructor
"""
return requests.Session(*args, **kwargs)


class BaseClient(object):
""" Base Class for API Client Classes. Each individual Client class
needs to set up its own methods for REST endpoints and Topic Exchange
routing key patterns. The _makeApiCall() and _topicExchange() methods
help with this.
"""

def __init__(self, options=None):
def __init__(self, options=None, session=None):
o = copy.deepcopy(self.classOptions)
o.update(_defaultConfig)
if options:
Expand All @@ -69,6 +76,11 @@ def __init__(self, options=None):
log.debug('credentials key scrubbed from logging output')
log.debug(dict((k, v) for k, v in o.items() if k != 'credentials'))

if session:
self.session = options.session
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be: self.session = session?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. that was a typo from addressing review feedback. this would still work if you pass in the session as part of options, but thats not the desired situation.

else:
self.session = createSession()

def makeHawkExt(self):
""" Make an 'ext' for Hawk authentication """
o = self.options
Expand Down
9 changes: 5 additions & 4 deletions taskcluster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def scope_match(assumed_scopes, required_scope_sets):
return False # none of the required_scope_sets were satisfied


def makeHttpRequest(method, url, payload, headers, retries=MAX_RETRIES):
def makeHttpRequest(method, url, payload, headers, retries=MAX_RETRIES, session=None):
""" Make an HTTP request and retry it until success, return request """
retry = -1
response = None
Expand All @@ -156,7 +156,7 @@ def makeHttpRequest(method, url, payload, headers, retries=MAX_RETRIES):

log.debug('Making attempt %d', retry)
try:
response = makeSingleHttpRequest(method, url, payload, headers)
response = makeSingleHttpRequest(method, url, payload, headers, session)
except requests.exceptions.RequestException as rerr:
if retry < retries:
log.warn('Retrying because of: %s' % rerr)
Expand All @@ -180,12 +180,13 @@ def makeHttpRequest(method, url, payload, headers, retries=MAX_RETRIES):
assert False, "Error from last retry should have been raised!"


def makeSingleHttpRequest(method, url, payload, headers):
def makeSingleHttpRequest(method, url, payload, headers, session=None):
method = method.upper()
log.debug('Making a %s request to %s', method, url)
log.debug('HTTP Headers: %s' % str(headers))
log.debug('HTTP Payload: %s (limit 100 char)' % str(payload)[:100])
response = requests.request(method.upper(), url, data=payload, headers=headers)
obj = session if session else requests
response = obj.request(method.upper(), url, data=payload, headers=headers)
log.debug('Received HTTP Status: %s' % response.status_code)
log.debug('Received HTTP Headers: %s' % str(response.headers))
log.debug('Received HTTP Payload: %s (limit 1024 char)' % str(response.text)[:1024])
Expand Down
6 changes: 6 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,12 @@ def test_insert_to_index(self):
}
result = self.i.insertTask('testing', payload)
self.assertEqual(payload['expires'], result['expires'])
payload['rank'] += 1
result = self.i.insertTask('testing', payload)
self.assertEqual(payload['expires'], result['expires'])
payload['rank'] += 1
result = self.i.insertTask('testing', payload)
self.assertEqual(payload['expires'], result['expires'])

def test_listworkertypes_signed_url(self):
surl = self.a.buildSignedUrl('listWorkerTypes')
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class TestPutfile(base.TCTest):
def test_success_put_file(self):
with mock.patch.object(subject, 'makeSingleHttpRequest') as p:
subject.putFile('setup.py', 'http://www.example.com', 'text/plain')
p.assert_called_once_with('put', 'http://www.example.com', mock.ANY, mock.ANY)
p.assert_called_once_with('put', 'http://www.example.com', mock.ANY, mock.ANY, mock.ANY)


class TestStableSlugIdClosure(TestCase):
Expand Down