make state endpoint more solid

This commit is contained in:
henryruhs committed 2026-09-23 22:28:42 +02:00
1 parent 37becda877
commit 4bc621fa62
3 files changed
+42 -1

No files matched your search

+10 -1
View File
@@ -4,6 +4,7 @@ from starlette.status import HTTP_200_OK, HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOU
from facefusion import args_helper, capability_store, state_manager, translator
from facefusion.apis import asset_store
from facefusion.apis.state_helper import cast_argument_value, validate_argument_value
async def get_state(request : Request) -> JSONResponse:
@@ -33,7 +34,15 @@ async def set_state(request : Request) -> Response:
'message': translator.get('invalid_state_key', 'facefusion.apis')
}, status_code = HTTP_400_BAD_REQUEST)
__api_args__[key] = value
__value__ = cast_argument_value(key, value)
if not validate_argument_value(key, __value__):
return JSONResponse(
{
'message': translator.get('invalid_state_value', 'facefusion.apis')
}, status_code = HTTP_400_BAD_REQUEST)
__api_args__[key] = __value__
if __api_args__:
+1
View File
@@ -11,6 +11,7 @@ LOCALES : Locales =\
'invalid_refresh_token': 'invalid refresh token',
'session_limit_reached': 'session limit reached',
'invalid_state_key': 'invalid state key',
'invalid_state_value': 'invalid state value',
'source_asset_not_found': 'source asset not found',
'target_asset_not_found': 'target asset not found',
'invalid_job_status': 'invalid job status',
+31
View File
@@ -0,0 +1,31 @@
from facefusion.common_helper import cast_bool, cast_float, cast_int
from facefusion.types import State, StateKey, StateValue
def validate_argument_value(key : StateKey, value : StateValue) -> bool:
if State.__annotations__.get(key) is int:
return isinstance(value, int)
if State.__annotations__.get(key) is float:
return isinstance(value, float)
if State.__annotations__.get(key) is bool:
return isinstance(value, bool)
return True
def cast_argument_value(key : StateKey, value : StateValue) -> StateValue:
if State.__annotations__.get(key) is int:
return cast_int(value)
if State.__annotations__.get(key) is float:
return cast_float(value)
if State.__annotations__.get(key) is bool:
if isinstance(value, bool):
return value
return cast_bool(value)
return value