Esempio n. 1
0
    def get_directory(self, dir_identifier):
        """
        Get a specific directory with directory's id

        :param dir_identifier:
        :return:
        """
        req_url = self.METAX_GET_DIRECTORY.format(dir_identifier)

        try:
            metax_qvain_api_response = requests.get(
                req_url,
                headers={'Accept': 'application/json'},
                auth=(self.user, self.pw),
                verify=self.verify_ssl,
                timeout=10)
            metax_qvain_api_response.raise_for_status()
        except Exception as e:
            if isinstance(e, requests.HTTPError):
                log.debug(
                    "Failed to get data for directory {0} from Metax API".
                    format(dir_identifier))
                log.debug('Response status code: {0}'.format(
                    metax_qvain_api_response.status_code))
                log.debug('Response text: {0}'.format(
                    json_or_empty(metax_qvain_api_response)
                    or metax_qvain_api_response.text))
            else:
                log.error(
                    "Failed to get data for directory {0} from Metax API".
                    format(dir_identifier))
                log.error(e)
            return None

        return metax_qvain_api_response.json()
Esempio n. 2
0
    def get_catalog_record_with_file_details(self, identifier):
        """
        Get a catalog record with a given identifier from MetaX API.

        :return: Metax catalog record as json
        """
        try:
            metax_api_response = requests.get(
                self.METAX_GET_CATALOG_RECORD_WITH_FILE_DETAILS_URL.format(
                    identifier),
                headers={'Accept': 'application/json'},
                auth=(self.user, self.pw),
                verify=self.verify_ssl,
                timeout=3)
            metax_api_response.raise_for_status()
        except Exception as e:
            if isinstance(e, requests.HTTPError):
                log.debug(
                    "Failed to get catalog record {0} from Metax API".format(
                        identifier))
                log.debug('Response status code: {0}'.format(
                    metax_api_response.status_code))
                log.debug('Response text: {0}'.format(
                    json_or_empty(metax_api_response)
                    or metax_api_response.text))
            else:
                log.error(
                    "Failed to get catalog record {0} from Metax API".format(
                        identifier))
                log.error(e)
            return None
        return metax_api_response.json()
Esempio n. 3
0
    def create_dataset(self, data):
        """
        Send the data from the frontend to Metax.

        Arguments:
            data {object} -- Object with the dataset data that has been validated and converted to comply with the Metax schema.

        Returns:
            [type] -- The response from Metax.

        """
        req_url = self.METAX_CREATE_DATASET
        headers = {'Accept': 'application/json'}
        try:
            metax_api_response = requests.post(req_url,
                                               json=data,
                                               headers=headers,
                                               auth=(self.user, self.pw),
                                               verify=self.verify_ssl,
                                               timeout=10)
        except Exception as e:
            if isinstance(e, requests.HTTPError):
                log.debug("Failed to create dataset.")
                log.debug('Response status code: {0}'.format(
                    metax_api_response.status_code))
                log.debug('Response text: {0}'.format(
                    json_or_empty(metax_api_response)
                    or metax_api_response.text))
            else:
                log.error(
                    "Failed to get data for directory {0} from Metax API")
                log.error(e)
            return {'Error_message': 'Error trying to send data to metax.'}

        return metax_api_response.json()
Esempio n. 4
0
    def get_rems_permission(self, user_id, rems_resource):
        """
        Get user entitlement for a rems resource.

        :param user_id:
        :param rems_resource:
        :return:
        """
        if not user_id or not rems_resource:
            return False
        try:
            rems_api_response = requests.get(
                self.REMS_URL.format(rems_resource),
                headers={
                    'Accept': 'application/json',
                    'x-rems-api-key': self.API_KEY,
                    'x-rems-user-id': user_id
                },
                verify=False,
                timeout=3)
            rems_api_response.raise_for_status()
        except Exception as e:
            log.error(
                'Failed to get entitlement data from Fairdata REMS for user_id: {0}, resource: {1}'
                .format(user_id, rems_resource))
            if isinstance(e, requests.HTTPError):
                log.warning('Response status code: {0}'.format(
                    rems_api_response.status_code))
                log.warning('Response text: {0}'.format(
                    json_or_empty(rems_api_response)))
            else:
                log.error(e)
            return False

        return len(rems_api_response.json()) > 0
    def download(self, cr_id, file_ids, dir_ids):
        """
        Download files from Download API.

        :param cr_id:
        :param file_ids:
        :param dir_ids:
        :return:
        """
        if self.is_testing:
            return self._get_error_response(200)

        url = self._create_url(cr_id, file_ids, dir_ids)
        try:
            dl_api_response = requests.get(
                url,
                stream=True,
                timeout=15,
                auth=(self.USER, self.PASSWORD.encode('utf-8')))
            dl_api_response.raise_for_status()
        except requests.Timeout as t:
            log.error('Request to Download API timed out')
            log.error(t)
            return self._get_error_response(200)
        except requests.ConnectionError as c:
            log.error('Unable to connect to Download API')
            log.error(c)
            return self._get_error_response(200)
        except requests.HTTPError:
            log.warning(
                'Download API returned an unsuccessful status code: {0}'.
                format(dl_api_response.status_code))
            log.warning('Response: {0}'.format(json_or_empty(dl_api_response)))
            return self._get_error_response(200)
        except Exception as e:
            log.error(e)
            return self._get_error_response(200)
        else:
            response = Response(response=stream_with_context(
                dl_api_response.iter_content(chunk_size=1024)),
                                status=dl_api_response.status_code)

            if 'Content-Type' in dl_api_response.headers:
                response.headers['Content-Type'] = dl_api_response.headers[
                    'Content-Type']
            if 'Content-Disposition' in dl_api_response.headers:
                response.headers[
                    'Content-Disposition'] = dl_api_response.headers[
                        'Content-Disposition']
            if 'Content-Length' in dl_api_response.headers:
                response.headers['Content-Length'] = dl_api_response.headers[
                    'Content-Length']

            return response
Esempio n. 6
0
    def get_directory_for_catalog_record(self, cr_identifier, dir_identifier,
                                         file_fields, directory_fields):
        """
        Get directory contents for a specific catalog record

        :param cr_identifier:
        :param dir_identifier:
        :param file_fields:
        :param directory_fields:
        :return:
        """
        req_url = self.METAX_GET_DIRECTORY_FOR_CR_URL.format(
            dir_identifier, cr_identifier)
        if file_fields:
            req_url = req_url + '&file_fields={0}'.format(file_fields)
        if directory_fields:
            req_url = req_url + '&directory_fields={0}'.format(
                directory_fields)

        try:
            metax_api_response = requests.get(
                req_url,
                headers={'Accept': 'application/json'},
                auth=(self.user, self.pw),
                verify=self.verify_ssl,
                timeout=10)
            metax_api_response.raise_for_status()
        except Exception as e:
            if isinstance(e, requests.HTTPError):
                log.debug(
                    "Failed to get data for directory {0} in catalog record {1} from Metax API"
                    .format(dir_identifier, cr_identifier))
                log.debug('Response status code: {0}'.format(
                    metax_api_response.status_code))
                log.debug('Response text: {0}'.format(
                    json_or_empty(metax_api_response)
                    or metax_api_response.text))
            else:
                log.error(
                    "Failed to get data for directory {0} in catalog record {1} from Metax API"
                    .format(dir_identifier, cr_identifier))
                log.error(e)
            return None

        return metax_api_response.json()
Esempio n. 7
0
    def get_datasets_for_user(self, user_id, limit, offset):
        """
        Get datasets created by the specified user. Uses pagination, so offset and limit are used as well.

        :param user_id:
        :return datasets:
        """
        req_url = self.METAX_GET_DATASETS_FOR_USER.format(user_id)

        if (limit):
            req_url = req_url + "&limit={0}".format(limit[0])
        if (offset):
            req_url = req_url + "&offset={}".format(offset[0])

        try:
            metax_api_response = requests.get(
                req_url,
                headers={'Accept': 'application/json'},
                auth=(self.user, self.pw),
                verify=self.verify_ssl,
                timeout=10)
            metax_api_response.raise_for_status()
        except Exception as e:
            if isinstance(e, requests.HTTPError):
                log.debug("Failed to get datasets for user {0} from Metax API".
                          format(user_id))
                log.debug('Response status code: {0}'.format(
                    metax_api_response.status_code))
                log.debug('Response text: {0}'.format(
                    json_or_empty(metax_api_response)
                    or metax_api_response.text))
            else:
                log.debug("Failed to get datasets for user {0} from Metax API".
                          format(user_id))
                log.error(e)
            return None

        return metax_api_response.json()