Skip to content
Browse files

Merge pull request #23 from k0s/22

  • Loading branch information...
2 parents 1a4d0d2 + 1a85cfd commit 420602ed5d4f053897f02c8ed23641b34dfa533d @malini malini committed Aug 10, 2012
Showing with 61 additions and 17 deletions.
  1. +26 −17 dzclient/client.py
  2. +35 −0 dzclient/tests/test_datazilla_request.py
View
43 dzclient/client.py
@@ -201,37 +201,46 @@ def send(self, dataset):
user = self.project
params = {
- 'oauth_version': "1.0",
- 'oauth_nonce': oauth.generate_nonce(),
- 'oauth_timestamp': int(time.time()),
- 'user': user,
'data': urllib.quote(json.dumps(dataset)),
}
- #There is no requirement for the token in two-legged
- #OAuth but we still need the token object.
- token = oauth.Token(key="", secret="")
- consumer = oauth.Consumer(key=self.oauth_key, secret=self.oauth_secret)
+ use_oauth = bool(self.oauth_key and self.oauth_secret)
+ if use_oauth:
- params['oauth_token'] = token.key
- params['oauth_consumer_key'] = consumer.key
+ params.update({'user': user,
+ 'oauth_version': "1.0",
+ 'oauth_nonce': oauth.generate_nonce(),
+ 'oauth_timestamp': int(time.time())})
- req = oauth.Request(method="POST", url=uri, parameters=params)
- #Set the signature
- signature_method = oauth.SignatureMethod_HMAC_SHA1()
+ # There is no requirement for the token in two-legged
+ # OAuth but we still need the token object.
+ token = oauth.Token(key="", secret="")
+ consumer = oauth.Consumer(key=self.oauth_key, secret=self.oauth_secret)
- #Sign the request
- req.sign_request(signature_method, consumer, token)
+ params['oauth_token'] = token.key
+ params['oauth_consumer_key'] = consumer.key
- #Build the header
+ req = oauth.Request(method="POST", url=uri, parameters=params)
+
+ # Set the signature
+ signature_method = oauth.SignatureMethod_HMAC_SHA1()
+
+ # Sign the request
+ req.sign_request(signature_method, consumer, token)
+ body = req.to_postdata()
+ else:
+ body = urllib.urlencode(params)
+
+ # Build the header
header = {'Content-type': 'application/x-www-form-urlencoded'}
+ # Make the POST request
conn = None
if self.protocol == 'http':
conn = httplib.HTTPConnection(self.host)
else:
conn = httplib.HTTPSConnection(self.host)
- conn.request("POST", path, req.to_postdata(), header)
+ conn.request("POST", path, body, header)
return conn.getresponse()
View
35 dzclient/tests/test_datazilla_request.py
@@ -207,5 +207,40 @@ def test_send(self,
'application/x-www-form-urlencoded',
)
+ @patch("dzclient.client.httplib.HTTPConnection")
+ def test_send_without_oauth(self, mock_HTTPConnection):
+ """Can send data to the server without oauth"""
+
+ protocol = 'http'
+ host = "datazilla.mozilla.org"
+ project = "project"
+ key = None
+ secret = None
+ req = DatazillaRequest(protocol, host, project, key, secret)
+
+ mock_conn = mock_HTTPConnection.return_value
+ mock_request = mock_conn.request
+ mock_response = mock_conn.getresponse.return_value
+
+ response = req.send({"some": "data"})
+
+ self.assertEqual(mock_HTTPConnection.call_count, 1)
+ self.assertEqual(mock_HTTPConnection.call_args[0][0], host)
+
+ self.assertEqual(mock_response, response)
+
+ self.assertEqual(mock_request.call_count, 1)
+
+ method, path, data, header = mock_request.call_args[0]
+ self.assertEqual(method, "POST")
+ self.assertEqual(path, "/project/api/load_test")
+ self.assertEqual(data, 'data=%257B%2522some%2522%253A%2520%2522data%2522%257D')
+
+ self.assertEqual(
+ header['Content-type'],
+ 'application/x-www-form-urlencoded',
+ )
+
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 420602e

Please sign in to comment.
Something went wrong with that request. Please try again.