コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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
コード例 #5
0
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
コード例 #6
0
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)
コード例 #7
0
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"}
コード例 #8
0
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)
コード例 #9
0
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)
コード例 #10
0
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
コード例 #11
0
def current_user(token: str = Depends(oauth2_scheme)):
    return get_current_user(token)