Skip to content

Commit

Permalink
Use auth token from environment in notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Jun 15, 2024
1 parent 538560c commit 40005ca
Showing 1 changed file with 30 additions and 212 deletions.
242 changes: 30 additions & 212 deletions notebooks/radis_api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,31 @@
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from datetime import date, datetime\n",
"\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from IPython.display import display\n",
"\n",
"load_dotenv(\"../.env.dev\")\n",
"\n",
"base_url = \"http://localhost:8000/api/\"\n",
"\n",
"document_id = \"gepacs_3dfidii5858-6633i4-ii398841\"\n",
"auth_token = \"f2e7412ca332a85e37f3fce88c6a1904fe35ad63\""
"token = os.environ[\"ADMIN_AUTH_TOKEN\"]\n",
"\n",
"document_id = \"gepacs_3dfidii5858-6633i4-ii398841\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Status Code: 201\n"
]
},
{
"data": {
"text/plain": [
"{'id': 1003,\n",
" 'language': 'en',\n",
" 'metadata': {'study_instance_uid': '34343-34343-34343',\n",
" 'accession_number': '345348389',\n",
" 'series_instance_uid': '34343-676556-3343',\n",
" 'sop_instance_uid': '35858-384834-3843'},\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'pacs_aet': 'gepacs',\n",
" 'pacs_name': 'GE PACS',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_id': '1234578',\n",
" 'patient_birth_date': '1976-05-23',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'study_datetime': '2000-08-10T00:00:00+00:53',\n",
" 'body': 'This is the report',\n",
" 'created_at': '2024-04-11T11:01:51.472801+02:00',\n",
" 'updated_at': '2024-04-11T11:01:51.472808+02:00',\n",
" 'groups': [2]}"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"# Create a new report\n",
"\n",
Expand All @@ -84,7 +53,7 @@
"}\n",
"\n",
"response = requests.post(\n",
" base_url + \"reports/\", json=data, headers={\"Authorization\": f\"Token {auth_token}\"}\n",
" base_url + \"reports/\", json=data, headers={\"Authorization\": f\"Token {token}\"}\n",
")\n",
"\n",
"print(f\"Status Code: {response.status_code}\")\n",
Expand All @@ -93,39 +62,9 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 1003,\n",
" 'language': 'en',\n",
" 'metadata': {'study_instance_uid': '34343-34343-34343',\n",
" 'accession_number': '345348389',\n",
" 'series_instance_uid': '34343-676556-3343',\n",
" 'sop_instance_uid': '35858-384834-3843'},\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'pacs_aet': 'gepacs',\n",
" 'pacs_name': 'GE PACS',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_id': '1234578',\n",
" 'patient_birth_date': '1976-05-23',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'study_datetime': '2000-08-10T00:00:00+00:53',\n",
" 'body': 'This is the updated report',\n",
" 'created_at': '2024-04-11T11:01:51.472801+02:00',\n",
" 'updated_at': '2024-04-11T11:01:51.472808+02:00',\n",
" 'groups': [2]}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# Update an existing report\n",
"\n",
Expand Down Expand Up @@ -154,7 +93,7 @@
"response = requests.put(\n",
" base_url + f\"reports/{document_id}/\",\n",
" json=data,\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
" params={\"upsert\": False},\n",
")\n",
"\n",
Expand All @@ -163,106 +102,31 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 1003,\n",
" 'language': 'en',\n",
" 'metadata': {'study_instance_uid': '34343-34343-34343',\n",
" 'accession_number': '345348389',\n",
" 'series_instance_uid': '34343-676556-3343',\n",
" 'sop_instance_uid': '35858-384834-3843'},\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'pacs_aet': 'gepacs',\n",
" 'pacs_name': 'GE PACS',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_id': '1234578',\n",
" 'patient_birth_date': '1976-05-23',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'study_datetime': '2000-08-10T01:07:00+02:00',\n",
" 'body': 'This is the report',\n",
" 'created_at': '2024-04-11T11:01:51.472801+02:00',\n",
" 'updated_at': '2024-04-11T11:01:51.472808+02:00',\n",
" 'groups': [2]}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# Fetch a report (without all document representations in full text search databases)\n",
"\n",
"response = requests.get(\n",
" base_url + f\"reports/{document_id}\",\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
")\n",
"\n",
"response.json()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 1003,\n",
" 'language': 'en',\n",
" 'metadata': {'study_instance_uid': '34343-34343-34343',\n",
" 'accession_number': '345348389',\n",
" 'series_instance_uid': '34343-676556-3343',\n",
" 'sop_instance_uid': '35858-384834-3843'},\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'pacs_aet': 'gepacs',\n",
" 'pacs_name': 'GE PACS',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_id': '1234578',\n",
" 'patient_birth_date': '1976-05-23',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'study_datetime': '2000-08-10T01:07:00+02:00',\n",
" 'body': 'This is the report',\n",
" 'created_at': '2024-04-11T11:01:51.472801+02:00',\n",
" 'updated_at': '2024-04-11T11:01:51.472808+02:00',\n",
" 'groups': [2],\n",
" 'documents': {'vespa': {'pathId': '/document/v1/report/report/docid/gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'id': 'id:report:report::gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'fields': {'pacs_name': 'GE PACS',\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'language': 'en',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_birth_date': 201657600,\n",
" 'body': 'This is the report',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'groups': [2],\n",
" 'patient_age': 24,\n",
" 'pacs_aet': 'gepacs',\n",
" 'study_datetime': 965862420}}}}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# Fetch a report with all document representations in full text search databases\n",
"\n",
"response = requests.get(\n",
" base_url + f\"reports/{document_id}\",\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
" params={\"full\": True},\n",
")\n",
"\n",
Expand All @@ -271,62 +135,24 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [204]>\n"
]
}
],
"outputs": [],
"source": [
"# Delete a report\n",
"\n",
"response = requests.delete(\n",
" base_url + f\"reports/{document_id}\",\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 1004,\n",
" 'language': 'en',\n",
" 'metadata': {'study_instance_uid': '34343-34343-34343',\n",
" 'accession_number': '345348389',\n",
" 'series_instance_uid': '34343-676556-3343',\n",
" 'sop_instance_uid': '35858-384834-3843'},\n",
" 'modalities': ['CT', 'PET'],\n",
" 'document_id': 'gepacs_3dfidii5858-6633i4-ii398841',\n",
" 'pacs_aet': 'gepacs',\n",
" 'pacs_name': 'GE PACS',\n",
" 'pacs_link': 'http://gepacs.com/34343-34343-34343',\n",
" 'patient_id': '1234578',\n",
" 'patient_birth_date': '1976-05-23',\n",
" 'patient_sex': 'M',\n",
" 'study_description': 'CT of the Thorax',\n",
" 'study_datetime': '2000-08-10T00:00:00+00:53',\n",
" 'body': 'This is an upserted report',\n",
" 'created_at': '2024-04-11T11:01:54.724342+02:00',\n",
" 'updated_at': '2024-04-11T11:01:54.724355+02:00',\n",
" 'groups': [2]}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"data = {\n",
" \"document_id\": \"gepacs_3dfidii5858-6633i4-ii398841\",\n",
Expand All @@ -353,7 +179,7 @@
"response = requests.put(\n",
" base_url + f\"reports/{document_id}/\",\n",
" json=data,\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
" params={\"upsert\": True},\n",
")\n",
"\n",
Expand All @@ -362,21 +188,13 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [204]>\n"
]
}
],
"outputs": [],
"source": [
"response = requests.delete(\n",
" base_url + f\"reports/{document_id}\",\n",
" headers={\"Authorization\": f\"Token {auth_token}\"},\n",
" headers={\"Authorization\": f\"Token {token}\"},\n",
")\n",
"print(response)"
]
Expand All @@ -398,7 +216,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.12.3"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit 40005ca

Please sign in to comment.