-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastAPI_tutorial.py
More file actions
1466 lines (1060 loc) · 44.6 KB
/
FastAPI_tutorial.py
File metadata and controls
1466 lines (1060 loc) · 44.6 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# requirement.txt
fastapi
uvicorn[standard]
pydantic[email]
python-multipart
python-jose[cryptography]
passlib[bcrypt]
SQLAlchemy
# Mainfile: main.py
from datetime import datetime, time, timedelta
from enum import Enum
import time
from typing import Literal, Union
from uuid import UUID
from fastapi import (
Body,
Depends,
FastAPI,
Query,
Path,
Cookie,
Header,
status,
Form,
File,
UploadFile,
HTTPException,
Request,
BackgroundTasks,
)
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.exception_handlers import (
http_exception_handler,
request_validation_exception_handler,
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, PlainTextResponse
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel, Field, HttpUrl, EmailStr
from passlib.context import CryptContext
from jose import jwt, JWTError
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import HTMLResponse
app = FastAPI()
@app.get("/")
async def get():
return {"message": "hello world"}
'''
on the command line enter 'uvicorn main:app' main==filename, app==fastapi instance.
other alternatives 'uvicorn main:app --reload' == helps when you make changes and want to see them
alternative 2" 'uvicron main:app --port=9000' == assigning a port. without this, it defaults to 8000
'localhost:8000/docs': an interractive interface created by fastapi one can assess each route command
'''
@app.post("/")
async def post():
return {"message": 'hello from the post route'}
@app.put("/")
async def put():
return {"message": 'hello from the put route'}
@app.get("/items")
async def list_items():
return {"message": 'list items route'} # the ./items url should work too
@app.get("/items/{item_id}")
async def get_items(item_id: int):
return {"item_id": item_id} # the ./items/5 should work, prints 'item_id: 5' other data type won't
# Type of route commands
@app.get("/items/{item_id}") # Retrieve data
@app.post("/items/") # creates new data
@app.put("/items/{item_id}") #updates existing data
@app.delete("/items/{item_id}") #delete existing data
@app.options("/items/") #describes a communication
@app.head("/items/") #Retrive head without the body
@app.patch("/items/{item_id}") #partially update existing data
@app.copy("/items/{item_id}") #copy a resource to a new location
@app.link("/items/{item_id}") #establish linkes between resources
@app.unlink("/items/{item_id}") #remove links between resources
# using specific types
from enum import Enum
class FoodEnum(str, Enum):
fruits = 'fruits'
vegetables = 'vegetables'
diary = 'dairy'
@app.get("/foods/{food_name}")
async def get_food(food_name: FoodEnum):
if food_name == FoodEnum.vegetables:
return {"food_name": food_name, "message": "you are healthy"}
if food_name.value == "fruits":
return {
"food_name": food_name,
"message": "you are still healthy, but like sweet things",
}
return {"food_name": food_name, "message": "i like chocolate milk"}
# on the browser, localhost:8000/foods/{food_name} or localhost:8000/foods/docs
# if food_name is either fruits, vegetable or diary, the message under will print. in docs, it will be interractive.
# sample message 'food_name': vegetable, "message": 'you are healthy'
'''Querying Parameters''' # they are not defined in the get, only in the function. Path parameters are defined in both
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
@app.get("/items")
async def list_items(skip: int = 0, limit: int = 10): # variables are only passed in here
return fake_items_db[skip : skip + limit] #localhost:8000/items?skip=2, or localhost:8000/items?limit=1 [skip: skip + limit] is indexing
@app.get("/items/{item_id}") # path parameter is passed here. query parameters are also passed in inside function get_item
async def get_item(
item_id: str, sample_query_param: str, q: str | None = None, short: bool = False #str | None=None is a hint type. read as str or None but default is None.
):
item = {"item_id": item_id, "sample_query_param": sample_query_param}
if q:
item.update({"q": q})
if not short:
item.update(
{
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consectetur."
}
)
return item # localhost:8000/items/food?q=world&short=1 path parameters are typed with slash, while the query ones use (? and &)
# the answer will be ('item:food, q: world, description:"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consectetur.")
# NB: short=no, short=True, short=false will work
@app.get("/users/{user_id}/items/{item_id}") # passing multiple path parameters
async def get_user_item(
user_id: int, item_id: str, q: str | None = None, short: bool = False
):
item = {"item_id": item_id, "owner_id": user_id}
if q:
item.update({"q": q})
if not short:
item.update(
{
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consectetur."
}
)
return item
'''Request_Body'''
class Item(BaseModel): # BaseModel was imported from pydantic. 'from pydantic import BaseModel'
name: str #NB: pydantic models do not require the self.name. they are automatically inferred.
description: str | None = None
price: float
tax: float | None = None
@app.post("/items") #post request sends data. the data isn't included in the url like the get.
# url stays same 'localhost:8000/items' items are within the class object.
async def create_item(item: Item):
item_dict = item.dict()
if item.tax:
price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax})
return item_dict
@app.put("/items/{item_id}") #put request makes updates or requests data(as get) if data doesn't exist in that path. it can take in parameters and
#it can also update request updates to the request body. (i.e): 'localhost:8000/items/food' food is a path parameter, q is a query parameter, Items is a class object.
# if we don't set our query parameters to none 'q:str | None = None' they'll be required.
async def create_item_with_put(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result
'''query Parameters string''' # to go to a method definition (the class definition) in vscode, right click the method or press F12, press ALT + F12 for documentation
@app.get("/items")
async def read_items(
q: str # for multiple values 'q: list[str]' signifying that q is a list of strings.
| None = Query( # Query function is imported from FastAPI, it is used for data validation. do not enter values less than 3 or greater than 10, check docs for more
None, # to define query without a default value (None in our case) use '...'. i.e: q: str = Query(..., min_length=)
min_length=3,
max_length=10,
title="Sample query string",
description="This is a sample query string.",
alias="item-query", #sets the new name of q, url will be 'localhost:8000/items?item-query=food' we don't use item-query above directly because python doesnt work well with '-'
)
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results
@app.get("/items_hidden")
async def hidden_query_route(
hidden_query: str | None = Query(None, include_in_schema=False)
):
if hidden_query:
return {"hidden_query": hidden_query}
return {"hidden_query": "Not found"}
'''Path Parameters Numeric'''
@app.get("/items_validation/{item_id}")
async def read_items_validation(
*, #generally we can't have positional arguments after keyword arguments. but if we use the '*' for unpacking positional
# argument, python assumes all the arguments afterwards are keyword arguments. regardless of if it isn't
item_id: int = Path(..., title="The ID of the item to get", gt=10, le=100), # Path works similar to query but for path parameters. 'from FastAPI import Path'
q: str = "hello",
size: float = Query(..., gt=0, lt=7.75)
):
results = {"item_id": item_id, "size": size}
if q:
results.update({"q": q})
return results
'''Body Fields'''
class Item(BaseModel):
name: str
description: str | None = Field( #'Field' works like Query or Path but it is imported from 'pydantic'
None, title="The description of the item", max_length=300
)
price: float = Field(..., gt=0, description="The price must be greater than zero.") # 'ge' = greater than or equal to, 'le' is same
tax: float | None = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(..., embed=True)): #Body is imported from FastAPI. normally, we'll have to use class __(BaseModel) to set a request body parameter.
results = {"item_id": item_id, "item": item} #Alternatively, we could use 'Body' to set it.
return results
'''Nested Models'''
class Image(BaseModel):
url: HttpUrl # HttpUrl is imported from pydantic. the value entered must be a url otherwise it will raise an error
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = []
image: list[Image] | None = None
class Offer(BaseModel):
name: str
description: str | None = None
price: float
items: list[Item]
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
@app.post("/offers")
async def create_offer(offer: Offer = Body(..., embed=True)):
return offer
@app.post("/images/multiple")
async def create_multiple_images(images: list[Image]):
return images
@app.post("/blah")
async def create_some_blahs(blahs: dict[int, float]): # 'dict[int, float]' says key and value should be int and float respectively
return blahs
'''Declare Request'''
class Item(BaseModel):
name: str # using the 'Field' module from pydantic will look like name: str = Field(..., example ='food', description = 'what i want')
description: str | None = None
price: float
tax: float | None = None
# class Config: # using 'class Config' we can set the default values of our request body. another way is to use 'Field' module from pydantic
# schema_extra = {
# "example": {
# "name": "Foo",
# "description": "A very nice Item",
# "price": 16.25,
# "tax": 1.67,
# }
# }
@app.put("/items/{item_id}")
async def update_item(
item_id: int,
item: Item = Body( # using Body on the class object is another way to define the response body
...,
examples={ # examples gives the option of displaying either of the main keys in the dictionary
"normal": { # each key must be entered as 'summary, description and value'. Only the value shows in code window others qualify the value.
"summary": "A normal example",
"description": "A __normal__ item works _correctly_",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 16.25,
"tax": 1.67,
},
},
"converted": {
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {"name": "Bar", "price": "16.25"},
},
"invalid": {
"summary": "Invalid data is rejected with an error",
"description": "Hello youtubers",
"value": {"name": "Baz", "price": "sixteen point two five"},
},
},
),
):
results = {"item_id": item_id, "item": item}
return results
'''Extra Data Type'''
@app.put("/items/{item_id}")
async def read_items(
item_id: UUID, # unique user identifier 'from uuid import UUID'
start_date: datetime | None = Body(None),
end_date: datetime | None = Body(None), # 'from datetime import datetime, time, timedelta'
repeat_at: time | None = Body(None),
process_after: timedelta | None = Body(None),
):
start_process = start_date + process_after
duration = end_date - start_process
return {
"item_id": item_id,
"start_date": start_date,
"end_date": end_date,
"repeat_at": repeat_at,
"process_after": process_after,
"start_process": start_process,
"duration": duration,
}
# on cli to get uuid to input, see the python code below
from uuid import uuid4
uuid4()
# output is a unique value that can be used as uuid
'''Cookie and Header Parameters'''
# in HTTP, headers are metadata that provide additional info about the message
# cookies are small info that server passes to our browser and back, to track sessions.
@app.get("/items")
async def read_items(
cookie_id: str | None = Cookie(None),
accept_encoding: str | None = Header(None),
sec_ch_ua: str | None = Header(None),
user_agent: str | None = Header(None),
x_token: list[str] | None = Header(None),
):
return {
"cookie_id": cookie_id,
"Accept-Encoding": accept_encoding,
"sec-ch-ua": sec_ch_ua,
"User-Agent": user_agent,
"X-Token values": x_token,
}
'''Response Model'''
# pydantic[email] was added to our requirements.txt EmailStr was imported from pydantic (Email: EmailStr)--a class attribute or parameter
# 'response_model' helps us get more description in the response body
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float = 10.5
tags: list[str] = []
items = {
"foo": {"name": "Foo", "price": 50.2},
"bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
"baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True) # all unassigned variables shouldn't show in output
async def read_item(item_id: Literal["foo", "bar", "baz"]):
return items[item_id]
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return item
class UserBase(BaseModel):
username: str
email: EmailStr
full_name: str | None = None
class UserIn(UserBase): # this is typically how to hide passwords.
password: str
class UserOut(UserBase):
pass
@app.post("/user/", response_model=UserOut) #UserOut inherited the UserBase hence the response model won't display password
async def create_user(user: UserIn):
return user
@app.get(
"/items/{item_id}/name",
response_model=Item,
response_model_include={"name", "description"}, # only those included will show in output
)
async def read_item_name(item_id: Literal["foo", "bar", "baz"]): #'from typing import Literal' helps to valate that only our permitted value is used (foo, bar, baz)
return items[item_id]
@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude={"tax"}) #those excluded won't show even when assigned.
async def read_items_public_data(item_id: Literal["foo", "bar", "baz"]):
return items[item_id]
'''Extra Models'''
class UserBase(BaseModel):
username: str
email: EmailStr
full_name: str | None = None
class UserIn(UserBase):
password: str
class UserOut(UserBase):
pass
class UserInDB(UserBase):
hashed_password: str
def fake_password_hasher(raw_password: str):
return f"supersecret{raw_password}"
def fake_save_user(user_in: UserIn):
hashed_password = fake_password_hasher(user_in.password)
user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password)
print("User 'saved'.")
return user_in_db
@app.post("/user/", response_model=UserOut)
async def create_user(user_in: UserIn):
user_saved = fake_save_user(user_in)
return user_saved
class BaseItem(BaseModel):
description: str
type: str
class CarItem(BaseItem):
type = "car"
class PlaneItem(BaseItem):
type = "plane"
size: int
items = {
"item1": {"description": "All my friends drive a low rider", "type": "car"},
"item2": {
"description": "Music is my aeroplane, it's my aeroplane",
"type": "plane",
"size": 5,
},
}
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem]) # 'from typing import Union'
async def read_item(item_id: Literal["item1", "item2"]):
return items[item_id]
class ListItem(BaseModel):
name: str
description: str
list_items = [
{"name": "Foo", "description": "There comes my hero"},
{"name": "Red", "description": "It's my aeroplane"},
]
@app.get("/list_items/", response_model=list[ListItem]) #passing in the variable as the route. 'list_items'
async def read_items():
return items
@app.get("/arbitrary", response_model=dict[str, float])
async def get_arbitrary():
return {"foo": 1, "bar": "2"}
'''Response Status'''
@app.post("/items/", status_code=status.HTTP_201_CREATED) #status is imported from FastAPI, learn more about response code from HTTP documentation
async def create_item(name: str):
return {"name": name}
@app.delete("/items/{pk}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(pk: str):
print("pk", pk)
return pk
@app.get("/items/", status_code=status.HTTP_302_FOUND)
async def read_items_redirect():
return {"hello": "world"}
'''Form field'''
@app.post("/login/")
async def login(username: str = Form(...), password: str = Body(...)): #Forms is imported from FastAPI, fields make our respons body like json, while forms make it like an excel cell.
print("password", password)
return {"username": username}
@app.post("/login-json/")
async def login_json(username: str = Body(...), password: str = Body(...)):
print("password", password)
return {"username": username}
'''Request Files'''
@app.post("/files/")
async def create_file(
files: list[bytes] = File(..., description="A file read as bytes") # bytes objects make it such that we can upload a file, 'File' and 'UploadFile' are imported from FastAPI
):
return {"file_sizes": [len(file) for file in files]}
@app.post("/uploadfile/")
async def create_upload_file(
files: list[UploadFile] = File(..., description="A file read as UploadFile") # 'UploadFile' also makes it possible to upload a file this is better because it can work with multiple file types
): # 'await file.read()' reads the content of the file.
return {"filename": [file.filename for file in files]}
@app.get("/")
async def main():
content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
"""
return HTMLResponse(content=content)
'''Request Forms and Files'''
@app.post("/files/")
async def create_file(
file: bytes = File(...),
fileb: UploadFile = File(...),
token: str = Form(...),
hello: str = Body(...),
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
"hello": hello,
}
'''Handling Errors'''
items = {"foo": "The Foo Wrestlers"}
@app.get("/items/{item_id}")
async def read_item(item_id: str):
if item_id not in items:
raise HTTPException( # 'HTTPException' is imported from FastAPI
status_code=404,
detail="Item not found",
headers={"X-Error": "There goes my error"},
)
return {"item": items[item_id]}
class UnicornException(Exception):
def __init__(self, name: str):
self.name = name
@app.exception_handler(UnicornException) # exception_handler is another kind of request for raising exceptions
async def unicorn_exception_handler(request: Request, exc: UnicornException): # Request is installed from FastAPI,
return JSONResponse( # JSONResponse is installed from FastAPI.responses
status_code=418,
content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
)
@app.get("/unicorns/{name}")
async def read_unicorns(name: str):
if name == "yolo":
raise UnicornException(name=name)
return {"unicorn_name": name}
# @app.exception_handler(RequestValidationError) # imported 'from FastAPI.exceptions import RequestValidationError'
# async def validation_exception_handler(request, exc):
# return PlainTextResponse(str(exc), status_code=400)
#
#
# @app.exception_handler(StarletteHTTPException) #HTTPException can also be imported from starlette, we just used an alias
# async def http_exception_handler(request, exc):
# return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
#
#
# @app.get("/validation_items/{item_id}")
# async def read_validation_items(item_id: int):
# if item_id == 3:
# raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
# return {"item_id": item_id}
# @app.exception_handler(RequestValidationError)
# async def validation_exception_handler(request: Request, exc: RequestValidationError):
# return JSONResponse(
# status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
# content=jsonable_encoder({"detail": exc.errors(), "blahblah": exc.body}), # from FastAPI.encoders import 'jsonable_encoder'
# )
#
#
# class Item(BaseModel):
# title: str
# size: int
#
#
# @app.post("/items/")
# async def create_item(item: Item):
# return item
@app.exception_handler(StarletteHTTPException) # HTTPException is imported from FastAPI, by hovering a module, vscode will tell what package to import if from.
async def custom_http_exception_handler(request, exc):
print(f"OMG! An HTTP error!: {repr(exc)}")
return await http_exception_handler(request, exc) # from FastAPI.exception_handler import 'http_exception_handler'
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
print(f"OMG! The client sent invalid data!: {exc}")
return await request_validation_exception_handler(request, exc) # from FastAPI.exception_handler import 'request_validation_exception_handler'
@app.get("/blah_items/{item_id}")
async def read_items(item_id: int):
if item_id == 3:
raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
return {"item_id": item_id}
'''Path Operation Configuration'''
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
class Tags(Enum):
items = "items"
users = "users"
@app.post(
"/items/",
response_model=Item,
status_code=status.HTTP_201_CREATED, # we can also put the response_model, status_code in the decorator
tags=[Tags.items],
summary="Create an Item-type item", # 'tags' helps with organization for the docs
# description="Create an item with all the information: "
# "name; description; price; tax; and a set of "
# "unique tags",
response_description="The created item",
)
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
"""
return item
@app.get("/items/", tags=[Tags.items])
async def read_items():
return [{"name": "Foo", "price": 42}]
@app.get("/users/", tags=[Tags.users])
async def read_users():
return [{"username": "PhoebeBuffay"}]
@app.get("/elements/", tags=[Tags.items], deprecated=True)
async def read_elements():
return [{"item_id": "Foo"}]
'''JSON Compactible Encoder'''
class Item(BaseModel):
name: str | None = None
description: str | None = None
price: float | None = None
tax: float = 10.5
tags: list[str] = []
items = {
"foo": {"name": "Foo", "price": 50.2},
"bar": {
"name": "Bar",
"description": "The bartenders",
"price": 62,
"tax": 20.2,
},
"baz": {
"name": "Baz",
"description": None,
"price": 50.2,
"tax": 10.5,
"tags": [],
},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
return items.get(item_id)
@app.put("/items/{item_id}", response_model=Item) # put request replaces data
def update_item(item_id: str, item: Item):
update_item_encoded = jsonable_encoder(item)
items[item_id] = update_item_encoded
return update_item_encoded
@app.patch("/items/{item_id}", response_model=Item) # patch request partially modifies the data
def patch_item(item_id: str, item: Item):
stored_item_data = items.get(item_id)
if stored_item_data is not None:
stored_item_model = Item(**stored_item_data)
else:
stored_item_model = Item()
update_data = item.dict(exclude_unset=True) # exclude_unset doesn't show those values not reset or replaced.
updated_item = stored_item_model.copy(update=update_data)
items[item_id] = jsonable_encoder(updated_item)
return updated_item
'''Dependencies Intro'''
async def hello():
return "world"
async def common_parameters(
q: str | None = None, skip: int = 0, limit: int = 100, blah: str = Depends(hello)
):
return {"q": q, "skip": skip, "limit": limit, "hello": blah}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)): # 'Depend' is imported from FastAPI. this is used to link variables to multiple request statements.
return commons
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
'''Classes As Dependencies'''
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/{item_id}")
async def read_items(commons: CommonQueryParams = Depends()):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
'''Sub-Dependencies'''
def query_extractor(q: str | None = None):
return q
def query_or_body_extractor(
q: str = Depends(query_extractor), last_query: str | None = Body(None)
):
if q:
return q
return last_query
@app.post("/item")
async def try_query(query_or_body: str = Depends(query_or_body_extractor)):
return {"q_or_body": query_or_body}
'''Dependencies in Path operation decorators'''
async def verify_token(x_token: str = Header(...)):
if x_token != "fake-super-secret-token":
raise HTTPException(status_code=400, detail="X-Token header invalid")
async def verify_key(x_key: str = Header(...)):
if x_key != "fake-super-secret-key":
raise HTTPException(status_code=400, detail="X-Key header invalid")
return x_key
# app = FastAPI(dependencies=[Depends(verify_token), Depends(verify_key)]) # another way to define a dependency whose values we do not need.
@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)]) # Define a dependency in the decorator if you don't need their values within the function.
async def read_items():
return [{"item": "Foo"}, {"item": "Bar"}]
@app.get("/users/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_users():
return [{"username": "Rick"}, {"username": "Morty"}]
'''Security, First Steps'''
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") # 'from FastAPI.security import OAuth2PasswordBearer' creates a login that is required before status_code == 200 (as a form)
fake_users_db = {
"johndoe": dict(
username="johndoe",
full_name="John Doe",
hashed_password="fakehashedsecret",
disabled=False,
),
"alice": dict(
username="alice",
full_name="Alice Wonderson",
hashed_password="fakehashedsecret2",
disabled=True,
),
}
def fake_hash_password(password: str):
return f"fakehashed{password}"
class User(BaseModel):
username: str
email: str | None = None
full_name: str | None = None
disabled: bool | None = None
class UserInDB(User):
hashed_password: str
def get_user(db, username: str):
if username in db:
user_dict = db[username]
return UserInDB(**user_dict)
def fake_decode_token(token):
return get_user(fake_users_db, token)
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = fake_decode_token(token)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return user
async def get_current_active_user(current_user: User = Depends(get_current_user)):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()): # 'from FastAPI.security import OAuth2PasswordRequestForm' this retrieves the data entered into form
user_dict = fake_users_db.get(form_data.username)
if not user_dict:
raise HTTPException(status_code=400, detail="Incorrect username or password")
user = UserInDB(**user_dict)
hashed_password = fake_hash_password(form_data.password) # 'form_data.password' is the password retrieved from the authorization form
if not hashed_password == user.hashed_password:
raise HTTPException(status_code=400, detail="Incorrect username or password")
return {"access_token": user.username, "token_type": "bearer"}
@app.get("/users/me")
async def get_me(current_user: User = Depends(get_current_active_user)):
return current_user
@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
return {"token": token}
'''Security, OAuth2 Beearer and JWT''' #jwt is json web token, it is a hash identifier.
#installed dependencies: python-jose[cryptography], paslib[bcrypt]
from passlib.context import CryptContext # Importation.
from jose import jwt, JWTError