Пример #1
0
    def upload_file(self, file_path, folder_path=None):
        """
        Upload a file from the given file path into the user's folder or sub-folder
        :param file_path: the path of the file locally where it can be accessed for upload
        :param folder_path: the path of the sub folders in the user's folder on the server
                            where the file will be uploaded.
                            It should be in the format of 'folder1/folder2/folder3'.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + UPLOAD_FILE

        if folder_path:
            url = '/'.join([url, folder_path])

        headers = self.get_basic_headers()

        with open(file_path, "rb") as a_file:
            file_dict = [('files', (os.path.basename(file_path), a_file))]

            response = ResponseUtils.handle_response(
                requests.post(url, headers=headers, files=file_dict))

        return response
Пример #2
0
    def get_submission(self, accession_id):
        """
        Get all the metadata information of a specific submission given by the accession id parameter.
        :param accession_id: accession id of the queried submission.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + GET_SUBMISSION_BY_ACCESSION_ID.format(
            accession_id=accession_id)
        headers = self.get_basic_headers()

        response = ResponseUtils.handle_response(
            requests.get(url, headers=headers))

        return response
Пример #3
0
    def delete_submission(self, accession_id):
        """
        Delete a specific submission from the BioStudies archive given by the accession id parameter.
        :param accession_id: accession id of the queried submission.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + DELETE_SUBMISSION.format(
            accession_id=accession_id)
        headers = self.get_basic_headers()

        response = ResponseUtils.handle_response(
            requests.delete(url, headers=headers))

        return response
Пример #4
0
    def create_user_sub_folder(self, folder_name):
        """
        Create a folder in the user's directory.
        The name of the folder could be a single folder
        or a deeper folder structure like: 'folder1/folder2/folder3'.
        :param folder_name: the name of the folder or folder structure to be create for the user
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + CREATE_FOLDER.format(folder_name=folder_name)

        headers = self.get_basic_headers()
        response = ResponseUtils.handle_response(
            requests.post(url, headers=headers))

        return response
Пример #5
0
    def delete_file(self, file_name):
        """
        Delete a file/folder with the given file/folder name from the user's root folder or sub-folder path.
        :param file_name:   the name of the file or folder to be deleted.
                            If it is a folder that needs to be deleted,
                            then the parameter's value should be in the format of 'folder1/folder2/folder3'.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + DELETE_FILE.format(file_name=file_name)
        headers = self.get_basic_headers()

        response = ResponseUtils.handle_response(
            requests.delete(url, headers=headers))

        return response
Пример #6
0
    def create_submission(self, metadata):
        """
        Create and submit a submission with the given metadata.
        In the metadata the user can include a list of files, too.
        :param metadata: Contains all the metadata belongs to a submission.
        The metadata optionally can contain information of files that belongs to this submission.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + CREATE_SUBMISSION
        headers = self.get_basic_headers()
        headers.update({'Submission_Type': 'application/json'})

        response = ResponseUtils.handle_response(
            requests.post(url, headers=headers, json=metadata))

        return response
Пример #7
0
    def login(self, username=None, password=None):
        """
        This method tries to send a login request with the configured credentials
        to the BioStudies REST API.
        The URL to send the login request is defined during initialisation.
        The method checks the returned status code from BioStudies REST API.
        In case it is 200 OK, then parse the response and gets the session id from it.
        Otherwise it gets the error message from the response.
        :return: Response from BioStudies API with the session id or the error message included
        :rtype biostudiesclient.auth.AuthResponse
        """

        self.__set_credentials(username, password)

        response = ResponseUtils.handle_response(requests.post(self.login_url, json=self.__login_payload()))

        auth_response = AuthResponse(status=HTTPStatus(response.status))

        self.session_id = response.json["sessid"]
        auth_response.session_id = self.session_id

        return auth_response
Пример #8
0
    def get_user_files(self, folder_path=None):
        """
        Get the list of files and folders from the user's root directory
        if the folder_path parameter is empty,
        otherwise return the contents of the user's sub folder structure defined by
        the value of the given parameter.

        :param folder_path: the path of the sub folders in the user's folder on the server.
                            It should be in the format of 'folder1/folder2/folder3'.
        :return: Response from BioStudies API
        :rtype biostudiesclient.response_utils.ResponseObject
        """

        url = self.base_url + GET_USER_FILES

        if folder_path:
            url = '/'.join([url, folder_path])

        headers = self.get_basic_headers()

        response = ResponseUtils.handle_response(
            requests.get(url, headers=headers))

        return response
Пример #9
0
 def setUp(self) -> None:
     self.response_utils = ResponseUtils()
     self.valid_session_id = "123.456.789"
Пример #10
0
class TestResponseUtils(unittest.TestCase):
    def setUp(self) -> None:
        self.response_utils = ResponseUtils()
        self.valid_session_id = "123.456.789"

    def test_when_processing_ok_response_return_response_object_without_error(
            self):
        input_response = Mock(spec=Response)
        input_response.status_code = HTTPStatus.OK
        response_text = self.correct_response_text_with_session_id()
        input_response.json.return_value = response_text
        input_response.text = response_text

        output_response = self.response_utils.handle_response(input_response)

        self.assertEqual(output_response.status, HTTPStatus.OK)
        self.assertEqual(output_response.json, response_text)

    def test_when_processing_plain_error_message_returns_parsed_message(self):
        input_response = Mock(spec=Response)
        input_response.status_code = HTTPStatus.REQUEST_TIMEOUT
        error_message: str = '{"message":"Something went wrong."}'
        input_response.json.return_value = json.loads(error_message)
        input_response.text = error_message

        assert_that(self.response_utils.handle_response)\
            .raises(RestErrorException)\
            .when_called_with(input_response)\
            .starts_with("('Something")\
            .is_equal_to("('Something went wrong.', <HTTPStatus.REQUEST_TIMEOUT: 408>)")

    def test_when_processing_detailed_error_message_returns_parsed_message(
            self):
        input_response = Mock(spec=Response)
        input_response.status_code = HTTPStatus.BAD_REQUEST
        a_detailed_error_message = 'A detailed error message'
        error_message: dict = {
            "status": "FAIL",
            "log": {
                "level": "ERROR",
                "message": a_detailed_error_message,
                "subnodes": []
            }
        }
        input_response.json.return_value = error_message
        input_response.text = json.dumps(error_message)

        assert_that(self.response_utils.handle_response) \
            .raises(RestErrorException) \
            .when_called_with(input_response) \
            .starts_with("('A detailed") \
            .is_equal_to("('A detailed error message', <HTTPStatus.BAD_REQUEST: 400>)")

    def correct_response_text_with_session_id(self):
        return {
            "sessid": self.valid_session_id,
            "email": "*****@*****.**",
            "username": "******",
            "secret": "aa/aabbcc-1122-4785-a875-cfade60fbb7c-a11",
            "fullname": "Test Full Name",
            "superuser": False,
            "allow": ["Public"],
            "deny": [],
            "aux": {
                "orcid": ""
            }
        }