def api_status(self): resp = self.make_api_request('nodes?name=%s' % self.name) data = loads(resp.read()) if not data: raise NotRegistered("Unable to find node entry for name '%s'" % self.name) elif len(data) > 1: raise ServiceUnavailable("Duplicate node entry for name '%s'" % self.name) else: node_resp = self.make_api_request('nodes', data[0]['id']) node = loads(node_resp.read()) return node
def api_status(self): resp = self.make_api_request('nodes?name=%s' % self.name) data = loads(resp.read()) if not data: raise NotRegistered( "Unable to find node entry for name '%s'" % self.name) elif len(data) > 1: raise ServiceUnavailable( "Duplicate node entry for name '%s'" % self.name) else: node_resp = self.make_api_request('nodes', data[0]['id']) node = loads(node_resp.read()) return node
def _register(self, node_id=None, data=None): if node_id: path = 'nodes/%s' % node_id else: path = 'nodes' if not data: data = self._local_info() data['status'] = 'PENDING' resp = self.make_api_request(path, method='POST', data=data) return loads(resp.read())
def node_request(self, node, method, path, **kwargs): url = "http://%s:%s%s" % (node.hostname, node.port, path) headers = {"X-Request-Id": getattr(logger.local, "request_id", "lunr-%s" % uuid4())} if method in ("GET", "HEAD", "DELETE"): url += "?" + urlencode(kwargs) req = Request(url, urlencode(kwargs), headers=headers) req.get_method = lambda *args, **kwargs: method try: resp = urlopen(req, timeout=self.app.node_timeout) logger.debug("%s on %s succeeded with %s" % (req.get_method(), req.get_full_url(), resp.getcode())) return loads(resp.read()) except (socket.timeout, urllib2.HTTPError, urllib2.URLError, HTTPException), e: raise NodeError(req, e)
def __init__(self, req, e): self.method = req.get_method() self.url = req.get_full_url() detail = "%s on %s " % (self.method, self.url) if type(e) is socket.timeout: detail += "failed with socket timeout" self.reason = detail if type(e) is urllib2.HTTPError: raw_body = ''.join(e.fp.read()) self.reason = raw_body # most basic reason try: body = loads(raw_body) except ValueError: pass else: # json body has more info if 'reason' in body: self.reason = body['reason'] elif 'message' in body: self.reason = body['message'] detail += "returned '%s' with '%s'" % (e.code, self.reason) self.title = e.msg self.code = e.code if type(e) is urllib2.URLError: detail += "failed with '%s'" % e.reason self.reason = e.reason if type(e) is IOError: detail += "failed with '%s'" % e self.reason = str(e) if isinstance(e, HTTPException): # work around urllib2 bug, it throws a # BadStatusLine without an explaination. if isinstance(e, BadStatusLine): detail += "failed with '%s'" % e.__class__.__name__ else: detail += "failed with '%s'" % e self.reason = str(e) super(HTTPClientError, self).__init__(detail=detail)
def node_request(self, node, method, path, **kwargs): url = 'http://%s:%s%s' % (node.hostname, node.port, path) headers = {'X-Request-Id': getattr(logger.local, 'request_id', 'lunr-%s' % uuid4())} if method in ('GET', 'HEAD', 'DELETE'): url += '?' + urlencode(kwargs) req = Request(url, urlencode(kwargs), headers=headers) req.get_method = lambda *args, **kwargs: method try: resp = urlopen(req, timeout=self.app.node_timeout) logger.debug( "%s on %s succeeded with %s" % (req.get_method(), req.get_full_url(), resp.getcode())) return loads(resp.read()) except (socket.timeout, urllib2.HTTPError, urllib2.URLError, HTTPException), e: raise NodeError(req, e)
try: resp = urllib2.urlopen(req) except urllib2.HTTPError, e: resp = e except urllib2.URLError: attempts += 1 if attempts > 3: raise # exponential backoff sleep(2 ** attempts) continue break try: # read in the entire response body = loads(''.join(resp.readlines())) return Struct(code=resp.code, body=body, reason=resp.msg) except urllib2.URLError, e: sys.stderr.write('Caught URLError - %s (%s)' % (resp.code, e)) def unlink(self, file): try: os.unlink(file) except OSError: pass def assertCode(self, resp, code, action=''): msg = "expected code '%s' got '%s' : %s"\ % (code, resp.code, resp.body) if action: msg += " when trying to %s" % action