File size: 6,804 Bytes
9d54b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/schaff/Documents/workspace/vcell/python-restclient/.venv/lib/python3.12/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field \"model_key\" has conflict with protected namespace \"model_\".\n",
      "\n",
      "You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from vcell_client.api_client import ApiClient, Configuration\n",
    "from vcell_client.auth.auth_utils import login_interactive\n",
    "from vcell_client import Publication\n",
    "from vcell_client.api.publication_resource_api import PublicationResourceApi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T21:12:53.063024Z",
     "start_time": "2024-05-01T21:12:52.850129Z"
    }
   },
   "id": "5e183b4a68315dfe",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "# VCell API Client usage\n",
    "1. as a public user (no authentication)\n",
    "2. as an authenticated user (with authentication)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d11f5830839dbfb4"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1) Create anonymous API Client - access to public resources"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "524d1696c6c50b83"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "api_url: str = \"https://vcell-dev.cam.uchc.edu\"  # vcell base url\n",
    "\n",
    "public_client = ApiClient(configuration=Configuration(host=api_url))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T21:13:12.872880Z",
     "start_time": "2024-05-01T21:13:12.870503Z"
    }
   },
   "id": "902c6d3a8dc70af",
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Test Publication API - without authentication\n",
    "1. get publications - doesn't need authentication/authorization"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b12dfb281d589869"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Publication(pub_key=270069811, title='K+ and pH homeostasis in plant cells is controlled by a synchronized K+ /H+ antiport at the plasma and vacuolar membrane', authors=['Li, K.', 'Grauschopf, C.', 'Hedrich, R.', 'Dreyer, I.', 'Konrad, K. R.'], year=2024, citation='New Phytol . 2024 Feb;241(4):1525-1542', pubmedid='38017688', doi='10.1111/nph.19436', endnoteid=None, url=None, wittid=-1, biomodel_refs=[BiomodelRef(bm_key=270051643, name='Li_et_al_2024_NewPhytol_Guard_Cell_Homeostats', owner_name='idreyer', owner_key=97134174, version_flag=3)], mathmodel_refs=[], var_date=datetime.date(2023, 11, 28))"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "public_publication_api = PublicationResourceApi(public_client)\n",
    "pubs: list[Publication] = public_publication_api.get_publications()\n",
    "display(pubs[0])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T21:13:15.493227Z",
     "start_time": "2024-05-01T21:13:15.190372Z"
    }
   },
   "id": "9ba5b1ac2df17231",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2) invoke VCell API as an authenticated user\n",
    "This will open a browser window to login into VCell using Auth0.  After login, the browser will redirect to the VCell webapp '/success_login'.  `api_client` will be authenticated for VCell API requests.  "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6444e342beaccd4d"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f56d96abf59682dd",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T21:14:26.398587Z",
     "start_time": "2024-05-01T21:14:25.485352Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'may loose focus to the browser window for login, switch back to this notebook after login.'"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "client_id: str = 'cjoWhd7W8A8znf7Z7vizyvKJCiqTgRtf' # default client id for standalone VCell clients\n",
    "issuer_url: str = 'https://dev-dzhx7i2db3x3kkvq.us.auth0.com' # Auth0 issuer url for VCell\n",
    "\n",
    "authenticated_client = login_interactive(api_base_url=api_url, client_id=client_id, issuer_url=issuer_url)\n",
    "display(\"may loose focus to the browser window for login, switch back to this notebook after login.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0e558b807038809",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Test Publication API\n",
    "1. get publications - also doesn't need authentication/authorization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f95b36e4437ccbb",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T21:14:34.427704Z",
     "start_time": "2024-05-01T21:14:34.148156Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Publication(pub_key=270069811, title='K+ and pH homeostasis in plant cells is controlled by a synchronized K+ /H+ antiport at the plasma and vacuolar membrane', authors=['Li, K.', 'Grauschopf, C.', 'Hedrich, R.', 'Dreyer, I.', 'Konrad, K. R.'], year=2024, citation='New Phytol . 2024 Feb;241(4):1525-1542', pubmedid='38017688', doi='10.1111/nph.19436', endnoteid=None, url=None, wittid=-1, biomodel_refs=[BiomodelRef(bm_key=270051643, name='Li_et_al_2024_NewPhytol_Guard_Cell_Homeostats', owner_name='idreyer', owner_key=97134174, version_flag=3)], mathmodel_refs=[], var_date=datetime.date(2023, 11, 28))"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "publication_api = PublicationResourceApi(authenticated_client)\n",
    "pubs: list[Publication] = publication_api.get_publications()\n",
    "display(pubs[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed266f9194abb748",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}