def delete(self): # Delete from data api result = self.client.deleteHelper(self.url) if 'error' in result: raise ModelApiError(result['error']['message']) else: return True
def update_permissions(self, acl): params = {'acl': acl.to_api_param()} response = self.client.patchHelper(self.url, params) if response.status_code != 200: raise ModelApiError('Unable to update permissions: ' + response.json()['error']['message']) return True
def _get_directory_iterator(self, type_filter=None): marker = None first = True while first or (marker is not None and len(marker) > 0): first = False url = self.url query_params = {} if marker: query_params['marker'] = marker response = self.client.getHelper(url, **query_params) if response.status_code != 200: raise ModelApiError("Directory iteration failed: " + str(response.content)) responseContent = response.content if isinstance(responseContent, six.binary_type): responseContent = responseContent.decode() content = json.loads(responseContent) if 'marker' in content: marker = content['marker'] else: marker = None if type_filter is ModelObjectType.directory or type_filter is None: for d in self._iterate_directories(content): yield d if type_filter is ModelObjectType.file or type_filter is None: for f in self._iterate_files(content): yield f
def putJson(self, data): # Post to data api jsonElement = json.dumps(data) result = self.client.putHelper(self.url, jsonElement) if 'error' in result: raise ModelApiError(result['error']['message']) else: return self
def putFile(self, path): # Post file to data api with open(path, 'rb') as f: result = self.client.putHelper(self.url, f) if 'error' in result: raise ModelApiError(result['error']['message']) else: return self
def delete(self, force=False): # Delete from data api url = self.url if force: url += '?force=true' result = self.client.deleteHelper(url) if 'error' in result: raise ModelApiError(result['error']['message']) else: return True
def create(self, acl=None): '''Creates a directory, optionally include Acl argument to set permissions''' parent, name = getParentAndBase(self.path) json = {'name': name} if acl is not None: json['acl'] = acl.to_api_param() response = self.client.postJsonHelper(ModelDirectory._getUrl(parent), json, False) if (response.status_code != 200): raise ModelApiError("Directory creation failed: " + str(response.content))
def getFile(self): exists, error = self.existsWithError() if not exists: raise ModelApiError('unable to get file {} - {}'.format(self.path, error)) # Make HTTP get request response = self.client.getHelper(self.url) with tempfile.NamedTemporaryFile(delete = False) as f: for block in response.iter_content(1024): if not block: break; f.write(block) f.flush() return open(f.name)
def get_permissions(self): ''' Returns permissions for this directory or None if it's a special collection such as .session or .algo ''' response = self.client.getHelper(self.url, acl='true') if response.status_code != 200: raise ModelApiError('Unable to get permissions:' + str(response.content)) content = response.json() if 'acl' in content: return Acl.from_acl_response(content['acl']) else: return None
def put(self, data): # Post to data api # First turn the data to bytes if we can if isinstance(data, six.string_types) and not isinstance(data, six.binary_type): data = bytes(data.encode()) if isinstance(data, six.binary_type): result = self.client.putHelper(self.url, data) if 'error' in result: raise ModelApiError(result['error']['message']) else: return self else: raise TypeError("Must put strings or binary data. Use putJson instead")
def getJson(self): exists, error = self.existsWithError() if not exists: raise ModelApiError('unable to get file {} - {}'.format(self.path, error)) # Make HTTP get request return self.client.getHelper(self.url).json()