Commit 189be17c authored by Enrique Garcia's avatar Enrique Garcia
Browse files

Merge branch 'new_fetch_methods' into 'master'

Add different fetch (GET) methods

Closes #4, #5, and #6

See merge request !18
parents 7fe207f3 c4b856ac
Pipeline #120519 failed with stages
in 1 minute and 8 seconds
......@@ -24,9 +24,9 @@ class TestZenodoApiSandbox(unittest.TestCase):
assert z.access_token == token
assert type(z.exist_codemeta_file) == bool
assert type(z.exist_zenodo_metadata_file) == bool
assert z.path_codemeta_file == ROOT_DIR
assert z.path_zenodo_metadata_file == ROOT_DIR
assert z.proj_root_dir == Path(ROOT_DIR)
assert z.path_codemeta_file == z.proj_root_dir
assert z.path_zenodo_metadata_file == z.proj_root_dir
assert isinstance(z.proj_root_dir, (Path, str))
......@@ -42,9 +42,9 @@ class TestZenodoAPI(unittest.TestCase):
assert z.access_token == token
assert type(z.exist_codemeta_file) == bool
assert type(z.exist_zenodo_metadata_file) == bool
assert z.path_codemeta_file == ROOT_DIR
assert z.path_zenodo_metadata_file == ROOT_DIR
assert z.proj_root_dir == Path(ROOT_DIR)
assert z.path_codemeta_file == z.proj_root_dir
assert z.path_zenodo_metadata_file == z.proj_root_dir
assert isinstance(z.proj_root_dir, (Path, str))
def test_zenodo_api_methods(self):
......@@ -60,6 +60,9 @@ class TestZenodoAPI(unittest.TestCase):
fetch_user_entry = z.fetch_user_entries()
create_new_entry = z.create_new_entry()
fetch_single_entry = z.fetch_entry(
entry_id=test_id
)
# upload_file_entry = z.upload_file_entry(
# entry_id=test_id,
# name_file=test_filename,
......@@ -82,15 +85,22 @@ class TestZenodoAPI(unittest.TestCase):
new_version_entry = z.new_version_entry(
entry_id=test_id
)
community_entries = z.fetch_community_entries()
fetch_ids = z.fetch_community_entries_per_id()
fetch_filenames = z.fetch_community_entries_per_title()
assert isinstance(fetch_user_entry, requests.models.Response)
assert isinstance(create_new_entry, requests.models.Response)
assert isinstance(fetch_single_entry, requests.models.Response)
# assert isinstance(upload_file_entry, requests.models.Response)
assert isinstance(upload_metadata_entry, requests.models.Response)
assert isinstance(erase_entry, requests.models.Response)
assert isinstance(erase_file_entry, requests.models.Response)
assert isinstance(publish_entry, requests.models.Response)
assert isinstance(new_version_entry, requests.models.Response)
assert isinstance(community_entries, requests.models.Response)
assert isinstance(fetch_ids, list)
assert isinstance(fetch_filenames, list)
def test_search_codemeta_file(self):
token = 'FakeToken'
......
......@@ -43,11 +43,13 @@ class ZenodoAPI:
self.zenodo_api_url = zenodo_api_url
self.access_token = access_token
self.parameters = {'access_token': self.access_token}
self.proj_root_dir = Path(proj_root_dir)
self.exist_codemeta_file = False
self.path_codemeta_file = proj_root_dir
self.path_codemeta_file = self.proj_root_dir
self.exist_zenodo_metadata_file = False
self.path_zenodo_metadata_file = proj_root_dir
self.path_zenodo_metadata_file = self.proj_root_dir
def fetch_user_entries(self):
"""
......@@ -55,13 +57,11 @@ class ZenodoAPI:
GET method to {zenodo_api_url}/deposit/depositions
:return: obj
request.get answer
:return: request.get method
"""
url = f"{self.zenodo_api_url}/deposit/depositions"
parameters = {'access_token': self.access_token}
return requests.get(url, params=parameters)
return requests.get(url, params=self.parameters)
def create_new_entry(self):
"""
......@@ -69,14 +69,26 @@ class ZenodoAPI:
POST method to {zenodo_api_url}/deposit/depositions
:return: obj
requests.put answer
:return: request.put method
"""
url = f"{self.zenodo_api_url}/deposit/depositions"
headers = {"Content-Type": "application/json"}
parameters = {'access_token': self.access_token}
return requests.post(url, json={}, headers=headers, params=parameters)
return requests.post(url, json={}, headers=headers, params=self.parameters)
def fetch_entry(self, entry_id):
"""
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}
:param entry_id: str
entry_id of the entry to fetch
:return: request.get method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}"
return requests.get(url, params=self.parameters)
def upload_file_entry(self, entry_id, name_file, path_file):
"""
......@@ -92,20 +104,17 @@ class ZenodoAPI:
:param path_file: str
Path to the file to be uploaded
:return: obj
json requests.put object
:return: request.put method
"""
# 1 - Retrieve and recover information of an existing deposit
parameters = {'access_token': self.access_token}
fetch = requests.get(f"{self.zenodo_api_url}/deposit/depositions/{entry_id}",
params=parameters)
fetch = self.fetch_entry(str(entry_id))
# 2 - Upload the files
bucket_url = fetch.json()['links']['bucket'] # full url is recovered from fetch (GET) method
url = f"{bucket_url}/{name_file}"
with open(path_file, 'rb') as upload_file:
upload = requests.put(url, data=upload_file, params=parameters)
upload = requests.put(url, data=upload_file, params=self.parameters)
return upload.json()
......@@ -121,17 +130,13 @@ class ZenodoAPI:
:param json_metadata: object
json object containing the metadata (compulsory fields) that are enclosed when a new entry is created.
:return: obj
requests.put answer
:return: request.put method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}"
headers = {"Content-Type": "application/json"}
parameters = {'access_token': self.access_token}
data = {"metadata": json_metadata}
return requests.put(url, data=json.dumps(data),
headers=headers, params=parameters)
return requests.put(url, data=json.dumps(json_metadata),
headers=headers, params=self.parameters)
def erase_entry(self, entry_id):
"""
......@@ -143,13 +148,10 @@ class ZenodoAPI:
:param entry_id: str
deposition_id of the Zenodo entry to be erased
:return: obj
requests.delete json answer
:return: request.delete method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}"
parameters = {'access_token': self.access_token}
return requests.delete(url, params=parameters)
return requests.delete(url, params=self.parameters)
def erase_file_entry(self, entry_id, file_id):
"""
......@@ -163,13 +165,10 @@ class ZenodoAPI:
:param file_id: str
Id of the files stored in Zenodo
:return: obj
requests.delete answer
:return: requests.delete method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/files/{file_id}"
parameters = {'access_token': self.access_token}
return requests.delete(url, params=parameters)
return requests.delete(url, params=self.parameters)
def publish_entry(self, entry_id):
"""
......@@ -179,13 +178,10 @@ class ZenodoAPI:
:param entry_id: str
deposition_id of the Zenodo entry
:return: obj
requests.put answer
:return: requests.put method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/actions/publish"
parameters = {'access_token': self.access_token}
return requests.post(url, params=parameters)
return requests.post(url, params=self.parameters)
def new_version_entry(self, entry_id):
"""
......@@ -195,14 +191,75 @@ class ZenodoAPI:
:param entry_id: str
deposition_id of the Zenodo entry
:return: obj
requests.post answer
:return: requests.post method
"""
url = f"{self.zenodo_api_url}/deposit/depositions/{entry_id}/actions/newversion"
parameters = {'access_token': self.access_token}
return requests.post(url, params=parameters)
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
:param community_name: str
Community name. DEFAULT='escape2020'
:param results_per_query: int
Number of entries returned per call to the REST API. DEFAULT=100.
:return: requests.get method
"""
# https://developers.zenodo.org/#list36
update_query_args = {'communities': str(community_name),
'size': int(results_per_query)
}
self.parameters.update(update_query_args)
# Full answer
# content = requests.post(url, params=self.parameters)
# Answer items
# content.json().keys()
# Stats
# content.json()['aggregations']
# Total num of entries
# content.json()['hits']['total']
# 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)
def fetch_community_entries_per_id(self, community_name='escape2020', results_per_query=100):
"""
Query the `entries ids` of all the entries within a community
:param community_name: str
Community name. DEFAULT='escape2020'
:param results_per_query: int
Number of entries returned per call to the REST API. DEFAULT=100.
:return: list
List containing the `id`s of each community entry
"""
return [entry['id'] for entry in
self.fetch_community_entries(community_name, results_per_query).json()['hits']['hits']]
def fetch_community_entries_per_title(self, community_name='escape2020', results_per_query=100):
"""
Query the title of all the entries within a community
:param community_name: str
Community name. DEFAULT='escape2020'
:param results_per_query: int
Number of entries returned per call to the REST API. DEFAULT=100.
:return: list
List containing the title of each community entry
"""
return [entry['metadata']['title'] for entry in
self.fetch_community_entries(community_name, results_per_query).json()['hits']['hits']]
def search_codemeta_file(self):
"""Check if a `codemeta.json` files exists in the ROOT directory of the project"""
......@@ -329,6 +386,7 @@ class ZenodoAPI:
print(" ! ERROR while deleting test entry\n", delete_test_entry.json())
print("\n\tYAY ! Successful testing of the connection to Zenodo ! \n\n"
"You should not face any trouble when uploading a project to Zenodo through the "
"ESCAPE-GitLabCI pipeline.\n"
"You should not face any trouble when uploading a project to Zenodo - if you followed the "
"`OSSR how to publish tutorial`:\n"
"\t https://escape2020.pages.in2p3.fr/wp3/ossr-pages/page/contribute/publish_tutorial/#3-add-the-following-code-snippet \n"
"In case you do, please contact us !\n")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment