Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
template_project_escape
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Gitlab has been updated. More info
here
.
Show more breadcrumbs
ESCAPE2020
WP3
template_project_escape
Merge requests
!5
The source project of this merge request has been removed.
Handler rest api os
Merged
Handler rest api os
(removed):handler_rest_api_os
into
master
Overview
0
Commits
3
Pipelines
0
Changes
1
Merged
Enrique Garcia
requested to merge
(removed):handler_rest_api_os
into
master
4 years ago
Overview
0
Pipelines
0
Changes
1
Expand
add our own version of the REST api handler
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
4ff28011
3 commits,
4 years ago
1 file
+
124
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
.zenodoci/zenodoapi.py
0 → 100644
+
124
−
0
Options
# -*- coding: utf-8 -*-
import
json
import
requests
class
ZenodoAPI
:
def
__init__
(
self
,
access_token
,
sandbox
=
True
):
"""
Manages the communication with the (sandbox.)zenodo REST API.
This class is **EXCLUSIVELY** developed to be used within a CI/CD pipeline and to **EXCLUSIVELY PERFORM**
five tasks within the (sandbox.)zenodo api environment:
- Create a new deposit,
- Create a new version of an existing deposit,
- Upload files to a specific Zenodo entry,
- Upload information to the entry (Zenodo compulsory deposit information)
- Publish an entry
:param access_token: str
Personal access token to (sandbox.)zenodo.org/api
:param sandbox: bool
Communicates with either zenodo or sandbox.zenodo api
"""
if
sandbox
:
zenodo_api_url
=
"
https://sandbox.zenodo.org/api/
"
else
:
zenodo_api_url
=
"
https://zenodo.org/api/
"
self
.
zenodo_api_url
=
zenodo_api_url
self
.
access_token
=
access_token
def
create_new_upload
(
self
):
"""
Create a new entry / deposition in (sandbox.)zenodo
POST method to {zenodo_api_url}/deposit/depositions
:return: request.put answer
"""
url
=
f
"
{
self
.
zenodo_api_url
}
/deposit/depositions
"
headers
=
{
"
Content-Type
"
:
"
application/json
"
}
parameters
=
{
'
access_token
'
:
self
.
access_token
}
return
requests
.
post
(
url
,
headers
=
headers
,
params
=
parameters
)
def
upload_files_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}.
PUT method to {bucket_url}/{filename}. The full api url is recovered when the entry is firstly retrieved.
:param entry_id: str
deposition_id of the Zenodo entry
:param name_file: str
File name of the file when uploaded
:param path_file: str
Path to the file to be uploaded
:return: json request.put object
"""
# 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
)
# 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
)
return
upload
.
json
()
def
update_info_entry
(
self
,
entry_id
,
data
):
"""
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)
:param entry_id: str
deposition_id of the Zenodo entry
:param data: object
json object containing the metadata (compulsory fields) that are enclosed when a new entry is created.
:return: request.put answer
"""
url
=
f
"
{
self
.
zenodo_api_url
}
/deposit/depositions/
{
entry_id
}
"
headers
=
{
"
Content-Type
"
:
"
application/json
"
}
parameters
=
{
'
access_token
'
:
self
.
access_token
}
return
requests
.
put
(
url
,
data
=
json
.
dump
(
data
),
headers
=
headers
,
params
=
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
:param entry_id: str
deposition_id of the Zenodo entry
:return: request.put answer
"""
url
=
f
"
{
self
.
zenodo_api_url
}
/deposit/depositions/
{
entry_id
}
/actions/publish
"
parameters
=
{
'
access_token
'
:
self
.
access_token
}
return
requests
.
post
(
url
,
params
=
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
:param entry_id: str
deposition_id of the Zenodo entry
:return: request.post answer
"""
url
=
f
"
{
self
.
zenodo_api_url
}
deposit/depositions/
{
entry_id
}
/actions/newversion
"
parameters
=
{
'
access_token
'
:
self
.
access_token
}
return
requests
.
post
(
url
,
params
=
parameters
)
Loading