def raise_error(self, status_code, data): if status_code == UNAUTHORIZED: raise AuthError(self.uri) if status_code == FORBIDDEN: raise Forbidden(self.uri) if data: content = json_loads(data.decode('utf-8')) else: content = {} message = content.pop("message", "HTTP request to <%s> returned unexpected status code %s" % (self.uri, status_code)) error = GraphError(message, **content) error.http_status_code = status_code raise error
def sync(self): ref = self.ref # Some of the transactional URIs do not support empty statement # lists in versions earlier than 2.3. Which doesn't really matter # as it's a waste sending anything anyway. if ref in (self.autocommit_ref, self.begin_ref, self.transaction_ref) and not self._statements: return 0 count = 0 try: response = self.post(ref, {"statements": self._statements}, expected=(OK, CREATED)) if response.status == 201: location_path = urlsplit(response.headers["Location"]).path self.transaction_ref = "".join(location_path.rpartition("transaction")[1:]) self.commit_ref = "%s/commit" % self.transaction_ref self.ref = self.transaction_ref content = json_loads(response.data.decode("utf-8")) errors = content["errors"] if errors: from py2neo.database import GraphError raise GraphError.hydrate(errors[0]) for i, result_loader in enumerate(self._result_loaders): try: count += result_loader.load(content["results"][i]) except IndexError: result_loader.fail() return count finally: self._statements[:] = () self._result_loaders[:] = ()
def run(self, statement, parameters=None, tx=None, graph=None, keys=None, entities=None): hydrator = JSONHydrator(version="rest", graph=graph, keys=keys, entities=entities) r = self._post("/db/data/transaction/%s" % (tx or "commit"), statement, hydrator.dehydrate(parameters)) assert r.status == 200 # TODO: other codes try: raw_result = hydrator.hydrate_result(r.data.decode("utf-8")) except HydrationError as e: from py2neo.database import GraphError # TODO: breaks abstraction layers :( if tx is not None: self.transactions.remove(tx) raise GraphError.hydrate(e.args[0]) else: result = CypherResult({ "connection": self.connection_data, "fields": raw_result.get("columns"), "plan": raw_result.get("plan"), "stats": raw_result.get("stats"), }) hydrator.keys = result.keys() result.append_records( hydrator.hydrate(record[hydrator.version]) for record in raw_result["data"]) result.done() return result
def _fail(cls, metadata): from py2neo.database import GraphError raise GraphError.hydrate(metadata)
def set_failure(self, **metadata): self._status = 2 # TODO: tidy up where these errors live from py2neo.database import GraphError self._failure = GraphError.hydrate(metadata)
def audit(self): if self.errors(): from py2neo.database import GraphError failure = GraphError.hydrate(self.errors().pop(0)) raise failure