Permalink
Browse files

swapped out httplib2 for requests in rest_client

Had to update test_create_headers() as it used to
be doing Auth. Eventually create_headers() will die.
But that is out of scope for this patch.

Removed httplib2 from required libs in setup.py

Change-Id: Ia9ae858ba6524221a0e2053110bf18ee9175ec7c
Reviewed-on: http://review.couchbase.org/19427
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>
Tested-by: Benjamin Young <benjamin@couchbase.com>
  • Loading branch information...
1 parent f64007c commit c84a4c2f0c71075c9e01a4ba14ef2619c91bfc29 @BigBlueHat BigBlueHat committed with Benjamin Young Aug 9, 2012
Showing with 39 additions and 28 deletions.
  1. +7 −1 couchbase/client.py
  2. +29 −22 couchbase/rest_client.py
  3. +2 −4 couchbase/tests/test_restclient.py
  4. +1 −1 setup.py
View
@@ -25,8 +25,12 @@
import requests
+from couchbase.logger import logger
from couchbase.rest_client import RestConnection
from couchbase.couchbaseclient import CouchbaseClient
+from couchbase.exception import BucketCreationException
+
+log = logger("client")
class Couchbase(object):
@@ -129,6 +133,7 @@ def create(self, bucket_name, bucket_password='', ram_quota_mb=100,
saslPassword=bucket_password,
replicaNumber=replica,
bucketType='membase')
+
ip, port, _, _ = self._rest_info()
while True:
@@ -139,9 +144,10 @@ def create(self, bucket_name, bucket_password='', ram_quota_mb=100,
(ip, port, bucket_name),
method='GET', params='',
headers=None, timeout=120)
+ content = json.loads(content)
except ValueError:
pass
- if json.loads(content)['basicStats']['quotaPercentUsed'] > 0.0:
+ if content['basicStats']['quotaPercentUsed'] > 0.0:
time.sleep(2)
break
time.sleep(1)
@@ -23,7 +23,6 @@
import warnings
import requests
-import httplib2
from couchbase.logger import logger
from couchbase.exception import ServerAlreadyJoinedException, \
@@ -279,10 +278,7 @@ def _create_headers(self):
if self.username == "default":
return {'Content-Type': 'application/json', 'Accept': '*/*'}
else:
- authorization = base64.encodestring('%s:%s' % (self.username,
- self.password))
return {'Content-Type': 'application/x-www-form-urlencoded',
- 'Authorization': 'Basic %s' % authorization,
'Accept': '*/*'}
def _http_request(self, api, method='GET', params='', headers=None,
@@ -292,33 +288,43 @@ def _http_request(self, api, method='GET', params='', headers=None,
end_time = time.time() + timeout
while True:
try:
- response, content = httplib2.Http().request(api, method,
- params, headers)
- if response['status'] in ['200', '201', '202']:
- return True, content
+ if method == 'GET':
+ r = requests.get(api, headers=headers, params=params,
+ auth=(self.username, self.password))
+ elif method in ['POST', 'PUT']:
+ r = getattr(requests, method.lower())(api,
+ headers=headers,
+ data=params,
+ auth=(self.username,
+ self.password))
+ elif method in ['DELETE']:
+ r = requests.delete(api, headers=headers,
+ auth=(self.username, self.password))
+
+ if r.status_code in [200, 201, 202]:
+ return True, r.content
else:
- try:
- json_parsed = json.loads(content)
- except:
- json_parsed = {}
reason = "unknown"
status = False
- if "error" in json_parsed:
- reason = json_parsed["error"]
- status = reason
- elif "errors" in json_parsed:
+ if r.json is None:
+ reason = r.text
+ status = False
+ elif "error" in r.json:
+ reason = r.json["error"]
+ status = False
+ elif "errors" in r.json:
errors = [error for _, error in
- json_parsed["errors"].iteritems()]
+ r.json["errors"].iteritems()]
reason = ", ".join(errors)
- status = reason
+ status = False
log.error('%s error %s reason: %s %s' %
- (api, response['status'], reason, content))
- return status, content
+ (api, r.status_code, reason, r.content))
+ return status, r.content
except socket.error, e:
log.error(e)
if time.time() > end_time:
raise ServerUnavailableException(ip=self.ip)
- except httplib2.ServerNotFoundError, e:
+ except requests.ConnectionError, e:
log.error(e)
if time.time() > end_time:
raise ServerUnavailableException(ip=self.ip)
@@ -751,7 +757,8 @@ def create_bucket(self, bucket,
log.warn(error)
else:
log.error(error)
- raise Exception(error)
+ raise BucketCreationException(self.ip, self.username,
+ error)
status, content = self._http_request(api, 'POST',
urllib.urlencode(params))
@@ -215,10 +215,8 @@ def test_view_results(self):
def test_create_headers(self):
self.setup_rest_connection()
headers = self.rest._create_headers()
- self.assertEqual(headers['Authorization'],
- 'Basic ' + base64.encodestring("%s:%s" %
- (self.rest.username,
- self.rest.password)))
+ self.assertEqual(headers['Content-Type'],
+ 'application/x-www-form-urlencoded')
@attr(cbv="1.0.0")
def test_create_bucket(self):
View
@@ -34,7 +34,7 @@
author="Couchbase, Inc.",
author_email="info@couchbase.com",
packages=["couchbase", "couchbase/utils", "couchbase/migrator"],
- install_requires=["httplib2", "requests", "unittest2", "uuid"],
+ install_requires=["requests", "unittest2", "uuid"],
setup_requires=["nose>=1.0"],
tests_require=["nose-testconfig"],
url="http://www.couchbase.com/develop/python/next",

0 comments on commit c84a4c2

Please sign in to comment.