Skip to content
Snippets Groups Projects
test_connection_zenodo.py 1.82 KiB
Newer Older
Enrique Garcia's avatar
Enrique Garcia committed
# -*- coding: utf-8 -*-

import argparse
import requests
import numpy as np
from distutils.util import strtobool
from zenodoapi import ZenodoAPI

if __name__ == '__main__':

    # Required arguments
    parser = argparse.ArgumentParser(description="Upload new deposit entry to Zenodo")

    parser.add_argument('--token', '-t', type=str,
                        dest='zenodo_token',
                        help='Personal access token to (sandbox)Zenodo')

    parser.add_argument('--sandbox_zenodo', '-s', action='store',
                        type=lambda x: bool(strtobool(x)),
                        dest='sandbox_flag',
                        help='Set the Zenodo environment.'
                             'If True connects with Zenodo. If False with Sanbox Zenodo',
                        default=False)

    args = parser.parse_args()

    z = ZenodoAPI(access_token=args.zenodo_token,
                  sandbox=args.sandbox_flag  # True for sandbox.zenodo.org !! False for zenodo.org
                  )

    # Test that the you can communicate with (sandbox)zenodo - you have passed the correct token
    parameters = {'access_token': z.access_token}

    test_connection = requests.get(
        f'{z.zenodo_api_url}/deposit/depositions',
        params=parameters
    )
    np.testing.assert_equal(test_connection.status_code, 200)

    # Test that you can create a new entry - then all the rest of the pipeline will work
    new_entry = z.create_new_entry()
    np.testing.assert_equal(new_entry.status_code, 201)

    # Erase the un-submitted entry and test that the order has been passed correctly
    entry_id = new_entry.json()['id']
    erase_unsubmitted_entry = requests.delete(
        f'{z.zenodo_api_url}/deposit/depositions/{entry_id}',
        params=parameters
    )
    np.testing.assert_equal(erase_unsubmitted_entry.status_code, 204)