Skip to content
Snippets Groups Projects
Commit fa5af8bb authored by Vuillaume's avatar Vuillaume
Browse files

Update ossr_api-Upload_records_OSSR.ipynb with ZENODO_TOKEN for CI

parent 0d71a6df
No related branches found
No related tags found
1 merge request!52Update ossr_api-Upload_records_OSSR.ipynb with ZENODO_TOKEN for CI
Pipeline #138032 passed
%% Cell type:markdown id:d5e9bc28 tags:
# Upload records on the OSSR
%% Cell type:markdown id:a4123841 tags:
To upload records on the OSSR, you may use the [Zenodo client](https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/blob/master/eossr/api/zenodo.py).
The client is also used by the continuous integration to automatise project's uploads from GitLab to Zenodo [check the EOSSR scripts here](https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/tree/master/eossr/scripts).
**However**, to upload records to the OSSR, you **will need** to use your Zenodo token ([create one](https://zenodo.org/account/settings/applications/tokens/new/)).
%% Cell type:code id:52aedbb2 tags:
``` python
token = ''
token = $SANDBOX_ZENODO_TOKEN_GARCIA
```
%% Cell type:code id:6fcd7ef2 tags:
``` python
import json
from eossr.api.zenodo import ZenodoAPI
# Please note that for this demo we are using Zenodo Sandbox.
z = ZenodoAPI(access_token=token, sandbox=True)
```
%% Cell type:code id:24eecce0 tags:
``` python
```
%% Cell type:markdown id:c13a5232 tags:
## Create a new entry
First we would need to create a new entry
%% Cell type:code id:83075f0b tags:
``` python
new_entry = z.create_new_entry()
```
%% Cell type:code id:84173c5d tags:
``` python
```
%% Cell type:markdown id:fcd8e90c tags:
### Zenodo API answer handler
We have also developped a REST API response client to manage the answers from the Zenodo API.
%% Cell type:code id:22f8e2b2 tags:
``` python
from eossr.api.zenodo.http_status import ZenodoHTTPStatus
```
%% Cell type:code id:07fd3360 tags:
``` python
status_new_entry = ZenodoHTTPStatus(new_entry.status_code, new_entry.json())
print(status_new_entry)
```
%% Cell type:markdown id:b8761071 tags:
## Add files to the entry
Then we upload to the new entry a file (let's say a `codemeta.json` metadata file)
%% Cell type:code id:74351af6 tags:
``` python
deposit_id = new_entry.json()['id']
upload_file = z.upload_file_entry(deposit_id,
name_file='codemeta.json',
path_file='../../codemeta.json')
```
%% Cell type:code id:a836e493 tags:
``` python
status_upload = ZenodoHTTPStatus(upload_file.status_code, upload_file.json())
print(status_upload)
```
%% Cell type:code id:7809adba tags:
``` python
```
%% Cell type:markdown id:aadbd21e tags:
## Add metadata to the entry
We add metadata to the Zenodo entry using the EOSSR codemeta2zenodo conversor
%% Cell type:code id:c622d5c9 tags:
``` python
from eossr.metadata.codemeta2zenodo import parse_codemeta_and_write_zenodo_metadata_file
```
%% Cell type:code id:d0b1af6d tags:
``` python
parse_codemeta_and_write_zenodo_metadata_file(codemeta_filename='../../codemeta.json',
zenodo_outname='.zenodo.json')
```
%% Cell type:code id:9f3cfedc tags:
``` python
# Let's have a look to the output to the .zenodo.json file
! cat .zenodo.json
```
%% Cell type:code id:7c37dc70 tags:
``` python
with open('.zenodo.json') as f:
zenodo_metadata = json.load(f)
update_metadata_info = z.update_metadata_entry(deposit_id, json_metadata=zenodo_metadata)
```
%% Cell type:code id:ae6554d9 tags:
``` python
status_metadata = ZenodoHTTPStatus(update_metadata_info.status_code, update_metadata_info.json())
print(status_metadata)
```
%% Cell type:code id:1f67832d tags:
``` python
```
%% Cell type:markdown id:c6a4df85 tags:
### Check the content of the new entry
The `request` answers contain all the information of the current upload
%% Cell type:code id:4389a073 tags:
``` python
update_metadata_info.json()
```
%% Cell type:markdown id:a62874c5 tags:
We can check the files that we have already uploaded
%% Cell type:code id:f0328d01 tags:
``` python
update_metadata_info.json()['files']
```
%% Cell type:markdown id:ae3ccdb8 tags:
Or the metadata updated
%% Cell type:code id:7111b368 tags:
``` python
update_metadata_info.json()['metadata']
```
%% Cell type:code id:30337677 tags:
``` python
```
%% Cell type:markdown id:fa18756a tags:
## Publish the entry
Finally we can publish the new entry.
However in this case, because this is just a demo, we will just erase the entry.
%% Cell type:code id:6a9f1f09 tags:
``` python
# In case you would like to publish the entry, just uncomment the following lines.
# # publish = z.publish_entry(deposit_id)
# # status = ZenodoHTTPStatus(publish.status_code, publish.json())
```
%% Cell type:code id:ec4570b6 tags:
``` python
erase_entry = z.erase_entry(deposit_id)
```
%% Cell type:code id:3691016e tags:
``` python
ZenodoHTTPStatus(erase_entry.status_code).description
```
%% Cell type:code id:406541ef tags:
``` python
```
......
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