Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor: examples of use match case (#31312)

Asuka Minato3216b67bfa93
diff --git a/api/controllers/console/app/annotation.py b/api/controllers/console/app/annotation.py
index 6a4c1528b03..a07145ce9fb 100644
--- a/api/controllers/console/app/annotation.py
+++ b/api/controllers/console/app/annotation.py
@@ -107,10 +107,11 @@ class AnnotationReplyActionApi(Resource):
def post(self, app_id, action: Literal["enable", "disable"]):
app_id = str(app_id)
args = AnnotationReplyPayload.model_validate(console_ns.payload)
- if action == "enable":
- result = AppAnnotationService.enable_app_annotation(args.model_dump(), app_id)
- elif action == "disable":
- result = AppAnnotationService.disable_app_annotation(app_id)
+ match action:
+ case "enable":
+ result = AppAnnotationService.enable_app_annotation(args.model_dump(), app_id)
+ case "disable":
+ result = AppAnnotationService.disable_app_annotation(app_id)
return result, 200
diff --git a/api/controllers/console/auth/oauth_server.py b/api/controllers/console/auth/oauth_server.py
index 6162d88a0b1..38ea5d2daef 100644
--- a/api/controllers/console/auth/oauth_server.py
+++ b/api/controllers/console/auth/oauth_server.py
@@ -155,43 +155,43 @@ class OAuthServerUserTokenApi(Resource):
grant_type = OAuthGrantType(payload.grant_type)
except ValueError:
raise BadRequest("invalid grant_type")
-
- if grant_type == OAuthGrantType.AUTHORIZATION_CODE:
- if not payload.code:
- raise BadRequest("code is required")
-
- if payload.client_secret != oauth_provider_app.client_secret:
- raise BadRequest("client_secret is invalid")
-
- if payload.redirect_uri not in oauth_provider_app.redirect_uris:
- raise BadRequest("redirect_uri is invalid")
-
- access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
- grant_type, code=payload.code, client_id=oauth_provider_app.client_id
- )
- return jsonable_encoder(
- {
- "access_token": access_token,
- "token_type": "Bearer",
- "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- "refresh_token": refresh_token,
- }
- )
- elif grant_type == OAuthGrantType.REFRESH_TOKEN:
- if not payload.refresh_token:
- raise BadRequest("refresh_token is required")
-
- access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
- grant_type, refresh_token=payload.refresh_token, client_id=oauth_provider_app.client_id
- )
- return jsonable_encoder(
- {
- "access_token": access_token,
- "token_type": "Bearer",
- "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- "refresh_token": refresh_token,
- }
- )
+ match grant_type:
+ case OAuthGrantType.AUTHORIZATION_CODE:
+ if not payload.code:
+ raise BadRequest("code is required")
+
+ if payload.client_secret != oauth_provider_app.client_secret:
+ raise BadRequest("client_secret is invalid")
+
+ if payload.redirect_uri not in oauth_provider_app.redirect_uris:
+ raise BadRequest("redirect_uri is invalid")
+
+ access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
+ grant_type, code=payload.code, client_id=oauth_provider_app.client_id
+ )
+ return jsonable_encoder(
+ {
+ "access_token": access_token,
+ "token_type": "Bearer",
+ "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ "refresh_token": refresh_token,
+ }
+ )
+ case OAuthGrantType.REFRESH_TOKEN:
+ if not payload.refresh_token:
+ raise BadRequest("refresh_token is required")
+
+ access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
+ grant_type, refresh_token=payload.refresh_token, client_id=oauth_provider_app.client_id
+ )
+ return jsonable_encoder(
+ {
+ "access_token": access_token,
+ "token_type": "Bearer",
+ "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ "refresh_token": refresh_token,
+ }
+ )
@console_ns.route("/oauth/provider/account")