How to use the fastapi.Depends function in fastapi

To help you get started, we’ve selected a few fastapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 3lpsy / boucanpy / boucanpy / api / routers / http_server / router.py View on Github external
async def store(
    form: HttpServerCreateForm,
    http_server_repo: HttpServerRepo = Depends(HttpServerRepo()),
    token: TokenPayload = Depends(ScopedTo("http-server:create")),
):
    if http_server_repo.exists(name=form.name.lower()):
        abort_for_input("name", "Server name already taken")

    data = only(dict(form), ["name"])

    data["name"] = data["name"].lower()

    item = http_server_repo.create(data).data()
    return HttpServerResponse(http_server=item)
github mlrun / mlrun / mlrun / api / api / endpoints / logs.py View on Github external
def get_log(
        project: str,
        uid: str,
        size: int = -1,
        offset: int = 0,
        tag: str = "",
        db_session: Session = Depends(deps.get_db_session)):
    out, status = crud.Logs.get_log(db_session, project, uid, size, offset)
    return Response(content=out, media_type="text/plain", headers={"pod_status": status})
github 3lpsy / boucanpy / boucanpy / api / routers / zone / dns_record / router.py View on Github external
async def index(
    zone_id: int,
    sort_qs: SortQS = Depends(SortQS),
    pagination: PaginationQS = Depends(PaginationQS),
    dns_record_repo: DnsRecordRepo = Depends(DnsRecordRepo()),
    zone_repo: ZoneRepo = Depends(ZoneRepo()),
    token: TokenPayload = Depends(ScopedTo("dns-record:list")),
    includes: List[str] = Query(None),
):
    zone_repo.exists(id=zone_id, or_fail=True)

    includes = only(includes, ["zone"], values=True)

    pg, items = (
        dns_record_repo.loads("zone")
        .sort(sort_qs)
        .filter_by(zone_id=zone_id)
        .paginate(pagination)
        .includes(includes)
github mlrun / mlrun / mlrun / api / api / endpoints / runs.py View on Github external
def del_runs(
        project: str = None,
        name: str = None,
        labels: List[str] = Query([], alias='label'),
        state: str = None,
        days_ago: int = 0,
        db_session: Session = Depends(deps.get_db_session)):
    get_db().del_runs(db_session, name, project, labels, state, days_ago)
    return {}
github nsidnev / fastapi-realworld-example-app / app / api / dependencies / comments.py View on Github external
def check_comment_modification_permissions(
    comment: Comment = Depends(get_comment_by_id_from_path),
    user: User = Depends(authentication.get_current_user_authorizer()),
) -> None:
    if not check_user_can_modify_comment(comment, user):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=strings.USER_IS_NOT_AUTHOR_OF_ARTICLE,
        )
github tiangolo / full-stack-fastapi-couchbase / {{cookiecutter.project_slug}} / backend / app / app / api / api_v1 / endpoints / utils.py View on Github external
def test_email(
    email_to: EmailStr, current_user: UserInDB = Depends(get_current_active_superuser)
):
    """
    Test emails.
    """
    send_test_email(email_to=email_to)
    return {"msg": "Test email sent"}
github tiangolo / fastapi / docs / src / sql_databases / tutorial001.py View on Github external
def read_user(user_id: int, db: Session = Depends(get_db)):
    user = get_user(db, user_id=user_id)
    return user
github msys2 / msys2-web / app / web.py View on Github external
@router.get('/group/', dependencies=[Depends(Etag(get_etag))])
@router.get('/group/{group_name}', dependencies=[Depends(Etag(get_etag))])
async def group(request: Request, response: Response, group_name: Optional[str] = None) -> Response:
    global state

    if group_name is not None:
        res = []
        for s in state.sources.values():
            for k, p in sorted(s.packages.items()):
                if group_name in p.groups:
                    res.append(p)

        return templates.TemplateResponse("group.html", {
            "request": request,
            "name": group_name,
            "packages": res,
        }, headers=dict(response.headers))
github nsidnev / fastapi-realworld-example-app / app / api / routes / profiles.py View on Github external
async def follow_for_user(
    profile: Profile = Depends(get_profile_by_username_from_path),
    user: User = Depends(get_current_user_authorizer()),
    profiles_repo: ProfilesRepository = Depends(get_repository(ProfilesRepository)),
) -> ProfileInResponse:
    if user.username == profile.username:
        raise HTTPException(
            status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_FOLLOW_YOURSELF
        )

    if profile.following:
        raise HTTPException(
            status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_ALREADY_FOLLOWED
        )

    await profiles_repo.add_user_into_followers(
        target_user=profile, requested_user=user
    )
github tiangolo / fastapi / docs / src / dependencies / tutorial003.py View on Github external
async def read_items(commons=Depends(CommonQueryParams)):
    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