Python fastapi.Depends() Examples
The following are 30
code examples of fastapi.Depends().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
fastapi
, or try the search function
.
Example #1
Source File: users.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def search_users( q: str, skip: int = 0, limit: int = 100, current_user: UserInDB = Depends(get_current_active_superuser), ): """ Search users, use Bleve Query String syntax: http://blevesearch.com/docs/Query-String-Query/ For typeahead suffix with `*`. For example, a query with: `email:johnd*` will match users with email `johndoe@example.com`, `johndid@example.net`, etc. """ bucket = get_default_bucket() users = crud.user.search(bucket=bucket, query_string=q, skip=skip, limit=limit) return users
Example #2
Source File: users.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def create_user( *, user_in: UserCreate, current_user: UserInDB = Depends(get_current_active_superuser), ): """ Create new user. """ bucket = get_default_bucket() user = crud.user.get(bucket, username=user_in.username) if user: raise HTTPException( status_code=400, detail="The user with this username already exists in the system.", ) user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) if config.EMAILS_ENABLED and user_in.email: send_new_account_email( email_to=user_in.email, username=user_in.username, password=user_in.password ) return user
Example #3
Source File: users.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def update_user_me( *, password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: UserInDB = Depends(get_current_active_user), ): """ Update own user. """ user_in = UserUpdate(**current_user.dict()) if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email bucket = get_default_bucket() user = crud.user.update(bucket, username=current_user.username, user_in=user_in) return user
Example #4
Source File: users.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def update_user( *, username: str, user_in: UserUpdate, current_user: UserInDB = Depends(get_current_active_superuser), ): """ Update a user. """ bucket = get_default_bucket() user = crud.user.get(bucket, username=username) if not user: raise HTTPException( status_code=404, detail="The user with this username does not exist in the system", ) user = crud.user.update(bucket, username=username, user_in=user_in) return user
Example #5
Source File: items.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def read_items( skip: int = 0, limit: int = 100, current_user: UserInDB = Depends(get_current_active_user), ): """ Retrieve items. If superuser, all the items. If normal user, the items owned by this user. """ bucket = get_default_bucket() if crud.user.is_superuser(current_user): docs = crud.item.get_multi(bucket, skip=skip, limit=limit) else: docs = crud.item.get_multi_by_owner( bucket=bucket, owner_username=current_user.username, skip=skip, limit=limit ) return docs
Example #6
Source File: items.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def search_items( q: str, skip: int = 0, limit: int = 100, current_user: UserInDB = Depends(get_current_active_user), ): """ Search items, use Bleve Query String syntax: http://blevesearch.com/docs/Query-String-Query/ For typeahead suffix with `*`. For example, a query with: `title:foo*` will match items containing `football`, `fool proof`, etc. """ bucket = get_default_bucket() if crud.user.is_superuser(current_user): docs = crud.item.search(bucket=bucket, query_string=q, skip=skip, limit=limit) else: docs = crud.item.search_with_owner( bucket=bucket, query_string=q, username=current_user.username, skip=skip, limit=limit, ) return docs
Example #7
Source File: login.py From full-stack-fastapi-couchbase with MIT License | 6 votes |
def login(form_data: OAuth2PasswordRequestForm = Depends()): """ OAuth2 compatible token login, get an access token for future requests. """ bucket = get_default_bucket() user = crud.user.authenticate( bucket, username=form_data.username, password=form_data.password ) if not user: raise HTTPException(status_code=400, detail="Incorrect email or password") elif not crud.user.is_active(user): raise HTTPException(status_code=400, detail="Inactive user") access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES) return { "access_token": create_access_token( data={"username": user.username}, expires_delta=access_token_expires ), "token_type": "bearer", }
Example #8
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_async_raise(state: str = Depends(asyncgen_state_try)): assert state == "asyncgen raise started" raise AsyncDependencyError()
Example #9
Source File: alt_main.py From fastapi with MIT License | 5 votes |
def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): items = crud.get_items(db, skip=skip, limit=limit) return items
Example #10
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_sync_raise(state: str = Depends(generator_state_try)): assert state == "generator raise started" raise SyncDependencyError()
Example #11
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_async_raise_other(state: str = Depends(asyncgen_state_try)): assert state == "asyncgen raise started" raise OtherDependencyError()
Example #12
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_sync_raise_other(state: str = Depends(generator_state_try)): assert state == "generator raise started" raise OtherDependencyError()
Example #13
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_context_b(state: dict = Depends(context_b)): return state
Example #14
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_context_b_bg( tasks: BackgroundTasks, state: dict = Depends(context_b) ): async def bg(state: dict): state[ "sync_bg" ] = f"sync_bg set - b: {state['context_b']} - a: {state['context_a']}" tasks.add_task(bg, state) return state
Example #15
Source File: test_dependency_duplicates.py From fastapi with MIT License | 5 votes |
def sub_duplicate_dependency( item: Item, sub_item: Item = Depends(duplicate_dependency) ): return [item, sub_item]
Example #16
Source File: alt_main.py From fastapi with MIT License | 5 votes |
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): db_user = crud.get_user_by_email(db, email=user.email) if db_user: raise HTTPException(status_code=400, detail="Email already registered") return crud.create_user(db=db, user=user)
Example #17
Source File: alt_main.py From fastapi with MIT License | 5 votes |
def create_item_for_user( user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db) ): return crud.create_user_item(db=db, item=item, user_id=user_id)
Example #18
Source File: alt_main.py From fastapi with MIT License | 5 votes |
def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): users = crud.get_users(db, skip=skip, limit=limit) return users
Example #19
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def generator_state_try(state: Dict[str, str] = Depends(get_state)): state["/sync_raise"] = "generator raise started" try: yield state["/sync_raise"] except SyncDependencyError: errors.append("/sync_raise") finally: state["/sync_raise"] = "generator raise finalized"
Example #20
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_async(state: str = Depends(asyncgen_state)): return state
Example #21
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_context_b_bg(tasks: BackgroundTasks, state: dict = Depends(context_b)): async def bg(state: dict): state["bg"] = f"bg set - b: {state['context_b']} - a: {state['context_a']}" tasks.add_task(bg, state) return state # Sync versions
Example #22
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_context_b_raise(state: dict = Depends(context_b)): assert state["context_b"] == "started b" assert state["context_a"] == "started a" raise OtherDependencyError()
Example #23
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_context_b(state: dict = Depends(context_b)): return state
Example #24
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_raise_other(state: str = Depends(generator_state_try)): assert state == "generator raise started" raise OtherDependencyError()
Example #25
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync_raise(state: str = Depends(generator_state_try)): assert state == "generator raise started" raise SyncDependencyError()
Example #26
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_async_raise(state: str = Depends(asyncgen_state_try)): assert state == "asyncgen raise started" raise AsyncDependencyError()
Example #27
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_sync(state: str = Depends(generator_state)): return state
Example #28
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def get_async(state: str = Depends(asyncgen_state)): return state
Example #29
Source File: test_dependency_contextmanager.py From fastapi with MIT License | 5 votes |
def context_b(state: dict = Depends(context_a)): state["context_b"] = "started b" try: yield state finally: state["context_b"] = f"finished b with a: {state['context_a']}"
Example #30
Source File: tutorial005.py From fastapi with MIT License | 5 votes |
def get_current_user( security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme) ): if security_scopes.scopes: authenticate_value = f'Bearer scope="{security_scopes.scope_str}"' else: authenticate_value = f"Bearer" credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": authenticate_value}, ) try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) username: str = payload.get("sub") if username is None: raise credentials_exception token_scopes = payload.get("scopes", []) token_data = TokenData(scopes=token_scopes, username=username) except (PyJWTError, ValidationError): raise credentials_exception user = get_user(fake_users_db, username=token_data.username) if user is None: raise credentials_exception for scope in security_scopes.scopes: if scope not in token_data.scopes: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Not enough permissions", headers={"WWW-Authenticate": authenticate_value}, ) return user