Skip to content
Snippets Groups Projects
Commit 4026e39f authored by vuillaut's avatar vuillaut
Browse files

adding sandbox option to get records functions

parent 671e5370
No related branches found
No related tags found
1 merge request!27adding sandbox option to get records functions
Pipeline #135916 failed
...@@ -10,7 +10,7 @@ __all__ = [ ...@@ -10,7 +10,7 @@ __all__ = [
escape_community = 'escape2020' escape_community = 'escape2020'
def get_ossr_records(search='', **kwargs): def get_ossr_records(search='', sandbox=False, **kwargs):
""" """
Retrieve all OSSR entries. Retrieve all OSSR entries.
Potentially long when the number of records in the OSSR increases. Potentially long when the number of records in the OSSR increases.
...@@ -42,5 +42,5 @@ def get_ossr_records(search='', **kwargs): ...@@ -42,5 +42,5 @@ def get_ossr_records(search='', **kwargs):
# ruling out that possibility at the moment # ruling out that possibility at the moment
kwargs['communities'] = escape_community kwargs['communities'] = escape_community
return get_zenodo_records(search, **kwargs) return get_zenodo_records(search, sandbox=sandbox, **kwargs)
...@@ -19,6 +19,7 @@ __all__ = [ ...@@ -19,6 +19,7 @@ __all__ = [
zenodo_api_url = "https://zenodo.org/api" zenodo_api_url = "https://zenodo.org/api"
zenodo_sandobx_api_url = "https://sandbox.zenodo.org/api"
class ZenodoAPI: class ZenodoAPI:
...@@ -50,9 +51,9 @@ class ZenodoAPI: ...@@ -50,9 +51,9 @@ class ZenodoAPI:
""" """
if sandbox: if sandbox:
self.zenodo_api_url = "https://sandbox.zenodo.org/api" self.api_url = zenodo_sandobx_api_url
else: else:
self.zenodo_api_url = zenodo_api_url self.api_url = zenodo_api_url
self.access_token = access_token self.access_token = access_token
self.parameters = {'access_token': self.access_token} self.parameters = {'access_token': self.access_token}
...@@ -67,11 +68,11 @@ class ZenodoAPI: ...@@ -67,11 +68,11 @@ class ZenodoAPI:
""" """
Fetch the published entries of an user. Works to tests connection to Zenodo too. 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 :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) return requests.get(url, params=self.parameters)
...@@ -79,11 +80,11 @@ class ZenodoAPI: ...@@ -79,11 +80,11 @@ class ZenodoAPI:
""" """
Create a new entry / deposition in (sandbox.)zenodo 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 :return: request.put method
""" """
url = f"{self.zenodo_api_url}/deposit/depositions" url = f"{self.api_url}/deposit/depositions"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
return requests.post(url, json={}, headers=headers, params=self.parameters) return requests.post(url, json={}, headers=headers, params=self.parameters)
...@@ -92,7 +93,7 @@ class ZenodoAPI: ...@@ -92,7 +93,7 @@ class ZenodoAPI:
""" """
Fetches (recovers all the existing information, as well as links) of an existing Zenodo entry. 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 :param entry_id: str
entry_id of the entry to fetch entry_id of the entry to fetch
...@@ -103,13 +104,13 @@ class ZenodoAPI: ...@@ -103,13 +104,13 @@ class ZenodoAPI:
# a record is request.get('api/deposit/deposition/{entry_id}') - see also the upload_file_entry method. # 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: # 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) return requests.get(url, params=self.parameters)
def upload_file_entry(self, entry_id, name_file, path_file): 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 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. PUT method to {bucket_url}/{filename}. The full api url is recovered when the entry is firstly retrieved.
...@@ -123,7 +124,7 @@ class ZenodoAPI: ...@@ -123,7 +124,7 @@ class ZenodoAPI:
:return: request.put method :return: request.put method
""" """
# 1 - Retrieve and recover information of a record that is in process of being published # 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) params=self.parameters)
# 2 - Upload the files # 2 - Upload the files
...@@ -140,7 +141,7 @@ class ZenodoAPI: ...@@ -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 Update an entry resource. Data should be the entry information that will be shown when a deposition is visited
at the Zenodo site. 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 :param entry_id: str
deposition_id of the Zenodo entry deposition_id of the Zenodo entry
...@@ -149,7 +150,7 @@ class ZenodoAPI: ...@@ -149,7 +150,7 @@ class ZenodoAPI:
:return: request.put method :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"} headers = {"Content-Type": "application/json"}
# The metadata field is already created, just need to be updated. # The metadata field is already created, just need to be updated.
...@@ -164,14 +165,14 @@ class ZenodoAPI: ...@@ -164,14 +165,14 @@ class ZenodoAPI:
Erase an entry/new version of an entry that HAS NOT BEEN published yet. 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) 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 :param entry_id: str
deposition_id of the Zenodo entry to be erased deposition_id of the Zenodo entry to be erased
:return: request.delete method :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) return requests.delete(url, params=self.parameters)
def erase_file_entry(self, entry_id, file_id): def erase_file_entry(self, entry_id, file_id):
...@@ -179,7 +180,7 @@ class ZenodoAPI: ...@@ -179,7 +180,7 @@ class ZenodoAPI:
Erase a file from an entry resource. 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 This method is intended to be used for substitution of files (deletion) within an entry by their correspondent
new versions. 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 :param entry_id: str
deposition_id of the Zenodo entry deposition_id of the Zenodo entry
...@@ -188,33 +189,33 @@ class ZenodoAPI: ...@@ -188,33 +189,33 @@ class ZenodoAPI:
:return: requests.delete method :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) return requests.delete(url, params=self.parameters)
def publish_entry(self, entry_id): def publish_entry(self, entry_id):
""" """
Publishes an entry in (sandbox.)zenodo 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 :param entry_id: str
deposition_id of the Zenodo entry deposition_id of the Zenodo entry
:return: requests.put method :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) return requests.post(url, params=self.parameters)
def new_version_entry(self, entry_id): def new_version_entry(self, entry_id):
""" """
Creates a new version of AN EXISTING entry resource. 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 :param entry_id: str
deposition_id of the Zenodo entry deposition_id of the Zenodo entry
:return: requests.post method :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} parameters = {'access_token': self.access_token}
return requests.post(url, params=parameters) return requests.post(url, params=parameters)
...@@ -222,7 +223,7 @@ class ZenodoAPI: ...@@ -222,7 +223,7 @@ class ZenodoAPI:
def fetch_community_entries(self, community_name='escape2020', results_per_query=100): def fetch_community_entries(self, community_name='escape2020', results_per_query=100):
""" """
Query the entries within a community. 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 :param community_name: str
...@@ -249,7 +250,7 @@ class ZenodoAPI: ...@@ -249,7 +250,7 @@ class ZenodoAPI:
# Zenodo metadata of each entry # Zenodo metadata of each entry
# [item['metadata'] for item in content.json()['hits']['hits']] # [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): def fetch_community_entries_per_id(self, community_name='escape2020', results_per_query=100):
""" """
...@@ -393,7 +394,7 @@ class ZenodoAPI: ...@@ -393,7 +394,7 @@ class ZenodoAPI:
erase_error = self.erase_entry(test_entry_id) erase_error = self.erase_entry(test_entry_id)
if erase_error.status_code != 204: if erase_error.status_code != 204:
print(f" !! ERROR erasing dummy test entry. Please erase it manually at\n " 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: else:
print(" - Done.\n") print(" - Done.\n")
sys.exit(-1) sys.exit(-1)
...@@ -478,7 +479,7 @@ class Record: ...@@ -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`. Search the ossr based on `search`.
Function rewritten from pyzenodo3 (https://github.com/space-physics/pyzenodo3) Function rewritten from pyzenodo3 (https://github.com/space-physics/pyzenodo3)
...@@ -506,7 +507,8 @@ def get_zenodo_records(search='', **kwargs): ...@@ -506,7 +507,8 @@ def get_zenodo_records(search='', **kwargs):
**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"]] recs = [Record(hit) for hit in requests.get(url).json()["hits"]["hits"]]
...@@ -516,14 +518,15 @@ def get_zenodo_records(search='', **kwargs): ...@@ -516,14 +518,15 @@ def get_zenodo_records(search='', **kwargs):
return recs return recs
def get_record(record_id): def get_record(record_id, sandbox=False):
""" """
Get a record from its id Get a record from its id
:param record_id: int :param record_id: int
:return: Record :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() json = requests.get(url).json()
if 'status' in json.keys(): if 'status' in json.keys():
raise ValueError(f"Error {json['status']} : {json['message']}") raise ValueError(f"Error {json['status']} : {json['message']}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment