def refresh(self, obj): """ retrieve the object from cloud, and update its contents. This method returns nothing. """ (code, body) = self.context.newClient().get( url='%s/apps/%s/%s' % (self.context.url, self.context.app_id, obj.getPath()), context=self.context) if code >= 300: raise kii.CloudException(code, body) obj.update(body)
def query(self, bucket, condition): client = self.context.newClient() client.method = "POST" client.url = "%s/apps/%s/%s/%s" % (self.context.url, self.context.app_id, bucket.getPath(), "query") client.setContentType('application/vnd.kii.QueryRequest+json') client.setKiiHeaders(self.context, True) (code, body) = client.send(condition) if code != 200: raise kii.CloudException(code, body) nextKey = body.get("nextPaginationKey") condition.setPaginationKey(nextKey) return [kiiobject.KiiObject(bucket, o["_id"], **o) for o in body["results"]]
def downloadBody(self, obj, outFile): url = '%s/apps/%s%s/body' % (self.context.url, self.context.app_id, obj.getPath()) client = self.context.newClient() client.method = "GET" client.url = url client.setKiiHeaders(self.context, True) (code, body) = client.sendForDownload(outFile) if code == 200: return raise kii.CloudException(code, body)
def updateBody(self, obj, type, data, size): url = '%s/apps/%s%s/body' % (self.context.url, self.context.app_id, obj.getPath()) client = self.context.newClient() client.method = "PUT" client.url = url client.setContentType(type) client.setKiiHeaders(self.context, True) (code, body) = client.sendFile(data, size) if code == 200: return if code == 201: return raise kii.CloudException(code, body)
def save(self, obj): """ save the passed object and update the modification time. This meethod returns nothing. """ url = '%s/apps/%s/%s' % (self.context.url, self.context.app_id, obj.getPath()) client = self.context.newClient() client.method = "PUT" client.url = url client.setContentType('application/json') client.setKiiHeaders(self.context, True) (code, body) = client.send(obj) if code >= 300: raise kii.CloudException(code, body) udpated = copy.copy(obj) obj['_modified'] = body['modifiedAt']
def create(self, bucket, **data): url = '%s/apps/%s/%s/objects' % (self.context.url, self.context.app_id, bucket.getPath()) client = self.context.newClient() client.method = "POST" client.url = url client.setContentType('application/json') client.setKiiHeaders(self.context, True) (code, body) = client.send(data) if code != 201: raise kii.CloudException(code, body) id = body['objectID'] created = body['createdAt'] datatype = body['dataType'] return KiiObject(bucket, id, _id=id, _modified=created, _created=created, _dataType=datatype, **data)
def delete(self, obj): (code, body) = self.context.newClient().delete( url='%s/apps/%s/%s' % (self.context.url, self.context.app_id, obj.getPath()), context=self.context) if code >= 300: raise kii.CloudException(code, body)