Exemplo n.º 1
0
def check_dataverse_uri(url):
    """Check if an URL points to a dataverse instance."""
    url_parts = list(urlparse.urlparse(url))
    url_parts[2] = pathlib.posixpath.join(DATAVERSE_API_PATH,
                                          DATAVERSE_VERSION_API)

    url_parts[3:6] = [''] * 3
    version_url = urlparse.urlunparse(url_parts)

    with retry() as session:
        response = session.get(version_url)

        if response.status_code != 200:
            return False

        version_data = response.json()

        if 'status' not in version_data or 'data' not in version_data:
            return False

        version_info = version_data['data']

        if 'version' not in version_info or 'build' not in version_info:
            return False

        return True
Exemplo n.º 2
0
 def make_request(self, uri):
     """Execute network request."""
     with retry() as session:
         response = session.get(uri, headers={'Accept': self._accept})
         if response.status_code != 200:
             raise LookupError('record not found')
         return response
Exemplo n.º 3
0
 def _make_request(self, uri):
     """Execute network request."""
     with retry() as session:
         response = session.get(uri, headers={"Accept": self._accept})
         if response.status_code != 200:
             raise LookupError("record not found. Status: {}".format(
                 response.status_code))
         return response
Exemplo n.º 4
0
    def make_request(self, uri):
        """Execute network request."""
        record_id = ZenodoProvider.record_id(uri)

        with retry() as session:
            response = session.get(
                make_records_url(record_id), headers={'Accept': self._accept}
            )
            if response.status_code != 200:
                raise LookupError('record not found')
            return response
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
    def _query(self, doi):
        """Retrieve metadata for given doi."""
        doi = extract_doi(doi)
        url = make_doi_url(doi)

        with retry() as session:
            response = session.get(url, headers=self.headers)
            if response.status_code != 200:
                raise LookupError("record not found. Status: {}".format(response.status_code))

            return response
Exemplo n.º 7
0
    def _query(self, doi):
        """Retrieve metadata for given doi."""
        url = doi
        if doi.startswith('http') is False:
            url = make_doi_url(doi)

        with retry() as session:
            response = session.get(url, headers=self.headers)
            if response.status_code != 200:
                raise LookupError('record not found')

            return response
Exemplo n.º 8
0
    def remove_project():
        project_slug = "{0}/{1}".format(
            payload["project_namespace"],
            strip_and_lower(payload["project_name"]))

        project_slug_encoded = urllib.parse.quote(project_slug, safe="")
        project_delete_url = "{0}/api/v4/projects/{1}".format(
            payload["project_repository"], project_slug_encoded)

        with retry() as session:
            session.delete(url=project_delete_url,
                           headers=authentication_headers)

        return True
Exemplo n.º 9
0
    def remove_datasets():
        url = f'{server_url}/api/v1/dataverses/sdsc-test-dataverse/contents'
        try:
            with retry() as session:
                response = session.get(url=url, headers=headers)
        except (ConnectionError, requests.exceptions.RequestException):
            warnings.warn('Cannot clean up Dataverse datasets')
            return

        if response.status_code != 200:
            warnings.warn('Cannot clean up Dataverse datasets')
            return

        datasets = response.json().get('data', [])

        for dataset in datasets:
            id = dataset.get('id')
            if id is not None:
                url = f'https://demo.dataverse.org/api/v1/datasets/{id}'
                try:
                    with retry() as session:
                        session.delete(url=url, headers=headers)
                except (ConnectionError, requests.exceptions.RequestException):
                    pass