Local API (#982)

* Introduce API scelleton

* Raw impl for session

* Simple state endpoint

* Apply _body naming

* Finalize session testing and comment out tons of useless code

* Clean and refactor part1

* Clean and refactor part2

* Clean and refactor part2

* Clean and refactor part2

* Clean and refactor part2

* Refactor middleware

* Refactor middleware

* Clean and refactor part3

* TDD and 2 beers

* TDD and 2 beers

* Complete state endpoints

* You can only set what is already present

* Use only JSON as response

* Use default logger

* Improve auth extraction

* Extend api command with more args

* Adjust API messages
This commit is contained in:
Henry Ruhs
2026-05-11 16:31:45 +02:00
committed by henryruhs
parent f1149ebc84
commit 87678da498
20 changed files with 647 additions and 79 deletions
+39
View File
@@ -0,0 +1,39 @@
import secrets
from datetime import datetime, timedelta
from typing import Dict
from typing import Optional
from facefusion.types import Session, Token
SESSIONS : Dict[Token, Session] = {}
def create_session() -> Session:
session : Session =\
{
'access_token': secrets.token_urlsafe(128),
'refresh_token': secrets.token_urlsafe(128),
'created_at': datetime.now(),
'expires_at': datetime.now() + timedelta(minutes = 10)
}
return session
def get_session(access_token : Token) -> Optional[Session]:
return SESSIONS.get(access_token)
def set_session(access_token : Token, session : Session) -> None:
SESSIONS[access_token] = session
def validate_session(access_token : Token) -> bool:
session = get_session(access_token)
return session and datetime.now() <= session.get('expires_at')
def clear_session(access_token : Token) -> None:
if access_token in SESSIONS:
del SESSIONS[access_token]