|
| 1 | +"""Function invocation endpoint. |
| 2 | +
|
| 3 | +``POST /functions/{name}`` is the single entry point that converts an |
| 4 | +HTTP request body into a Dispatcher call. The body is a JSON object whose |
| 5 | +keys match the catalog's parameter names; opaque MEOS types (any |
| 6 | +parameter the catalog labels with ``decode_per_param``) are decoded by |
| 7 | +the :class:`WireCodec` before the dispatcher invokes the MEOS function, |
| 8 | +and the return value is encoded back to a wire-friendly representation |
| 9 | +on the way out. |
| 10 | +
|
| 11 | +The contract: |
| 12 | +
|
| 13 | +- Request body: ``{ "params": { "<arg>": <wire_value>, ... } }`` |
| 14 | +- Response body for serialised returns: |
| 15 | + ``{ "result": <wire_value>, "encoding": "mfjson"|"wkb"|... }`` |
| 16 | +- Response body for scalar returns: |
| 17 | + ``{ "result": <value>, "encoding": null }`` |
| 18 | +
|
| 19 | +This is the minimal vocabulary that lets a thin HTTP client (Polars |
| 20 | +notebook, Spark UDF, JavaScript map UI) treat every MEOS function the |
| 21 | +same way — call by name with a JSON body, read a typed result. |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +from fastapi import APIRouter, HTTPException, Request |
| 29 | +from pydantic import BaseModel, Field |
| 30 | + |
| 31 | +router = APIRouter() |
| 32 | + |
| 33 | + |
| 34 | +class InvokeRequest(BaseModel): |
| 35 | + """Body of a POST /functions/{name} request.""" |
| 36 | + |
| 37 | + params: dict[str, Any] = Field( |
| 38 | + default_factory=dict, |
| 39 | + description=( |
| 40 | + "Map of parameter name to wire value. Opaque MEOS types " |
| 41 | + "(Temporal*, STBox, Set, …) MUST be supplied as the encoded " |
| 42 | + "wire form named in the catalog's `decode_per_param`." |
| 43 | + ), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class InvokeResponse(BaseModel): |
| 48 | + """Body of a POST /functions/{name} response.""" |
| 49 | + |
| 50 | + result: Any = Field(..., description="The function return value.") |
| 51 | + encoding: str | None = Field( |
| 52 | + None, |
| 53 | + description=( |
| 54 | + "Encoding the result is delivered in. `null` for scalar " |
| 55 | + "(int / float / str / bool) returns; one of `mfjson` / " |
| 56 | + "`text` / `wkb` / `hexwkb` for serialised MEOS objects." |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +@router.post( |
| 62 | + "/{name}", |
| 63 | + response_model=InvokeResponse, |
| 64 | + summary="Invoke a dispatcher-exposable MEOS function", |
| 65 | +) |
| 66 | +def invoke(name: str, body: InvokeRequest, request: Request) -> InvokeResponse: |
| 67 | + dispatcher = request.app.state.dispatcher |
| 68 | + codec = request.app.state.codec |
| 69 | + |
| 70 | + if not dispatcher.has(name): |
| 71 | + raise HTTPException( |
| 72 | + status_code=404, |
| 73 | + detail=( |
| 74 | + f"Function `{name}` is not in the dispatcher catalog. " |
| 75 | + f"GET /catalog lists available functions." |
| 76 | + ), |
| 77 | + ) |
| 78 | + |
| 79 | + sig = dispatcher.signature(name) |
| 80 | + |
| 81 | + # 1. Decode opaque MEOS-type parameters via the codec, leaving |
| 82 | + # scalar parameters untouched. |
| 83 | + decoded: dict[str, Any] = {} |
| 84 | + for key, value in body.params.items(): |
| 85 | + encoding = sig.decode_per_param.get(key) |
| 86 | + if encoding: |
| 87 | + try: |
| 88 | + decoded[key] = codec.decode(encoding, value) |
| 89 | + except KeyError as e: |
| 90 | + raise HTTPException( |
| 91 | + status_code=400, |
| 92 | + detail=( |
| 93 | + f"Parameter `{key}` claims encoding `{encoding}` " |
| 94 | + f"but the WireCodec has no decoder for it." |
| 95 | + ), |
| 96 | + ) from e |
| 97 | + else: |
| 98 | + decoded[key] = value |
| 99 | + |
| 100 | + # 2. Dispatch — surfaces KeyError (unknown name, caught above) and |
| 101 | + # TypeError (missing / extra params) as 400 errors. |
| 102 | + try: |
| 103 | + result = dispatcher.dispatch(name, decoded) |
| 104 | + except TypeError as e: |
| 105 | + raise HTTPException(status_code=400, detail=str(e)) from e |
| 106 | + |
| 107 | + # 3. Encode the result if the catalog labels it with an encoding. |
| 108 | + if sig.encode_return: |
| 109 | + try: |
| 110 | + wire_result = codec.encode(sig.encode_return, result) |
| 111 | + except KeyError as e: |
| 112 | + raise HTTPException( |
| 113 | + status_code=500, |
| 114 | + detail=( |
| 115 | + f"Result claims encoding `{sig.encode_return}` " |
| 116 | + f"but the WireCodec has no encoder for it." |
| 117 | + ), |
| 118 | + ) from e |
| 119 | + return InvokeResponse(result=wire_result, encoding=sig.encode_return) |
| 120 | + |
| 121 | + return InvokeResponse(result=result, encoding=None) |
0 commit comments