Skip to content

Commit

Permalink
Revert most of 437a3c3 (the switch to requests in treeherder-client)
Browse files Browse the repository at this point in the history
This reverts bug 1144417 (Treeherder-client using requests) apart from
the version bump hunk - for causing bug 1160856.
  • Loading branch information
Ed Morley committed May 3, 2015
1 parent 0e6e61f commit efa98c7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
44 changes: 30 additions & 14 deletions tests/client/test_treeherder_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ def test_send_artifact_collection(self, mock_send):

@patch("treeherder.client.client.oauth.generate_nonce")
@patch("treeherder.client.client.oauth.time.time")
@patch("treeherder.client.client.requests.post")
def test_send(self, mock_post, mock_time, mock_generate_nonce):
@patch("treeherder.client.client.httplib.HTTPConnection")
def test_send(self, mock_HTTPConnection, mock_time, mock_generate_nonce):

"""Can send data to the server."""
mock_time.return_value = 1342229050
Expand All @@ -472,33 +472,41 @@ def test_send(self, mock_post, mock_time, mock_generate_nonce):
oauth_secret='secret',
)

mock_response = mock_post.return_value
mock_conn = mock_HTTPConnection.return_value
mock_request = mock_conn.request
mock_response = mock_conn.getresponse.return_value

tjc = TreeherderJobCollection()

tjc.add(tjc.get_job(self.job_data[0]))

response = req.post(tjc)

self.assertEqual(mock_HTTPConnection.call_count, 1)
self.assertEqual(mock_HTTPConnection.call_args[0][0], host)
self.assertEqual(mock_response, response)
self.assertEqual(mock_post.call_count, 1)
self.assertEqual(mock_request.call_count, 1)

path, resp = mock_post.call_args
req.get_uri(tjc)

deserialized_data = json.loads(resp['data'])
method, path, data, header = mock_request.call_args[0]

self.assertEqual(method, "POST")

deserialized_data = json.loads(data)
self.assertEqual(
deserialized_data,
tjc.get_collection_data()
)
self.assertEqual(
resp['headers']['Content-Type'],
header['Content-Type'],
'application/json',
)

@patch("treeherder.client.client.oauth.generate_nonce")
@patch("treeherder.client.client.oauth.time.time")
@patch("treeherder.client.client.requests.post")
def test_send_without_oauth(self, mock_post, mock_time,
@patch("treeherder.client.client.httplib.HTTPConnection")
def test_send_without_oauth(self, mock_HTTPConnection, mock_time,
mock_generate_nonce):

"""Can send data to the server."""
Expand All @@ -515,7 +523,9 @@ def test_send_without_oauth(self, mock_post, mock_time,
oauth_secret=None,
)

mock_response = mock_post.return_value
mock_conn = mock_HTTPConnection.return_value
mock_request = mock_conn.request
mock_response = mock_conn.getresponse.return_value

tjc = TreeherderJobCollection()

Expand All @@ -526,18 +536,24 @@ def test_send_without_oauth(self, mock_post, mock_time,

response = req.post(tjc)

self.assertEqual(mock_HTTPConnection.call_count, 1)
self.assertEqual(mock_HTTPConnection.call_args[0][0], host)
self.assertEqual(mock_response, response)
self.assertEqual(mock_post.call_count, 1)
self.assertEqual(mock_request.call_count, 1)

path, resp = mock_post.call_args
req.get_uri(tjc)

deserialized_data = json.loads(resp['data'])
method, path, data, header = mock_request.call_args[0]
self.assertEqual(method, "POST")

deserialized_data = json.loads(data)
self.assertEqual(
deserialized_data,
tjc.get_collection_data()
)

self.assertEqual(
resp['headers']['Content-Type'],
header['Content-Type'],
'application/json',
)

Expand Down
2 changes: 1 addition & 1 deletion treeherder/client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
license='MPL',
packages=['thclient'],
zip_safe=False,
install_requires=['oauth2', 'requests']
install_requires=['oauth2']
)
47 changes: 23 additions & 24 deletions treeherder/client/thclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals

import requests
import httplib
import logging
import json
import oauth2 as oauth
Expand Down Expand Up @@ -649,8 +649,7 @@ class TreeherderRequest(object):
Treeherder request object that manages test submission.
"""

PROTOCOLS = {'http', 'https'} # supported protocols
HEADERS = {'Content-Type': 'application/json'}
protocols = set(['http', 'https']) # supported protocols

def __init__(
self, protocol='', host='', project='', oauth_key='',
Expand All @@ -671,10 +670,10 @@ def __init__(
self.oauth_client = OauthClient(oauth_key, oauth_secret,
self.project)

if protocol not in self.PROTOCOLS:
if protocol not in self.protocols:
raise AssertionError('Protocol "%s" not supported; please use one '
'of %s' % (protocol,
', '.join(self.PROTOCOLS)))
', '.join(self.protocols)))
self.protocol = protocol
self.timeout = timeout

Expand Down Expand Up @@ -724,10 +723,18 @@ def send(self, endpoint, method=None, data=None):
:param endpoint: the target endpoint for this request
:param method: can be one of GET,POST,PUT
:param data: the body of this request
:returns: a requests Response object
:returns: an httplib Response object
"""

req_method = self._get_requests_method(method)
if method not in ("GET", "POST", "PUT"):
msg = "{0}: {1} is not a supported method".format(
self.__class__.__name__,
method
)
raise TreeherderClientError(msg, [])

# Build the header
headers = {'Content-Type': 'application/json'}

if data:
if not isinstance(data, str):
Expand All @@ -741,27 +748,19 @@ def send(self, endpoint, method=None, data=None):
uri = self.get_uri(endpoint)

if self.use_oauth:
uri = self.oauth_client.get_signed_uri(serialized_body, uri, method)
uri = self.oauth_client.get_signed_uri(serialized_body, uri,
method)

# Make the request
response = req_method(uri, data=serialized_body, headers=self.HEADERS)
conn = None
if self.protocol == 'http':
conn = httplib.HTTPConnection(self.host, timeout=self.timeout)
else:
conn = httplib.HTTPSConnection(self.host, timeout=self.timeout)

return response
conn.request(method, uri, serialized_body, headers)

def _get_requests_method(self, method):
try:
methods = {
"GET": requests.get,
"POST": requests.post,
"PUT": requests.put
}
return methods[method]
except KeyError:
msg = "{0}: {1} is not a supported method".format(
self.__class__.__name__,
method
)
raise TreeherderClientError(msg, [])
return conn.getresponse()

def get_uri(self, endpoint):

Expand Down

0 comments on commit efa98c7

Please sign in to comment.