brainstem-python-api-tools 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Peter C. Petersen - BrainSTEM Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: brainstem_python_api_tools
3
+ Version: 0.1.0
4
+ Summary: A Python toolset for interacting with API of BrainSTEM.
5
+ Home-page: https://github.com/brainstem-org/brainstem_python_api_tools
6
+ Author: BrainSTEM Team
7
+ Author-email: petersen.peter@gmail.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.7
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: requests
16
+ Requires-Dist: jupyter
17
+
18
+ # brainstem_python_api_tools
19
+ Python toolset for interacting with the API of BrainSTEM
20
+
21
+ Please see the tutorial script (brainstem_api_tutorial.py) for example calls
@@ -0,0 +1,4 @@
1
+ # brainstem_python_api_tools
2
+ Python toolset for interacting with the API of BrainSTEM
3
+
4
+ Please see the tutorial script (brainstem_api_tutorial.py) for example calls
@@ -0,0 +1 @@
1
+ from .brainstem_api_client import BrainstemClient, ModelType, PortalType
@@ -0,0 +1,247 @@
1
+ import os
2
+ from getpass import getpass
3
+ import requests
4
+ from requests.models import Response
5
+ import json
6
+ from enum import Enum
7
+
8
+
9
+ class ModelType(Enum):
10
+ project = "project"
11
+ subject = "subject"
12
+ session = "session"
13
+ collection = "collection"
14
+ cohort = "cohort"
15
+ procedure = "procedure"
16
+ behavior = "behavior"
17
+ dataacquisition = "dataacquisition"
18
+ manipulation = "manipulation"
19
+ equipment = "equipment"
20
+ consumablestock = "consumablestock"
21
+ procedurelog = "procedurelog"
22
+ subjectlog = "subjectlog"
23
+ behavioralparadigm = "behavioralparadigm"
24
+ datastorage = "datastorage"
25
+ inventory = "inventory"
26
+ setup = "setup"
27
+ consumable = "consumable"
28
+ hardwaredevice = "hardwaredevice"
29
+ supplier = "supplier"
30
+ brainregion = "brainregion"
31
+ setuptype = "setuptype"
32
+ species = "species"
33
+ strain = "strain"
34
+ strainapproval = "strainapproval"
35
+ journal = "journal"
36
+ laboratory = "laboratory"
37
+ publication = "publication"
38
+ journalapproval = "journalapproval"
39
+ user = "user"
40
+ group = "group"
41
+
42
+
43
+ class PortalType(Enum):
44
+ public = "public"
45
+ private = "private"
46
+ super = "super"
47
+
48
+
49
+ class LoginError(Exception):
50
+ def __str__(self):
51
+ return f'User/password combination incorrect or user does not exist'
52
+
53
+
54
+ class BrainstemClient:
55
+
56
+ def __init__(self,
57
+ token: str = None) -> None:
58
+
59
+ # Server path
60
+ self._address = 'https://www.brainstem.org/api/'
61
+
62
+ if token:
63
+ self._token = token
64
+ else:
65
+ username = input("Please enter your username:")
66
+ password = getpass("Please enter your password:")
67
+
68
+ self._token = self.__set_token_authentication(
69
+ url=self._address + "token/",
70
+ username=username,
71
+ password=password,
72
+ )
73
+
74
+ print("Your authorization token is:\n", self._token)
75
+ print("\nPlease keep it in a safe place.")
76
+
77
+ def __set_token_authentication(self,
78
+ url: str,
79
+ username: str,
80
+ password: str) -> str:
81
+
82
+ headers = {
83
+ "accept": "application/json",
84
+ "Content-Type": "application/json"
85
+ }
86
+ params = {
87
+ "username": username,
88
+ "password": password
89
+ }
90
+ resp = requests.post(url, headers=headers, json=params)
91
+
92
+ if resp.status_code != 200:
93
+ raise LoginError()
94
+
95
+ token = json.loads(resp.text)['token']
96
+ return token
97
+
98
+ def __get_app_from_model(self, modelname: str) -> str:
99
+ app = None
100
+
101
+ if modelname in ['project', 'subject', 'session', 'collection', 'cohort',
102
+ 'projectmembershipinvitation',
103
+ 'projectgroupmembershipinvitation']:
104
+ app = 'stem'
105
+
106
+ elif modelname in ['procedure', 'behavior', 'dataacquisition', 'manipulation',
107
+ 'equipment', 'consumablestock', 'procedurelog', 'subjectlog']:
108
+ app = 'modules'
109
+
110
+ elif modelname in ['behavioralparadigm', 'datastorage', 'inventory',
111
+ 'setup']:
112
+ app = 'personal_attributes'
113
+
114
+ elif any([x in modelname for x in
115
+ ['consumable', 'hardwaredevice', 'supplier']]):
116
+ app = 'resources'
117
+
118
+ elif any([x in modelname for x in
119
+ ['brainregion', 'setuptype',
120
+ 'species', 'strain']]):
121
+ app = 'taxonomies'
122
+
123
+ elif any([x in modelname for x in
124
+ ['journal', 'laboratory', 'publication']]):
125
+ app = 'attributes'
126
+
127
+ elif modelname in ['user', 'group', 'groupmembershipinvitation',
128
+ 'groupmembershiprequest']:
129
+ app = 'users'
130
+
131
+ return app
132
+
133
+ def load_model(self,
134
+ model: ModelType,
135
+ portal: PortalType = "private",
136
+ id: str = None,
137
+ options: str = None,
138
+ filters: dict = None,
139
+ sort: list = None,
140
+ include: list = None) -> Response:
141
+
142
+ app = self.__get_app_from_model(model)
143
+ if app is None:
144
+ resp = Response()
145
+ resp.status_code = 404
146
+ return resp
147
+
148
+ if options is None:
149
+ options = ""
150
+
151
+ if id:
152
+ query_parameters = id + "/" + options
153
+ else:
154
+ query_parameters = ""
155
+
156
+ if filters:
157
+ for key in filters.keys():
158
+ if query_parameters == "":
159
+ prefix = "?"
160
+ else:
161
+ prefix = "&"
162
+ query_parameters += (prefix
163
+ + "filter{" + key + "}="
164
+ + filters[key])
165
+ if sort:
166
+ for elem in sort:
167
+ if query_parameters == "":
168
+ prefix = "?"
169
+ else:
170
+ prefix = "&"
171
+ query_parameters += prefix + "sort[]=" + elem
172
+
173
+ if include:
174
+ for elem in include:
175
+ if query_parameters == "":
176
+ prefix = "?"
177
+ else:
178
+ prefix = "&"
179
+ query_parameters += prefix + "include[]=" + elem + ".*"
180
+
181
+ request_url = (self._address + portal
182
+ + "/" + app + "/" + model
183
+ + "/" + query_parameters)
184
+
185
+ resp = requests.get(request_url,
186
+ headers={"Authorization": "Bearer %s"
187
+ % self._token})
188
+ return resp
189
+
190
+ def save_model(self,
191
+ model: ModelType,
192
+ portal: PortalType = "private",
193
+ id: str = None,
194
+ data: dict = None,
195
+ options: str = None) -> Response:
196
+
197
+ app = self.__get_app_from_model(model)
198
+ if app is None:
199
+ resp = Response()
200
+ resp.status_code = 404
201
+ return resp
202
+
203
+ if data is None:
204
+ data = {}
205
+
206
+ if options is None:
207
+ options = ""
208
+
209
+ # Check if entry already exists
210
+ if id is not None:
211
+ # This is an update request
212
+ request_url = (self._address + portal + "/" + app + "/" + model
213
+ + "/" + id + "/" + options)
214
+ resp = requests.patch(request_url, json=data,
215
+ headers={"Authorization": "Bearer %s"
216
+ % self._token})
217
+
218
+ else:
219
+ # This is a create request
220
+ request_url = (self._address + portal + "/" + app + "/"
221
+ + model + "/" + options)
222
+ resp = requests.post(request_url, json=data,
223
+ headers={"Authorization": "Bearer %s"
224
+ % self._token})
225
+
226
+ return resp
227
+
228
+ def delete_model(self,
229
+ model: ModelType,
230
+ portal: PortalType = "private",
231
+ id: str = None):
232
+
233
+ app = self.__get_app_from_model(model)
234
+ if app is None:
235
+ resp = Response()
236
+ resp.status_code = 404
237
+ return resp
238
+
239
+ # Check if entry already exists
240
+ if id is not None:
241
+ request_url = (self._address + portal + "/" + app + "/" + model
242
+ + "/" + id + "/")
243
+ resp = requests.delete(request_url,
244
+ headers={"Authorization": "Bearer %s"
245
+ % self._token})
246
+
247
+ return resp
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: brainstem_python_api_tools
3
+ Version: 0.1.0
4
+ Summary: A Python toolset for interacting with API of BrainSTEM.
5
+ Home-page: https://github.com/brainstem-org/brainstem_python_api_tools
6
+ Author: BrainSTEM Team
7
+ Author-email: petersen.peter@gmail.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.7
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: requests
16
+ Requires-Dist: jupyter
17
+
18
+ # brainstem_python_api_tools
19
+ Python toolset for interacting with the API of BrainSTEM
20
+
21
+ Please see the tutorial script (brainstem_api_tutorial.py) for example calls
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ setup.py
5
+ brainstem_api_tools/__init__.py
6
+ brainstem_api_tools/brainstem_api_client.py
7
+ brainstem_python_api_tools.egg-info/PKG-INFO
8
+ brainstem_python_api_tools.egg-info/SOURCES.txt
9
+ brainstem_python_api_tools.egg-info/dependency_links.txt
10
+ brainstem_python_api_tools.egg-info/requires.txt
11
+ brainstem_python_api_tools.egg-info/top_level.txt
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,24 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="brainstem_python_api_tools",
5
+ version="0.1.0",
6
+ packages=find_packages(),
7
+ install_requires=[
8
+ "requests",
9
+ "jupyter",
10
+ ],
11
+ author="BrainSTEM Team",
12
+ author_email="petersen.peter@gmail.com",
13
+ description="A Python toolset for interacting with API of BrainSTEM.",
14
+ long_description=open("README.md").read(),
15
+ long_description_content_type="text/markdown",
16
+ url="https://github.com/brainstem-org/brainstem_python_api_tools",
17
+ license="MIT",
18
+ classifiers=[
19
+ "Programming Language :: Python :: 3",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Operating System :: OS Independent",
22
+ ],
23
+ python_requires='>=3.7',
24
+ )