diff --git a/eossr/api/__init__.py b/eossr/api/__init__.py index 6d8b2d6f1fbc9cbcd27824cb0c99b7959c9f05d6..a3173d0b6d4fc842bc60cdfa94971f97df5e73b8 100644 --- a/eossr/api/__init__.py +++ b/eossr/api/__init__.py @@ -10,7 +10,7 @@ __all__ = [ escape_community = 'escape2020' -def get_ossr_records(search='', **kwargs): +def get_ossr_records(search='', sandbox=False, **kwargs): """ Retrieve all OSSR entries. Potentially long when the number of records in the OSSR increases. @@ -42,5 +42,5 @@ def get_ossr_records(search='', **kwargs): # ruling out that possibility at the moment kwargs['communities'] = escape_community - return get_zenodo_records(search, **kwargs) + return get_zenodo_records(search, sandbox=sandbox, **kwargs) diff --git a/eossr/api/zenodo.py b/eossr/api/zenodo.py index 184320606625b6dfbee39ef0a025ceebaa659909..8c88ba8135b94f3c67993787e0c4ea3c23727521 100644 --- a/eossr/api/zenodo.py +++ b/eossr/api/zenodo.py @@ -19,6 +19,7 @@ __all__ = [ zenodo_api_url = "https://zenodo.org/api" +zenodo_sandobx_api_url = "https://sandbox.zenodo.org/api" class ZenodoAPI: @@ -50,9 +51,9 @@ class ZenodoAPI: """ if sandbox: - self.zenodo_api_url = "https://sandbox.zenodo.org/api" + self.api_url = zenodo_sandobx_api_url else: - self.zenodo_api_url = zenodo_api_url + self.api_url = zenodo_api_url self.access_token = access_token self.parameters = {'access_token': self.access_token} @@ -67,11 +68,11 @@ class ZenodoAPI: """ Fetch the published entries of an user. Works to tests connection to Zenodo too. - GET method to {zenodo_api_url}/deposit/depositions + GET method to {api_url}/deposit/depositions :return: request.get method """ - url = f"{self.zenodo_api_url}/deposit/depositions" + url = f"{self.api_url}/deposit/depositions" return requests.get(url, params=self.parameters) @@ -79,11 +80,11 @@ class ZenodoAPI: """ Create a new entry / deposition in (sandbox.)zenodo - POST method to {zenodo_api_url}/deposit/depositions + POST method to {api_url}/deposit/depositions :return: request.put method """ - url = f"{self.zenodo_api_url}/deposit/depositions" + url = f"{self.api_url}/deposit/depositions" headers = {"Content-Type": "application/json"} return requests.post(url, json={}, headers=headers, params=self.parameters) @@ -92,7 +93,7 @@ class ZenodoAPI: """ Fetches (recovers all the existing information, as well as links) of an existing Zenodo entry. - GET method to {zenodo_api_url}/deposit/depositions/{entry_id} + GET method to {api_url}/deposit/depositions/{entry_id} :param entry_id: str entry_id of the entry to fetch @@ -103,13 +104,13 @@ class ZenodoAPI: # a record is request.get('api/deposit/deposition/{entry_id}') - see also the upload_file_entry method. # To fetch any other entry, already published, use: - url = f"{self.zenodo_api_url}/records/{entry_id}" + url = f"{self.api_url}/records/{entry_id}" return requests.get(url, params=self.parameters) def upload_file_entry(self, entry_id, name_file, path_file): """ Upload a file to a Zenodo entry. If first retrieve the entry by a GET method to the - {zenodo_api_url}/deposit/depositions/{entry_id}. + {api_url}/deposit/depositions/{entry_id}. PUT method to {bucket_url}/{filename}. The full api url is recovered when the entry is firstly retrieved. @@ -123,7 +124,7 @@ class ZenodoAPI: :return: request.put method """ # 1 - Retrieve and recover information of a record that is in process of being published - fetch = requests.get(f"{self.zenodo_api_url}/deposit/depositions/{entry_id}", + fetch = requests.get(f"{self.api_url}/deposit/depositions/{entry_id}", params=self.parameters) # 2 - Upload the files @@ -140,7 +141,7 @@ class ZenodoAPI: Update an entry resource. Data should be the entry information that will be shown when a deposition is visited at the Zenodo site. - PUT method to {zenodo_api_url}/deposit/depositions/{entry_id}. `data` MUST be included as json.dump(data) + PUT method to {api_url}/deposit/depositions/{entry_id}. `data` MUST be included as json.dump(data) :param entry_id: str deposition_id of the Zenodo entry @@ -149,7 +150,7 @@ class ZenodoAPI: :return: request.put method """ - url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}" + url = f"{self.api_url}/deposit/depositions/{entry_id}" headers = {"Content-Type": "application/json"} # The metadata field is already created, just need to be updated. @@ -164,14 +165,14 @@ class ZenodoAPI: Erase an entry/new version of an entry that HAS NOT BEEN published yet. Any new upload/version will be first saved as 'draft' and not published until confirmation (i.e, requests.post) - DELETE method to {zenodo_api_url}/deposit/depositions/{entry_id}. + DELETE method to {api_url}/deposit/depositions/{entry_id}. :param entry_id: str deposition_id of the Zenodo entry to be erased :return: request.delete method """ - url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}" + url = f"{self.api_url}/deposit/depositions/{entry_id}" return requests.delete(url, params=self.parameters) def erase_file_entry(self, entry_id, file_id): @@ -179,7 +180,7 @@ class ZenodoAPI: Erase a file from an entry resource. This method is intended to be used for substitution of files (deletion) within an entry by their correspondent new versions. - DELETE method to {zenodo_api_url}/deposit/depositions/{entry_id}/files/{file_id} + DELETE method to {api_url}/deposit/depositions/{entry_id}/files/{file_id} :param entry_id: str deposition_id of the Zenodo entry @@ -188,33 +189,33 @@ class ZenodoAPI: :return: requests.delete method """ - url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/files/{file_id}" + url = f"{self.api_url}/deposit/depositions/{entry_id}/files/{file_id}" return requests.delete(url, params=self.parameters) def publish_entry(self, entry_id): """ Publishes an entry in (sandbox.)zenodo - POST method to {zenodo_api_url}/deposit/depositions/{entry_id}/actions/publish + POST method to {api_url}/deposit/depositions/{entry_id}/actions/publish :param entry_id: str deposition_id of the Zenodo entry :return: requests.put method """ - url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/actions/publish" + url = f"{self.api_url}/deposit/depositions/{entry_id}/actions/publish" return requests.post(url, params=self.parameters) def new_version_entry(self, entry_id): """ Creates a new version of AN EXISTING entry resource. - POST method to {zenodo_api_url}/deposit/depositions/{entry_id}/actions/newversion + POST method to {api_url}/deposit/depositions/{entry_id}/actions/newversion :param entry_id: str deposition_id of the Zenodo entry :return: requests.post method """ - url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/actions/newversion" + url = f"{self.api_url}/deposit/depositions/{entry_id}/actions/newversion" parameters = {'access_token': self.access_token} return requests.post(url, params=parameters) @@ -222,7 +223,7 @@ class ZenodoAPI: def fetch_community_entries(self, community_name='escape2020', results_per_query=100): """ Query the entries within a community. - GET method, previous modification of the query arguments, to {zenodo_api_url}/records + GET method, previous modification of the query arguments, to {api_url}/records :param community_name: str @@ -249,7 +250,7 @@ class ZenodoAPI: # Zenodo metadata of each entry # [item['metadata'] for item in content.json()['hits']['hits']] - return requests.get(f"{self.zenodo_api_url}/records", params=self.parameters) + return requests.get(f"{self.api_url}/records", params=self.parameters) def fetch_community_entries_per_id(self, community_name='escape2020', results_per_query=100): """ @@ -393,7 +394,7 @@ class ZenodoAPI: erase_error = self.erase_entry(test_entry_id) if erase_error.status_code != 204: print(f" !! ERROR erasing dummy test entry. Please erase it manually at\n " - f"{self.zenodo_api_url[:-4]}/deposit") + f"{self.api_url[:-4]}/deposit") else: print(" - Done.\n") sys.exit(-1) @@ -478,7 +479,7 @@ class Record: -def get_zenodo_records(search='', **kwargs): +def get_zenodo_records(search='', sandbox=False, **kwargs): """ Search the ossr based on `search`. Function rewritten from pyzenodo3 (https://github.com/space-physics/pyzenodo3) @@ -506,7 +507,8 @@ def get_zenodo_records(search='', **kwargs): **kwargs } - url = zenodo_api_url + "/records?" + urlencode(params) + api_url = zenodo_sandobx_api_url if sandbox else zenodo_api_url + url = api_url + "/records?" + urlencode(params) recs = [Record(hit) for hit in requests.get(url).json()["hits"]["hits"]] @@ -516,14 +518,15 @@ def get_zenodo_records(search='', **kwargs): return recs -def get_record(record_id): +def get_record(record_id, sandbox=False): """ Get a record from its id :param record_id: int :return: Record """ - url = f"{zenodo_api_url}/records/{record_id}" + api_url = zenodo_sandobx_api_url if sandbox else zenodo_api_url + url = f"{api_url}/records/{record_id}" json = requests.get(url).json() if 'status' in json.keys(): raise ValueError(f"Error {json['status']} : {json['message']}")