def _refresh_if_needed(self, request_ret): refresh_required = True if request_ret.status_code not in range(200, 300): try: message = json.loads(request_ret.text)[0] except Exception: message = request_ret.text raise ServerError(request_ret.status_code, message, result="Did not update {} record.".format( self.__class__.__name__)) else: try: message = request_ret.json() log.debug("Received response: %s" % message) if not isinstance(message, dict): raise ServerError( request_ret.status_code, message, result="Unknown error updating {0:s} record.".format( self.__class__.__name__)) else: if message.get("success", False): if isinstance(message.get(self.info_key, None), dict): self._info = message.get(self.info_key) self._full_init = True refresh_required = False else: if self._change_object_key_name in message.keys(): # if all we got back was an ID, try refreshing to get the entire record. log.debug( "Only received an ID back from the server, forcing a refresh" ) self._info[self.primary_key] = message[ self._change_object_key_name] refresh_required = True else: # "success" is False raise ServerError( request_ret.status_code, message.get("message", ""), result="Did not update {0:s} record.".format( self.__class__.__name__)) except Exception: pass self._dirty_attributes = {} if refresh_required: self.refresh() return self._model_unique_id
def stop(self): if self._is_deleted: raise ApiError("cannot stop a deleted query") url = self.urlobject_single.format(self._cb.credentials.org_key, self.id) + "/status" result = self._cb.put_object(url, {'status': 'CANCELLED'}) if (result.status_code == 200): try: self._info = result.json() self._last_refresh_time = time.time() return True except: raise ServerError(result.status_code, "Cannot parse response as JSON: {0:s}".format(result.content)) return False
def _raw_device_action(self, request): """ Invokes the API method for a device action. :param dict request: The request body to be passed as JSON to the API method. :return: The parsed JSON output from the request. :raises ServerError: If the API method returns an HTTP error code. """ url = "/appservices/v6/orgs/{0}/device_actions".format( self.credentials.org_key) resp = self.post_object(url, body=request) if resp.status_code == 200: return resp.json() elif resp.status_code == 204: return None else: raise ServerError(error_code=resp.status_code, message="Device action error: {0}".format( resp.content))
def _still_querying(self): if not self._query_token: self._submit() args = {"query_id": self._query_token} result = self._cb.get_object("/pscr/query/v1/status", query_parameters=args) if result.get("status_code") != 200: raise ServerError(result["status_code"], result["errorMessage"]) searchers_contacted = result.get("contacted", 0) searchers_completed = result.get("completed", 0) log.debug("contacted = {}, completed = {}".format(searchers_contacted, searchers_completed)) if searchers_contacted == 0: return True if searchers_completed < searchers_contacted: if self._timeout != 0 and (time.time() * 1000) - self._submit_time > self._timeout: self._timed_out = True return False return True return False