-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pier-digital/f/add-pydantic-support
Support to pydantic validators
- Loading branch information
Showing
10 changed files
with
407 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,57 @@ | ||
import typing | ||
|
||
import fastapi | ||
from slugify import slugify | ||
|
||
|
||
from modelib.core import schemas | ||
from modelib.runners.base import BaseRunner | ||
from modelib.runners.base import EndpointMetadataManager, BaseRunner | ||
|
||
|
||
def create_runner_endpoint( | ||
app: fastapi.FastAPI, | ||
runner: BaseRunner, | ||
runner_func: typing.Callable, | ||
endpoint_metadata_manager: EndpointMetadataManager, | ||
**kwargs, | ||
) -> fastapi.FastAPI: | ||
path = f"/{slugify(runner.name)}" | ||
path = f"/{endpoint_metadata_manager.slug}" | ||
|
||
route_kwargs = { | ||
"name": runner.name, | ||
"name": endpoint_metadata_manager.name, | ||
"methods": ["POST"], | ||
"response_model": runner.response_model, | ||
"response_model": endpoint_metadata_manager.response_model, | ||
} | ||
route_kwargs.update(kwargs) | ||
|
||
app.add_api_route( | ||
path, | ||
runner.get_runner_func(), | ||
runner_func, | ||
**route_kwargs, | ||
) | ||
|
||
return app | ||
|
||
|
||
def create_runners_router(runners: typing.List[BaseRunner]) -> fastapi.APIRouter: | ||
router = fastapi.APIRouter( | ||
tags=["runners"], | ||
responses={ | ||
500: { | ||
"model": schemas.JsonApiErrorModel, | ||
"description": "Inference Internal Server Error", | ||
} | ||
}, | ||
) | ||
def create_runners_router( | ||
runners: typing.List[BaseRunner], **runners_router_kwargs | ||
) -> fastapi.APIRouter: | ||
responses = runners_router_kwargs.pop("responses", {}) | ||
if 500 not in responses: | ||
responses[500] = { | ||
"model": schemas.JsonApiErrorModel, | ||
"description": "Inference Internal Server Error", | ||
} | ||
|
||
runners_router_kwargs["responses"] = responses | ||
|
||
runners_router_kwargs["tags"] = runners_router_kwargs.get("tags", ["runners"]) | ||
|
||
router = fastapi.APIRouter(**runners_router_kwargs) | ||
|
||
for runner in runners: | ||
router = create_runner_endpoint(router, runner) | ||
router = create_runner_endpoint( | ||
router, | ||
runner_func=runner.get_runner_func(), | ||
endpoint_metadata_manager=runner.endpoint_metadata_manager, | ||
) | ||
|
||
return router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.