-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
295 lines (271 loc) · 11.2 KB
/
api.py
File metadata and controls
295 lines (271 loc) · 11.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# MIT License
#
# Copyright (c) 2022 Mathieu Imfeld
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import uuid
from fastapi import APIRouter, Depends, status, HTTPException
from fastapi.responses import Response
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from mrmat_python_api_fastapi.db import get_db
from mrmat_python_api_fastapi.apis import StatusSchema
from .db import Resource, Owner
from .schema import (
ResourceSchema,
ResourceInputSchema,
ResourceListSchema,
OwnerSchema,
OwnerInputSchema,
OwnerListSchema
)
router = APIRouter()
@router.get('/resources',
name='list_resources',
summary='List all known resources',
description='Returns all currently known resources and their metadata',
response_model=ResourceListSchema)
async def get_resources(session: Session = Depends(get_db)) -> ResourceListSchema:
try:
resources = session.query(Resource).all()
return ResourceListSchema(resources=[ResourceSchema(uid=r.uid,
name=r.name,
owner_uid=r.owner_uid)
for r in resources])
except SQLAlchemyError as e:
# Handle the error appropriately, maybe raise an HTTPException
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.get('/resources/{uid}',
name='get_resource',
summary='Get a single resource',
description='Return a single resource identified by its resource id',
responses={
status.HTTP_404_NOT_FOUND: {
'description': 'The resource was not found',
'model': StatusSchema
},
status.HTTP_200_OK: {
'description': 'The requested resource',
'model': ResourceSchema
}
})
async def get_resource(uid: str,
response: Response,
session: Session = Depends(get_db)):
try:
resource = session.get(Resource, uid)
if not resource:
response.status_code = 404
return StatusSchema(code=404, msg='The resource was not found')
return resource
except SQLAlchemyError as e:
# Handle the error appropriately, maybe raise an HTTPException
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.post('/resources',
name='create_resource',
summary='Create a resource',
description='Create a resource owned by the authenticated user',
response_model=ResourceSchema)
async def create_resource(data: ResourceInputSchema,
response: Response,
session: Session = Depends(get_db)):
try:
resource = Resource(uid=str(uuid.uuid4()), name=data.name, owner_uid=data.owner_uid)
session.add(resource)
session.commit()
response.status_code = 201
return resource
except SQLAlchemyError as e:
session.rollback()
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.put('/resources/{uid}',
name='modify_resource',
summary='Modify a resource',
description='Modify a resource by its resource id',
responses={
status.HTTP_200_OK: {
'description': 'The resource was modified',
'model': ResourceSchema
},
status.HTTP_404_NOT_FOUND: {
'description': 'The resource was not found',
'model': StatusSchema
}
},
response_model=ResourceSchema)
async def modify_resource(uid: str,
data: ResourceInputSchema,
response: Response,
session: Session = Depends(get_db)):
try:
resource = session.get(Resource, uid)
if not resource:
response.status_code = 404
return StatusSchema(code=404, msg='Not found')
resource.name = data.name
session.add(resource)
session.commit()
return resource
except SQLAlchemyError as e:
session.rollback()
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.delete('/resources/{uid}',
name='remove_resource',
summary='Remove a resource',
description='Remove a resource by its resource id',
status_code=204,
responses={
status.HTTP_204_NO_CONTENT: {
'description': 'The resource was removed'
},
status.HTTP_410_GONE: {
'description': 'The resource was already gone',
'model': StatusSchema
}
})
async def remove_resource(uid: str,
response: Response,
session: Session = Depends(get_db)):
try:
resource = session.get(Resource, uid)
if not resource:
response.status_code = 410
return StatusSchema(code=410, msg='The resource was already gone')
session.delete(resource)
session.commit()
response.status_code = 204
return {}
except SQLAlchemyError as e:
session.rollback()
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.get('/owners',
name='list_owners',
summary='List all known owners',
description='Returns all currently known owners and their metadata',
response_model=OwnerListSchema)
async def get_owners(session: Session = Depends(get_db)):
try:
owners = session.query(Owner).all()
return OwnerListSchema(owners=[OwnerSchema(uid=o.uid, name=o.name) for o in owners])
except SQLAlchemyError as e:
# Handle the error appropriately, maybe raise an HTTPException
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.get('/owners/{uid}',
name='get_owner',
summary='Get a single owner',
description='Return a single owner identified by its owner id',
responses={
status.HTTP_404_NOT_FOUND: {
'description': 'The owner was not found',
'model': StatusSchema
},
status.HTTP_200_OK: {
'description': 'The requested owner',
'model': OwnerSchema
}
},
response_model=OwnerSchema)
async def get_owner(uid: str,
response: Response,
session: Session = Depends(get_db)):
try:
owner = session.get(Owner, uid)
if not owner:
response.status_code = 404
return StatusSchema(code=404, msg='The owner was not found')
return owner
except SQLAlchemyError as e:
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.post('/owners',
name='create_owner',
summary='Create a owner',
description='Create a owner',
response_model=OwnerSchema)
async def create_owner(data: OwnerInputSchema,
response: Response,
session: Session = Depends(get_db)):
try:
owner = Owner(uid=str(uuid.uuid4()), name=data.name)
session.add(owner)
session.commit()
response.status_code = 201
return owner
except SQLAlchemyError as e:
session.rollback()
# Handle the error appropriately, maybe raise an HTTPException
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.put('/owners/{uid}',
name='modify_owner',
summary='Modify a owner',
description='Modify a owner by its owner id',
responses={
status.HTTP_200_OK: {
'description': 'The owner was modified',
'model': OwnerSchema
},
status.HTTP_404_NOT_FOUND: {
'description': 'The owner was not found',
'model': StatusSchema
}
},
response_model=OwnerSchema)
async def modify_owner(uid: str,
data: OwnerInputSchema,
response: Response,
session: Session = Depends(get_db)):
try:
owner = session.get(Owner, uid)
if not owner:
response.status_code = 404
return StatusSchema(code=404, msg='The owner was not found')
owner.name = data.name
session.add(owner)
session.commit()
return owner
except SQLAlchemyError as e:
session.rollback()
raise HTTPException(status_code=500, detail="A database error occurred") from e
@router.delete('/owners/{uid}',
name='remove_owner',
summary='Remove a owner',
description='Remove a owner by its owner id',
status_code=204,
responses={
status.HTTP_204_NO_CONTENT: {
'description': 'The owner was removed'
},
status.HTTP_410_GONE: {
'description': 'The owner was already gone',
'model': StatusSchema
}
})
async def remove_owner(uid: str,
response: Response,
session: Session = Depends(get_db)):
try:
owner = session.get(Owner, uid)
if not owner:
response.status_code = status.HTTP_410_GONE
return
session.delete(owner)
session.commit()
response.status_code = status.HTTP_204_NO_CONTENT
except SQLAlchemyError as e:
session.rollback()
raise HTTPException(status_code=500, detail="A database error occurred") from e