|
| 1 | +"""Serverless function invocation facade.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +from collections.abc import Mapping |
| 7 | +from typing import TYPE_CHECKING, Protocol, cast |
| 8 | + |
| 9 | +from ._transport import TransportResponse, invoke, response_payload |
| 10 | +from .models import FunctionResponse, JSONValue |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from .client import VolcanoClient |
| 14 | + |
| 15 | +_FUNCTION_NAME = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$") |
| 16 | +_INVALID_FUNCTION_NAME = ( |
| 17 | + "Function name must be DNS-safe: lowercase letters, numbers, and hyphens; " |
| 18 | + "1-63 characters" |
| 19 | +) |
| 20 | +_INVALID_FUNCTION_RESPONSE = "Expected a complete function response" |
| 21 | +_INVALID_FUNCTION_PAYLOAD = "Function payload must be a mapping" |
| 22 | +_HTTP_SUCCESS_MIN = 200 |
| 23 | +_HTTP_SUCCESS_MAX = 300 |
| 24 | + |
| 25 | + |
| 26 | +class FunctionsTransport(Protocol): |
| 27 | + """Transport operations required by the functions facade.""" |
| 28 | + |
| 29 | + def resolve_function_for_invocation( |
| 30 | + self, |
| 31 | + *, |
| 32 | + authorization: str, |
| 33 | + name: str, |
| 34 | + ) -> TransportResponse: |
| 35 | + """Resolve a function name to its canonical identifier.""" |
| 36 | + ... |
| 37 | + |
| 38 | + def invoke_function( |
| 39 | + self, |
| 40 | + *, |
| 41 | + authorization: str, |
| 42 | + function_id: str, |
| 43 | + payload: Mapping[str, JSONValue], |
| 44 | + ) -> TransportResponse: |
| 45 | + """Invoke a resolved function identifier.""" |
| 46 | + ... |
| 47 | + |
| 48 | + |
| 49 | +class Functions: |
| 50 | + """Invoke deployed Volcano functions by name.""" |
| 51 | + |
| 52 | + def __init__(self, client: VolcanoClient) -> None: |
| 53 | + """Bind function calls to a Volcano client.""" |
| 54 | + self._client = client |
| 55 | + |
| 56 | + def invoke( |
| 57 | + self, |
| 58 | + name: str, |
| 59 | + payload: Mapping[str, JSONValue] | None = None, |
| 60 | + ) -> FunctionResponse: |
| 61 | + """Resolve and invoke a function with an optional JSON object payload.""" |
| 62 | + name = _function_name(name) |
| 63 | + request_payload = _function_payload(payload) |
| 64 | + authorization = self._client._function_token() |
| 65 | + transport = cast("FunctionsTransport", self._client._transport) |
| 66 | + resolved = invoke( |
| 67 | + transport.resolve_function_for_invocation, |
| 68 | + authorization=authorization, |
| 69 | + name=name, |
| 70 | + ) |
| 71 | + function_id = self._function_id(response_payload(resolved, _HTTP_SUCCESS_MIN)) |
| 72 | + response = invoke( |
| 73 | + transport.invoke_function, |
| 74 | + authorization=authorization, |
| 75 | + function_id=function_id, |
| 76 | + payload=request_payload, |
| 77 | + ) |
| 78 | + return self._response(response) |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def _function_id(payload: object) -> str: |
| 82 | + if not isinstance(payload, Mapping): |
| 83 | + raise TypeError(_INVALID_FUNCTION_RESPONSE) |
| 84 | + values = cast("Mapping[str, object]", payload) |
| 85 | + function_id = values.get("function_id") |
| 86 | + if not isinstance(function_id, str) or not function_id: |
| 87 | + raise TypeError(_INVALID_FUNCTION_RESPONSE) |
| 88 | + return function_id |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def _response(response: TransportResponse) -> FunctionResponse: |
| 92 | + status = int(response.status_code) |
| 93 | + version = _header(response.headers, "X-Volcano-Version") |
| 94 | + if not _HTTP_SUCCESS_MIN <= status < _HTTP_SUCCESS_MAX and version is None: |
| 95 | + response_payload(response, _HTTP_SUCCESS_MIN) |
| 96 | + data = response.payload |
| 97 | + if not isinstance(data, Mapping): |
| 98 | + raise TypeError(_INVALID_FUNCTION_RESPONSE) |
| 99 | + headers = {} if response.headers is None else dict(response.headers) |
| 100 | + return FunctionResponse( |
| 101 | + data=cast("Mapping[str, JSONValue]", data), |
| 102 | + status=status, |
| 103 | + headers=headers, |
| 104 | + version=version, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def _header(headers: Mapping[str, str] | None, name: str) -> str | None: |
| 109 | + if headers is None: |
| 110 | + return None |
| 111 | + for key, value in headers.items(): |
| 112 | + if key.casefold() == name.casefold(): |
| 113 | + return value |
| 114 | + return None |
| 115 | + |
| 116 | + |
| 117 | +def _function_name(value: object) -> str: |
| 118 | + if not isinstance(value, str) or _FUNCTION_NAME.fullmatch(value) is None: |
| 119 | + raise ValueError(_INVALID_FUNCTION_NAME) |
| 120 | + return value |
| 121 | + |
| 122 | + |
| 123 | +def _function_payload(value: object) -> dict[str, JSONValue]: |
| 124 | + if value is None: |
| 125 | + return {} |
| 126 | + if not isinstance(value, Mapping): |
| 127 | + raise TypeError(_INVALID_FUNCTION_PAYLOAD) |
| 128 | + return dict(cast("Mapping[str, JSONValue]", value)) |
0 commit comments