-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1868 from datadryad/3057-analyze-api-updates-need…
…ed-for-data-science-tools added jupyter notebooks examples in documentation
- Loading branch information
Showing
4 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 274 additions & 0 deletions
274
documentation/apis/Jupyter-Notebooks/create_dataset_notebook.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cd03e984-7b12-4689-9d22-42a4b495a09e", | ||
"metadata": {}, | ||
"source": [ | ||
"Import all needed libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "e02e99ee-1c49-4197-b452-15ea2478beb6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"import urllib.parse\n", | ||
"import urllib.request\n", | ||
"import json" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a4388476-7221-4591-b391-269fe2cd02f1", | ||
"metadata": {}, | ||
"source": [ | ||
"Set host and API credentials\n", | ||
"* __host__ - API host\n", | ||
"* __API_CLIENT_ID__ - API client ID\n", | ||
"* __API_CLIENT_SECRET__ - API client secret\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "c265b1b2-d713-4b0b-bcdc-def9ef474e2f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"host = 'https://sandbox.datadryad.org'\n", | ||
"API_CLIENT_ID = \"[CLIENT_ID]\"\n", | ||
"API_CLIENT_SECRET = \"[CLIENT_SECRET]\"\n", | ||
"\n", | ||
"headers = {\"Content-Type\": \"application/json\"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5a9e7dea-9d24-4128-91e0-0dac589da4ea", | ||
"metadata": {}, | ||
"source": [ | ||
"Authorize API connection and retrieve access token" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "6f3bd89a-e475-4cec-8aa9-8254929a4842", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"payload = {'client_id': API_CLIENT_ID, 'client_secret': API_CLIENT_SECRET, 'grant_type': 'client_credentials'}\n", | ||
"payload = {\n", | ||
" \"client_id\": API_CLIENT_ID,\n", | ||
" \"client_secret\": API_CLIENT_SECRET, \n", | ||
" \"grant_type\": \"client_credentials\"\n", | ||
"}\n", | ||
"url = f'{host}/oauth/token'\n", | ||
"response = requests.post(url, data=json.dumps(payload), headers=headers)\n", | ||
"\n", | ||
"if response.status_code != 200:\n", | ||
" raise SystemExit(response.json()['error_description'])\n", | ||
"token = response.json()['access_token']\n", | ||
"\n", | ||
"if not token:\n", | ||
" raise SystemExit('Unable to retrieve access token')\n", | ||
"\n", | ||
"headers['Authorization'] = f'{response.json()['token_type']} {token}'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a9a42849-70f6-42e7-9a57-086be116325c", | ||
"metadata": {}, | ||
"source": [ | ||
"Create dataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "9072c664-e89c-4286-9f4d-40387bff0e58", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"payload = {\n", | ||
" \"title\": \"Data from: Jupyter Notebook uploaded through API\",\n", | ||
" \"abstract\" : \"This study develops the random phylogenies rate test (RAPRATE), a likelihood method that simulates morphological evolution along randomly generated phylogenies, and uses it to determine whether a considerable difference in morphological diversity between two sister clades of South American fishes should be taken as evidence of differing rates of morphological change or lineage turnover.\",\n", | ||
" \"authors\" : [ \n", | ||
" {\n", | ||
" \"firstName\" : \"Test\",\n", | ||
" \t \"lastName\" : \"Author\",\n", | ||
" \t \"email\" : \"test@examples.com\",\n", | ||
" \t \"orcid\" : \"0009-0000-4404-1689\",\n", | ||
" \t \"affiliation\" : \"Oregon State University\",\n", | ||
" \t \"affiliationROR\" : \"https://ror.org/00ysfqy60\"\n", | ||
" \t}\n", | ||
" ]\n", | ||
"}\n", | ||
"headers['Content-Type'] = 'application/json'\n", | ||
"url = f'{host}/api/v2/datasets'\n", | ||
"response = requests.post(url, data=json.dumps(payload), headers=headers)\n", | ||
"dataset_id = response.json()['id']\n", | ||
"dataset_url = f'{host}{response.json()['_links']['self']['href']}'\n", | ||
"\n", | ||
"if 'error' in response.json():\n", | ||
" raise SystemExit(response.json()['error']); exit\n", | ||
"else:\n", | ||
" print('Resource successfully created')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6c4e2b56-ca10-4c26-a2ec-38cca903928f", | ||
"metadata": {}, | ||
"source": [ | ||
"Update dataset and set missing information" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "71a45f81-0063-4643-a78a-f39a6144afbf", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"payload = {\n", | ||
" \"title\": \"A Study of Red-Black Trees with Weight\",\n", | ||
" \"authors\": [\n", | ||
" {\n", | ||
" \"firstName\": \"Matilda\",\n", | ||
" \"lastName\": \"Swinton\",\n", | ||
" \"email\": \"matilda.swinton@example.com\",\n", | ||
" \"affiliation\": \"Example Organization\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"abstract\": \"<p>Information theorists agree that \\\"fuzzy\\\" models are an interesting new topic in the field of machine learning.</p>\\r\\n\",\n", | ||
" \"funders\": [\n", | ||
" {\n", | ||
" \"organization\": \"Stichting Sem Presser Archief\",\n", | ||
" \"identifierType\": \"ror\",\n", | ||
" \"identifier\": \"https://ror.org/02f5r3008\",\n", | ||
" \"awardNumber\": \"12345\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"keywords\": [\n", | ||
" \"Cat\",\n", | ||
" \"Host\",\n", | ||
" \"Computer\",\n", | ||
" \"Log\"\n", | ||
" ],\n", | ||
" \"methods\": \"<p><br>\\r\\nMany mathematicians would agree that, had it not been for systems, the emulation of active networks might never have occurred. We view algorithms as following a cycle of four phases: simulation, investigation, observation, and investigation. After years of robust research into Boolean logic, we confirm the evaluation of Smalltalk. to what extent can spreadsheets be evaluated to realize this intent?</p>\\r\\n\",\n", | ||
" \"relatedWorks\": [\n", | ||
" {\n", | ||
" \"relationship\": \"article\",\n", | ||
" \"identifierType\": \"URL\",\n", | ||
" \"identifier\": \"http://example.org/cats\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"userId\": 2,\n", | ||
" \"email\": \"some_email@example.com\"\n", | ||
"}\n", | ||
"\n", | ||
"headers['Content-Type'] = 'application/json'\n", | ||
"response = requests.put(dataset_url, data=json.dumps(payload), headers=headers)\n", | ||
"if response.status_code != 200:\n", | ||
" raise SystemExit(response.json()['error_description'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f6fe81b0-1fe0-43cb-9a00-b9a5a6bdeea8", | ||
"metadata": {}, | ||
"source": [ | ||
"Embed a file on the dataset using file " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "eef23b0f-8dff-421e-8585-37ef6b8e3c43", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"embed_url = f'{dataset_url}/urls'\n", | ||
"payload = {\n", | ||
" \"url\": \"https://github.com/datadryad/dryad-app/blob/e927b514efe97fc08c7fc30d99389a5ec6d8a493/documentation/apis/sample_dataset.json\",\n", | ||
" \"digest\": \"aca3032d20c829a6060f1b90afda6d14\",\n", | ||
" \"digestType\": \"md5\",\n", | ||
" \"description\": \"This is the best file ever!\",\n", | ||
" \"size\": 1234,\n", | ||
" \"path\": \"api_submission.md\",\n", | ||
" \"mimeType\": \"text/plain\",\n", | ||
" \"skipValidation\": True\n", | ||
"}\n", | ||
"\n", | ||
"headers['Content-Type'] = 'application/json'\n", | ||
"response = requests.post(embed_url, data=json.dumps(payload), headers=headers)\n", | ||
"if 'error' in response.json():\n", | ||
" raise SystemExit(response.json()['error']); exit\n", | ||
"else:\n", | ||
" print('File successfuly embeded')\n", | ||
" \n", | ||
"response.json()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8ce4c50a-5c38-4d0a-866b-95307b87dd13", | ||
"metadata": {}, | ||
"source": [ | ||
"Submit dataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "c1e18f98-e8bd-489e-9d88-9aea1191be5e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"payload = {\n", | ||
" \"op\": \"replace\",\n", | ||
" \"path\": \"/versionStatus\",\n", | ||
" \"value\": \"submitted\"\n", | ||
"}\n", | ||
"\n", | ||
"headers['Content-Type'] = 'application/json-patch+json'\n", | ||
"response = requests.patch(dataset_url, data=json.dumps(payload), headers=headers)\n", | ||
"\n", | ||
"errors = [obj['error'] for obj in response.json()]\n", | ||
"\n", | ||
"if len(errors) > 0:\n", | ||
" print('Errors: ')\n", | ||
" print(', '.join(errors))\n", | ||
"else:\n", | ||
" print('Status successfully updated')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.