"""FLorist server FastAPI endpoints and routes."""fromcontextlibimportasynccontextmanagerfromtypingimportAny,AsyncGeneratorfromfastapiimportDepends,FastAPIfromfastapi.responsesimportJSONResponsefrommotor.motor_asyncioimportAsyncIOMotorClientfromflorist.api.auth.tokenimportDEFAULT_USERNAME,Token,make_default_server_userfromflorist.api.clients.clientsimportClientfromflorist.api.clients.optimizersimportOptimizerfromflorist.api.db.configimportDatabaseConfigfromflorist.api.db.server_entitiesimportUserfromflorist.api.models.modelsimportModelfromflorist.api.routes.server.authimportcheck_default_user_tokenfromflorist.api.routes.server.authimportrouterasauth_routerfromflorist.api.routes.server.jobimportrouterasjob_routerfromflorist.api.routes.server.statusimportrouterasstatus_routerfromflorist.api.routes.server.trainingimportrouterastraining_routerfromflorist.api.servers.strategiesimportStrategy
[docs]@asynccontextmanagerasyncdeflifespan(app:FastAPI)->AsyncGenerator[Any,Any]:"""Set up function for app startup and shutdown."""# Set up mongodbapp.db_client=AsyncIOMotorClient(DatabaseConfig.mongodb_uri)# type: ignore[attr-defined]app.database=app.db_client[DatabaseConfig.mongodb_db_name]# type: ignore[attr-defined]# Create default user if it does not existuser=awaitUser.find_by_username(DEFAULT_USERNAME,app.database)# type: ignore[attr-defined]ifuserisNone:awaitmake_default_server_user(app.database)# type: ignore[attr-defined]# Making a dictionary to hold the authentication tokens of the clients# this server is connected toapp.clients_auth_tokens:dict[str,Token]={}# type: ignore[attr-defined, misc]yield# Shut down mongodbapp.db_client.close()# type: ignore[attr-defined]
[docs]@app.get(path="/api/server/models",response_description="Returns a list of all available models",dependencies=[Depends(check_default_user_token)],)deflist_models()->JSONResponse:""" Return a list of all available models. :return: (JSONResponse) A JSON response with a list of all elements in the `api.servers.common.Model` enum. """returnJSONResponse(Model.list())
[docs]@app.get(path="/api/server/clients/{strategy}",response_description="Returns a list of all available clients by strategy",dependencies=[Depends(check_default_user_token)],)deflist_clients(strategy:Strategy)->JSONResponse:""" Return a list of all available clients by strategy. :param strategy: (Strategy) The strategy to find the compatible clients. :return: (JSONResponse) A JSON response with a list of all elements in the `api.clients.common.Client` enum that are compatible with the given strategy. """returnJSONResponse(Client.list_by_strategy(strategy))
[docs]@app.get(path="/api/server/strategies",response_description="Returns a list of all available strategies",dependencies=[Depends(check_default_user_token)],)deflist_strategies()->JSONResponse:""" Return a list of all available strategies. :return: (JSONResponse) A JSON response with a list of all elements in the `api.servers.strategy.Strategies` enum. """returnJSONResponse(Strategy.list())
[docs]@app.get(path="/api/server/optimizers",response_description="Returns a list of all available optimizers",dependencies=[Depends(check_default_user_token)],)deflist_optimizers()->JSONResponse:""" Return a list of all available optimizers. :return: (JSONResponse) A JSON response with a list of all elements in the `api.clients.optimizers.Optimizer` enum. """returnJSONResponse(Optimizer.list())