def get_item(id: UUID4, token: str = Depends(oauth2_scheme)): """ get any specific item """ auth_actions.get_current_user(token) item = item_actions.get_item(id) if not item: raise HTTPException(status_code=400, detail="Item not found.") return item
def get_users(skip: int = 0, limit: int = 100, token: str = Depends(oauth2_scheme)): """ get many users """ auth_actions.get_current_user(token) users = user_actions.get_users(skip, limit) return users
def get_user(id: UUID4, token: str = Depends(oauth2_scheme)): """ get any user """ auth_actions.get_current_user(token) user = user_actions.get_user(id) if not user: raise HTTPException(status_code=400, detail="User not found.") return user
def get_items(skip: int = 0, limit: int = 100, token: str = Depends(oauth2_scheme)): """ get many items """ auth_actions.get_current_user(token) items = item_actions.get_items(skip, limit) if not items: raise HTTPException(status_code=400, detail="Items not found.") return items
def delete_user(token: str = Depends(oauth2_scheme)): """ delete the current user """ current_user = auth_actions.get_current_user(token) user_actions.delete_user(current_user) return current_user
def delete_item(id: UUID4, token: str = Depends(oauth2_scheme)): """ delete an item for the current user """ current_user = auth_actions.get_current_user(token) item = item_actions.get_user_item(current_user.id, id) if not item: raise HTTPException(status_code=400, detail="Item not found.") return item_actions.delete_item(item)
def logout(token: str = Depends(oauth2_scheme)): ''' return an invalid token, assuming login and logout workflows are handled in a client ''' current_user = get_current_user(token) access_token_expires = timedelta(minutes=-1) data = {'id': str(current_user.id), 'email': current_user.email} expired_token = create_access_token(data=data, expires_delta=access_token_expires) return {"access_token": expired_token, "token_type": "bearer"}
def create_item(token: str = Depends(oauth2_scheme), item_create: ItemCreate = Body( ..., example={ 'name': 'Item name', 'description': 'Item description' })): """ create an item for the current user """ current_user = auth_actions.get_current_user(token) item = item_actions.create_item(current_user.id, item_create) return item_actions.get_item(item.id)
def update_item(id: UUID4, token: str = Depends(oauth2_scheme), item_update: ItemUpdate = Body( ..., example={ 'name': 'Updated item name', 'description': 'Updated item description' })): """ update an item for the current user """ current_user = auth_actions.get_current_user(token) item = item_actions.get_user_item(current_user.id, id) if not item: raise HTTPException(status_code=400, detail="Item not found.") return item_actions.update_item(current_user.id, id, item_update)
def update_user(token: str = Depends(oauth2_scheme), user_update: UserUpdate = Body( ..., example={ 'name': 'Robert', 'email': '*****@*****.**', 'bio': 'logy #puns' })): """ update the current user """ current_user = auth_actions.get_current_user(token) if current_user.email != user_update.email: if user_actions.get_user_by_email(user_update.email): raise HTTPException(status_code=422, detail="This email is taken. Try another.") updated_user = user_actions.update_user(current_user.id, user_update) return updated_user
def current_user(token: str = Depends(oauth2_scheme)): return get_current_user(token)