Browse Source

client: detect and give a more clear error when HTTP requests are nested

tags/nilmdb-1.1^2
Jim Paris 11 years ago
parent
commit
2f7365412d
1 changed files with 15 additions and 4 deletions
  1. +15
    -4
      nilmdb/client/httpclient.py

+ 15
- 4
nilmdb/client/httpclient.py View File

@@ -33,6 +33,17 @@ class HTTPClient(object):
self.curl.setopt(pycurl.URL, url)
self.url = url

def _check_busy_and_set_upload(self, upload):
"""Sets the pycurl.UPLOAD option, but also raises a more
friendly exception if the client is already serving a request."""
try:
self.curl.setopt(pycurl.UPLOAD, upload)
except pycurl.error as e:
if "is currently running" in str(e):
raise Exception("Client is already performing a request, and "
"nesting calls is not supported.")
raise

def _check_error(self, body = None):
code = self.curl.getinfo(pycurl.RESPONSE_CODE)
if code == 200:
@@ -156,12 +167,12 @@ class HTTPClient(object):

def get(self, url, params = None, retjson = True):
"""Simple GET"""
self.curl.setopt(pycurl.UPLOAD, 0)
self._check_busy_and_set_upload(0)
return self._doreq(url, params, retjson)

def put(self, url, postdata, params = None, retjson = True):
"""Simple PUT"""
self.curl.setopt(pycurl.UPLOAD, 1)
self._check_busy_and_set_upload(1)
self._setup_url(url, params)
data = cStringIO.StringIO(postdata)
self.curl.setopt(pycurl.READFUNCTION, data.read)
@@ -184,12 +195,12 @@ class HTTPClient(object):

def get_gen(self, url, params = None, retjson = True):
"""Simple GET, returning a generator"""
self.curl.setopt(pycurl.UPLOAD, 0)
self._check_busy_and_set_upload(0)
return self._doreq_gen(url, params, retjson)

def put_gen(self, url, postdata, params = None, retjson = True):
"""Simple PUT, returning a generator"""
self.curl.setopt(pycurl.UPLOAD, 1)
self._check_busy_and_set_upload(1)
self._setup_url(url, params)
data = cStringIO.StringIO(postdata)
self.curl.setopt(pycurl.READFUNCTION, data.read)


Loading…
Cancel
Save