clearml-server/apiserver/service_repo/__init__.py

59 lines
1.8 KiB
Python
Raw Normal View History

from typing import Text, Sequence, Callable, Union, Type
2019-06-10 21:24:35 +00:00
2021-05-03 14:26:44 +00:00
from inspect import signature
2019-06-10 21:24:35 +00:00
from jsonmodels import models
from .apicall import APICall, APICallResult
from .endpoint import EndpointFunc, Endpoint
from .service_repo import ServiceRepo
2021-01-05 16:28:43 +00:00
__all__ = ["ServiceRepo", "APICall", "endpoint"]
2019-06-10 21:24:35 +00:00
LegacyEndpointFunc = Callable[[APICall], None]
def endpoint(
name: Text,
min_version: Text = "1.0",
required_fields: Sequence[Text] = None,
request_data_model: Type[models.Base] = None,
response_data_model: Type[models.Base] = None,
2019-06-10 21:24:35 +00:00
validate_schema=False,
):
""" Endpoint decorator, used to declare a method as an endpoint handler """
def decorator(f: Union[EndpointFunc, LegacyEndpointFunc]) -> EndpointFunc:
# Backwards compatibility: support endpoints with both old-style signature (call) and new-style signature
# (call, company, request_model)
func = f
sig = signature(f)
nonlocal request_data_model
if len(sig.parameters) == 3 and request_data_model is None:
last_arg = list(sig.parameters.items())[-1][1]
if last_arg.annotation != last_arg.empty:
request_data_model = last_arg.annotation
elif len(sig.parameters) == 1:
2019-06-10 21:24:35 +00:00
# old-style
def adapter(call, *_, **__):
return f(call)
func = adapter
ServiceRepo.register(
Endpoint(
name=name,
func=func,
min_version=min_version,
required_fields=required_fields,
request_data_model=request_data_model,
response_data_model=response_data_model,
validate_schema=validate_schema,
)
)
return func
return decorator