Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
 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))
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 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
Exemplo n.º 10
0
    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")
Exemplo n.º 11
0
 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()