Skip to content

API

startup

startup()

API startup handler.

Source code in src/trecover/app/api/trecoverapi.py
@api.on_event('startup')
def startup() -> None:
    """ API startup handler. """

    log.project_logger.info('FatAPI launched')

construct_response

construct_response(handler)

A decorator that wraps a request handler.

Parameters:

Name Type Description Default
handler Callable[..., Dict]

Request processing function.

required

Returns:

Name Type Description
wrap Callable[..., Dict]

Decorated handler.

Source code in src/trecover/app/api/trecoverapi.py
def construct_response(handler: Callable[..., Dict]) -> Callable[..., Dict]:
    """
    A decorator that wraps a request handler.

    Parameters
    ----------
    handler : Callable[..., Dict]
        Request processing function.

    Returns
    -------
    wrap : Callable[..., Dict]
        Decorated handler.

    """

    @wraps(handler)
    def wrap(request: Request, *args, **kwargs) -> Dict:
        """
        A wrapper that constructs a JSON response for an endpoint's results.

        Parameters
        ----------
        request : Request
            Client request information.

        Returns
        -------
        response : Dict
            The result of the handler function.

        """

        response = handler(request, *args, **kwargs)

        response['method'] = request.method
        response['timestamp'] = datetime.now().isoformat()
        response['url'] = request.url._url

        return response

    return wrap

index

index(request)

Healthcheck handler.

Parameters:

Name Type Description Default
request Request

Client request information.

required

Returns:

Name Type Description
Dict Dict

OK phrase as a Dict response.

Source code in src/trecover/app/api/trecoverapi.py
@api.get('/', tags=['General'])
@construct_response
def index(request: Request) -> Dict:
    """
    Healthcheck handler.

    Parameters
    ----------
    request : Request
        Client request information.

    Returns
    -------
    Dict:
        OK phrase as a Dict response.

    """

    return {
        'message': HTTPStatus.OK.phrase,
        'status_code': HTTPStatus.OK
    }

config

config(request)

Get the configuration of the model used for inference.

Parameters:

Name Type Description Default
request Request

Client request information.

required

Returns:

Name Type Description
response Dict

Response containing the values of the model configuration in the 'config' field.

Source code in src/trecover/app/api/trecoverapi.py
@api.get('/config', tags=['Configuration'])
@construct_response
def config(request: Request) -> Dict:
    """
    Get the configuration of the model used for inference.

    Parameters
    ----------
    request : Request
        Client request information.

    Returns
    -------
    response : Dict
        Response containing the values of the model configuration in the 'config' field.

    """

    task = get_model_config.delay()
    model_config = task.get()

    response = {
        'message': HTTPStatus.OK.phrase,
        'status_code': HTTPStatus.OK,
        'config': model_config
    }

    return response

config_param

config_param(request, param)

Get the specific configuration parameter of the model used for inference.

Parameters:

Name Type Description Default
request Request

Client request information.

required
param str

Parameter name.

required

Returns:

Name Type Description
response Dict

Response containing the value of the specific configuration parameter in the '' field if it exists, otherwise 'Not found' value.

Source code in src/trecover/app/api/trecoverapi.py
@api.get('/config/{param}', tags=['Configuration'])
@construct_response
def config_param(request: Request, param: str) -> Dict:
    """
    Get the specific configuration parameter of the model used for inference.

    Parameters
    ----------
    request : Request
        Client request information.
    param : str
        Parameter name.

    Returns
    -------
    response : Dict
        Response containing the value of the specific configuration parameter in
        the '<param>' field if it exists, otherwise 'Not found' value.

    """

    task = get_model_config.delay()
    model_config = task.get()

    response = {
        'message': HTTPStatus.OK.phrase,
        'status_code': HTTPStatus.OK,
        param: model_config.get(param, 'Not found')
    }

    return response

recover

recover(request, payload)

Perform keyless reading.

Parameters:

Name Type Description Default
request Request

Client request information.

required
payload PredictPayload

Data for keyless reading.

required

Returns:

Name Type Description
response TaskResponse

Response containing the id of the celery task in the 'task_id' field.

Source code in src/trecover/app/api/trecoverapi.py
@api.post('/recover', tags=['Prediction'], response_model=TaskResponse)
@construct_response
def recover(request: Request, payload: PredictPayload) -> Dict:
    """
    Perform keyless reading.

    Parameters
    ----------
    request : Request
        Client request information.
    payload : PredictPayload
        Data for keyless reading.

    Returns
    -------
    response : TaskResponse
        Response containing the id of the celery task in the 'task_id' field.

    """

    task = predict.delay(payload.columns, payload.beam_width, payload.delimiter)

    response = {
        'message': HTTPStatus.ACCEPTED.phrase,
        'status_code': HTTPStatus.ACCEPTED,
        'task_id': task.id
    }

    return response

status

status(
    request,
    task_id=Path(
        Ellipsis,
        title="The ID of the task to get status",
        regex="[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}",
    ),
)

Get a celery task status.

Parameters:

Name Type Description Default
request Request

Client request information.

required
task_id str

Celery task id.

Path(Ellipsis, title='The ID of the task to get status', regex='[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')

Returns:

Name Type Description
response PredictResponse

Response containing the status of the celery task in the 'state' and 'progress' fields if it's still in process, error information in 'message' and 'status_code' fields if it's failed, otherwise the result of keyless reading in the 'chains' field.

Source code in src/trecover/app/api/trecoverapi.py
@api.get('/status/{task_id}', tags=['Prediction'], response_model=PredictResponse)
@construct_response
def status(request: Request,
           task_id: str = Path(...,
                               title='The ID of the task to get status',
                               regex=r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')
           ) -> Dict:
    """
    Get a celery task status.

    Parameters
    ----------
    request : Request
        Client request information.
    task_id : str
        Celery task id.

    Returns
    -------
    response : PredictResponse
        Response containing the status of the celery task in the 'state' and 'progress'
        fields if it's still in process,
        error information in 'message' and 'status_code' fields if it's failed,
        otherwise the result of keyless reading in the 'chains' field.

    """

    task = AsyncResult(task_id, app=celery_app)

    if task.failed():
        response = {
            'message': str(task.info),
            'status_code': HTTPStatus.CONFLICT
        }

    elif task.ready():
        columns, chains = task.get()

        response = {
            'message': HTTPStatus.OK.phrase,
            'status_code': HTTPStatus.OK,
            'columns': columns,
            'chains': chains,
            'progress': len(columns)
        }
    else:
        info = task.info

        response = {
            'message': HTTPStatus.PROCESSING.phrase,
            'status_code': HTTPStatus.PROCESSING,
            'state': task.status,
            'progress': info.get('progress') if isinstance(info, dict) else None
        }

    return response

delete_prediction

delete_prediction(
    request,
    task_id=Path(
        Ellipsis,
        title="The ID of the task to forget",
        regex="[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}",
    ),
)

Delete task result in the celery backend database.

Parameters:

Name Type Description Default
request Request

Client request information.

required
task_id str

Task ID to delete its result from celery backend database.

Path(Ellipsis, title='The ID of the task to forget', regex='[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')

Returns:

Name Type Description
response Dict

OK phrase.

Source code in src/trecover/app/api/trecoverapi.py
@api.delete('/{task_id}', tags=['Prediction'])
@construct_response
def delete_prediction(request: Request,
                      task_id: str = Path(...,
                                          title='The ID of the task to forget',
                                          regex=r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')
                      ) -> Dict:
    """
    Delete task result in the celery backend database.

    Parameters
    ----------
    request : Request
        Client request information.
    task_id : str
        Task ID to delete its result from celery backend database.

    Returns
    -------
    response : Dict
        OK phrase.

    """

    task = AsyncResult(task_id, app=celery_app)

    if task.ready():
        task.forget()
    else:
        task.revoke(terminate=True)

    response = {
        'message': HTTPStatus.OK.phrase,
        'status_code': HTTPStatus.OK
    }

    return response