def _check_response(response): if response.status_code not in [200, 201, 202]: if response.status_code == 401: raise errors.AuthenticationError( "Access unauthorized - update access token.") json_res = response.json() raise errors.ExportError( "HTTP {} - Cannot export dataset: {}".format( response.status_code, json_res["message"] if "message" in json_res else json_res["status"]))
def _check_response(response): if response.status_code not in [200, 201, 202]: if response.status_code == 401: raise errors.AuthenticationError( 'Access unauthorized - update access token.') json_res = response.json() raise errors.ExportError( 'HTTP {} - Cannot export dataset: {}'.format( response.status_code, json_res['message'] if 'message' in json_res else json_res['status']))
def _post(self, url, json=None, data=None, files=None): headers = {'X-Dataverse-key': self.access_token} try: with retry() as session: return session.post(url=url, json=json, data=data, files=files, headers=headers) except requests.exceptions.RequestException as e: raise errors.ExportError('Cannot POST to remote server.') from e
def check_or_raise(response): """Check for expected response status code.""" if response.status_code not in [200, 201, 202]: if response.status_code == 401: raise errors.AuthenticationError( "Access unauthorized - update access token.") if response.status_code == 400: err_response = response.json() messages = [ '"{0}" failed with "{1}"'.format(err["field"], err["message"]) for err in err_response["errors"] ] raise errors.ExportError( "\n" + "\n".join(messages) + "\nSee `renku dataset edit -h` for details on how to edit" " metadata") else: raise errors.ExportError(response.content)
def check_or_raise(response): """Check for expected response status code.""" if response.status_code not in [200, 201, 202]: if response.status_code == 401: raise errors.AuthenticationError( 'Access unauthorized - update access token.') if response.status_code == 400: err_response = response.json() messages = [ '"{0}" failed with "{1}"'.format(err['field'], err['message']) for err in err_response['errors'] ] raise errors.ExportError( '\n' + '\n'.join(messages) + '\nSee `renku dataset edit -h` for details on how to edit' ' metadata') else: raise errors.ExportError(response.content)
def publish_dataset(self): """Publish a previously-created dataset.""" if self.dataset_pid is None: raise errors.ExportError('Dataset not created.') url = self._make_url(self.DATASET_PUBLISH_PATH, persistentId=self.dataset_pid, type='major') response = self._post(url=url) self._check_response(response) return response
def upload_file(self, full_path, path_in_dataset): """Upload a file to a previously-created dataset.""" if self.dataset_pid is None: raise errors.ExportError('Dataset not created.') url = self._make_url(self.FILE_UPLOAD_PATH, persistentId=self.dataset_pid) params = {'directoryLabel': str(path_in_dataset.parent)} data = dict(jsonData=json.dumps(params)) files = {'file': (path_in_dataset.name, open(full_path, 'rb'))} response = self._post(url=url, data=data, files=files) self._check_response(response) return response