本文整理匯總了Python中fastapi.Body方法的典型用法代碼示例。如果您正苦於以下問題:Python fastapi.Body方法的具體用法?Python fastapi.Body怎麽用?Python fastapi.Body使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fastapi
的用法示例。
在下文中一共展示了fastapi.Body方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: update_user_me
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
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
示例2: reset_password
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def reset_password(token: str = Body(...), new_password: str = Body(...)):
"""
Reset password.
"""
username = verify_password_reset_token(token)
if not username:
raise HTTPException(status_code=400, detail="Invalid token")
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.",
)
elif not crud.user.is_active(user):
raise HTTPException(status_code=400, detail="Inactive user")
user_in = UserUpdate(name=username, password=new_password)
user = crud.user.update(bucket, username=username, user_in=user_in)
return {"msg": "Password updated successfully"}
示例3: read_items
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def read_items(
item_id: UUID,
start_datetime: Optional[datetime] = Body(None),
end_datetime: Optional[datetime] = Body(None),
repeat_at: Optional[time] = Body(None),
process_after: Optional[timedelta] = Body(None),
):
start_process = start_datetime + process_after
duration = end_datetime - start_process
return {
"item_id": item_id,
"start_datetime": start_datetime,
"end_datetime": end_datetime,
"repeat_at": repeat_at,
"process_after": process_after,
"start_process": start_process,
"duration": duration,
}
示例4: get_autocomplete
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def get_autocomplete(
query: QueryParams = Depends(QueryParams), extra: ExtraParams = Body(ExtraParams())
):
async def get_intentions():
if not query.nlu or query.lang not in nlu_allowed_languages:
return None
focus = None
if query.lon and query.lat:
focus = Point(query.lon, query.lat)
return await nlu_client.get_intentions(text=query.q, lang=query.lang, focus=focus)
autocomplete_response, intentions = await asyncio.gather(
bragi_client.autocomplete(query, extra), get_intentions()
)
if intentions is not None:
autocomplete_response["intentions"] = intentions
return autocomplete_response
示例5: create_new_article
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def create_new_article(
article_create: ArticleInCreate = Body(..., embed=True, alias="article"),
user: User = Depends(get_current_user_authorizer()),
articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)),
) -> ArticleInResponse:
slug = get_slug_for_article(article_create.title)
if await check_article_exists(articles_repo, slug):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=strings.ARTICLE_ALREADY_EXISTS,
)
article = await articles_repo.create_article(
slug=slug,
title=article_create.title,
description=article_create.description,
body=article_create.body,
author=user,
tags=article_create.tags,
)
return ArticleInResponse(article=ArticleForResponse.from_orm(article))
示例6: tokenize
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def tokenize(self, text_input: str = Body(None, embed=True), return_ids: bool = Body(False, embed=True)):
"""
Tokenize the provided input and eventually returns corresponding tokens id:
- **text_input**: String to tokenize
- **return_ids**: Boolean flags indicating if the tokens have to be converted to their integer mapping.
"""
try:
tokens_txt = self._pipeline.tokenizer.tokenize(text_input)
if return_ids:
tokens_ids = self._pipeline.tokenizer.convert_tokens_to_ids(tokens_txt)
return ServeTokenizeResult(tokens=tokens_txt, tokens_ids=tokens_ids)
else:
return ServeTokenizeResult(tokens=tokens_txt)
except Exception as e:
raise HTTPException(status_code=500, detail={"model": "", "error": str(e)})
示例7: detokenize
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def detokenize(
self,
tokens_ids: List[int] = Body(None, embed=True),
skip_special_tokens: bool = Body(False, embed=True),
cleanup_tokenization_spaces: bool = Body(True, embed=True),
):
"""
Detokenize the provided tokens ids to readable text:
- **tokens_ids**: List of tokens ids
- **skip_special_tokens**: Flag indicating to not try to decode special tokens
- **cleanup_tokenization_spaces**: Flag indicating to remove all leading/trailing spaces and intermediate ones.
"""
try:
decoded_str = self._pipeline.tokenizer.decode(tokens_ids, skip_special_tokens, cleanup_tokenization_spaces)
return ServeDeTokenizeResult(model="", text=decoded_str)
except Exception as e:
raise HTTPException(status_code=500, detail={"model": "", "error": str(e)})
示例8: forward
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def forward(self, inputs=Body(None, embed=True)):
"""
**inputs**:
**attention_mask**:
**tokens_type_ids**:
"""
# Check we don't have empty string
if len(inputs) == 0:
return ServeForwardResult(output=[], attention=[])
try:
# Forward through the model
output = self._pipeline(inputs)
return ServeForwardResult(output=output)
except Exception as e:
raise HTTPException(500, {"error": str(e)})
示例9: update_user_me
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def update_user_me(
*,
db: Session = Depends(deps.get_db),
password: str = Body(None),
full_name: str = Body(None),
email: EmailStr = Body(None),
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update own user.
"""
current_user_data = jsonable_encoder(current_user)
user_in = schemas.UserUpdate(**current_user_data)
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
user = crud.user.update(db, db_obj=current_user, obj_in=user_in)
return user
示例10: create_user_open
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def create_user_open(
*,
db: Session = Depends(deps.get_db),
password: str = Body(...),
email: EmailStr = Body(...),
full_name: str = Body(None),
) -> Any:
"""
Create new user without the need to be logged in.
"""
if not settings.USERS_OPEN_REGISTRATION:
raise HTTPException(
status_code=403,
detail="Open user registration is forbidden on this server",
)
user = crud.user.get_by_email(db, email=email)
if user:
raise HTTPException(
status_code=400,
detail="The user with this username already exists in the system",
)
user_in = schemas.UserCreate(password=password, email=email, full_name=full_name)
user = crud.user.create(db, obj_in=user_in)
return user
示例11: reset_password
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def reset_password(
token: str = Body(...),
new_password: str = Body(...),
db: Session = Depends(deps.get_db),
) -> Any:
"""
Reset password
"""
email = verify_password_reset_token(token)
if not email:
raise HTTPException(status_code=400, detail="Invalid token")
user = crud.user.get_by_email(db, email=email)
if not user:
raise HTTPException(
status_code=404,
detail="The user with this username does not exist in the system.",
)
elif not crud.user.is_active(user):
raise HTTPException(status_code=400, detail="Inactive user")
hashed_password = get_password_hash(new_password)
user.hashed_password = hashed_password
db.add(user)
db.commit()
return {"msg": "Password updated successfully"}
示例12: config_edit
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def config_edit(data: RequestConfigEdit = Body(...)) -> StatusSchema:
"""
Change a given configuration key to a specified value.
This endpoint is not stable and may be subject to change in the future.
"""
db.set_plugin_configuration_key(data.plugin, data.key, data.value)
return StatusSchema(status="ok")
示例13: create_user_open
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def create_user_open(
*,
username: str = Body(...),
password: str = Body(...),
email: EmailStr = Body(None),
full_name: str = Body(None),
):
"""
Create new user without the need to be logged in.
"""
if not config.USERS_OPEN_REGISTRATION:
raise HTTPException(
status_code=403,
detail="Open user resistration is forbidden on this server",
)
bucket = get_default_bucket()
user = crud.user.get(bucket, username=username)
if user:
raise HTTPException(
status_code=400,
detail="The user with this username already exists in the system",
)
user_in = UserCreate(
username=username, password=password, email=email, full_name=full_name
)
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
示例14: compute
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def compute(a: int = Body(...), b: str = Body(...)):
return {"a": a, "b": b}
示例15: create_product
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Body [as 別名]
def create_product(data: Product = Body(..., media_type=media_type, embed=True)):
pass # pragma: no cover