コンテンツにスキップ
per page, with , order by , clip by
Results of 1 - 10 of about 55 for Code (0.013 sec.)
SQL (Relational) Databases 11279
SQL (Relational) Databases Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . FastAPI doesn't require you to use a SQL (relational) database. But you can use any data ... True ) name : str = Field ( index = True ) age : int | None = Field ( default = None , index = True ) secret_name : str # Code below omitted ? ? Full file preview Python 3.10+ from typing import Annotated from fastapi import Depends , FastAPI , HTTP ... int , session : SessionDep ) -> Hero : hero = session . get ( Hero , hero_id ) if not hero : raise HTTPException ( status_code = 404 , detail = "Hero not found" ) return hero @app . delete ( "/heroes/ {hero_id} " ) def delete_hero ( hero_id : int , ... session : SessionDep ): hero = session . get ( Hero , hero_id ) if not hero : raise HTTPException ( status_code = 404 , detail = "Hero not found" ) session . delete ( hero ) session . commit () return { "ok" : True } ? Other versions ...
https://man.mtool.work/fastapi/0.115/tutorial/sql-databases/
Simple OAuth2 with Password and Bearer 11028
Simple OAuth2 with Password and Bearer Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Now let's build from the previous chapter and add the missing parts to have ... as other characters like : or if it is a URL. Those details are implementation specific. For OAuth2 they are just strings. Code to get the username and password Now let's use the utilities provided by FastAPI to handle this. OAuth2PasswordRequestForm ... _user ( db , username : str ): if username in db : user_dict = db [ username ] return UserInDB ( ** user_dict ) def fake_decode_token ( token ): # This doesn't provide any security at all # Check the next version user = get_user ( fake_users_db , tok ... en ) return user async def get_current_user ( token : Annotated [ str , Depends ( oauth2_scheme )]): user = fake_decode_token ( token ) if not user : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Invalid authe ...
https://man.mtool.work/fastapi/0.115/tutorial/security/simple-oauth2/
テスト 10777
テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を ... Hello World" } client = TestClient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } 豆知識 テスト関数は async def ではなく、通常の def であること ... ain import app client = TestClient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } テスト: 例の拡張 次に、この例を拡張し、詳細を追加して、さまざ ... def read_main ( item_id : str , x_token : str = Header ()): if x_token != fake_secret_token : raise HTTPException ( status_code = 400 , detail = "Invalid X-Token header" ) if item_id not in fake_db : raise HTTPException ( status_code = 404 , detail = ...
https://man.mtool.work/fastapi/0.115/tutorial/testing/
OAuth2 scopes 10555
OAuth2 scopes Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can use OAuth2 scopes directly with FastAPI , they are integrated to work seamlessly. This would ... . Nevertheless, you still enforce those scopes, or any other security/authorization requirement, however you need, in your code. In many cases, OAuth2 with scopes can be an overkill. But if you know you need it, or you are curious, keep reading. OAut ... ssword ): return False return user def create_access_token ( data : dict , expires_delta : timedelta | None = None ): to_encode = data . copy () if expires_delta : expire = datetime . now ( timezone . utc ) + expires_delta else : expire = datetime . ... now ( timezone . utc ) + timedelta ( minutes = 15 ) to_encode . update ({ "exp" : expire }) encoded_jwt = jwt . encode ( to_encode , SECRET_KEY , algorithm = ALGORITHM ) return encoded ...
https://man.mtool.work/fastapi/0.115/advanced/security/oauth2-scopes/
Development - Contributing 10156
Development - Contributing Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . First, you might want to see the basic ways to help FastAPI and get help . Developing If ... you already cloned the fastapi repository and you want to deep dive in the code, here are some guidelines to set up your environment. Virtual environment Follow the instructions to create and activate a ... virtual environment for the internal code of fastapi . Install requirements using pip After activating the environment, install the required packages: $ pip install ... rts and uses FastAPI, and run it with the Python from your local environment, it will use your cloned local FastAPI source code. And if you update that local FastAPI source code when you run that Python file again, it will use the fresh version of Fa ...
https://man.mtool.work/fastapi/0.115/contributing/
path operationデコレータの依存関係 9757
path operationデコレータの依存関係 場合によっては path operation関数 の中で依存関係の戻り値を本当に必要としないこともあります。 もしくは、依存関係が値を返さない場合もあります。 しかし、それでも実行・解決する必要があります。 このような場合、 ... sync 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-secre ... t-key" : raise HTTPException ( status_code = 400 , detail = "X-Key header invalid" ) return x_key @app . get ( "/items/" , dependencies = [ Depends ( verify_token ), ... fy_token ( x_token : Annotated [ 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 : Annotated [ str , Header ()]): if x_key != "fak ...
https://man.mtool.work/fastapi/0.115/tutorial/dependencies/dependencies-in-path-...
Response - Change Status Code 9683
Response - Change Status Code Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You probably read before that you can set a default Response Status Code . But in som ... e cases you need to return a different status code than the default. Use case For example, imagine that you want to return an HTTP status code of "OK" 200 by default. But if ... the data didn't exist, you want to create it, and return an HTTP status code of "CREATED" 201 . But you still want to be able to filter and convert the data you return with a response_model . For tho ... of type Response in your path operation function (as you can do for cookies and headers). And then you can set the status_code in that temporal response object. Python 3.8+ from fastapi import FastAPI , Response , status app = FastAPI () tasks = { " ...
https://man.mtool.work/fastapi/0.115/advanced/response-change-status-code/
パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer 9313
パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer これでセキュリティの流れが全てわかったので、 JWT トークンと安全なパスワードのハッシュ化を使用して、実際にアプリケーションを安全にしてみましょう。 このコードは、アプリケーシ ... return False return user def create_access_token ( data : dict , expires_delta : Union [ timedelta , None ] = None ): to_encode = data . copy () if expires_delta : expire = datetime . now ( timezone . utc ) + expires_delta else : expire = datetime . ... now ( timezone . utc ) + timedelta ( minutes = 15 ) to_encode . update ({ "exp" : expire }) encoded_jwt = jwt . encode ( to_encode , SECRET_KEY , algorithm = ALGORITHM ) return encoded ... _jwt async def get_current_user ( token : str = Depends ( oauth2_scheme )): credentials_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : "Bearer" } ...
https://man.mtool.work/fastapi/0.115/tutorial/security/oauth2-jwt/
Custom Response Classes - File, HTML, Redirect, Streaming, etc. 8988
Custom Response Classes - File, HTML, Redirect, Streaming, etc. There are several custom response classes you can use to create an instance and return them directly from your path operations . Read more about it in the FastAPI docs for Cust ... s, you can use them to optimize JSON performance. ::: fastapi.responses.UJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie ::: fastapi. ... responses.ORJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie Starlette Re ... sponses ::: fastapi.responses.FileResponse options: members: - chunk_size - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie ::: fastapi. ...
https://man.mtool.work/fastapi/0.115/reference/responses/
Virtual Environments 8707
Virtual Environments Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . When you work in Python projects you probably should use a virtual environment (or a similar m ... ry uv . Create a Project First, create a directory for your project. What I normally do is that I create a directory named code inside my home/user directory. And inside of that I create one directory per project. // Go to the home directory $ cd // ... Create a directory for all your code projects $ mkdir code // Enter into that code directory $ cd code // Create a directory for this project $ mkdir awesome-p ... ou are using the virtual environment you intended. Linux, macOS, Windows Bash Windows PowerShell $ which python /home/user/code/awesome-project/.venv/bin/python If it shows the python binary at .venv/bin/python , inside of your project (in this case ...
https://man.mtool.work/fastapi/0.115/virtual-environments/
PREV 1 2 3 4 5 6 NEXT