Skip to content

autonomy.deploy._http_server

Minimal Flask-compatible HTTP server.

Covers the subset of Flask/Werkzeug used by autonomy.deploy.generators.localhost.tendermint.app and autonomy.replay.tendermint: App, Request, Response, jsonify, route decorators, error handlers, an in-process test_client() for unit tests, and a threaded HTTP/1.1 server for production use. Deliberately API-compatible so callers don't need to change and the switch can be reverted.

HTTPException Objects

class HTTPException(Exception)

Base class for HTTP errors.

NotFound Objects

class NotFound(HTTPException)

404 Not Found.

InternalServerError Objects

class InternalServerError(HTTPException)

500 Internal Server Error.

_Args Objects

class _Args(dict)

Query-string args with Flask's get(key, default=None, type=None).

get

def get(key: str,
        default: Any = None,
        type: Optional[Callable[[str], Any]] = None) -> Any

Return key from the query string, optionally coerced via type.

Arguments:

  • key: query-string key to look up.
  • default: value to return when key is absent.
  • type: optional callable to coerce the string value.

Returns:

the (optionally coerced) value or default.

Request Objects

class Request()

Flask-compatible request object.

__init__

def __init__(method: str, path: str, args: Mapping[str, str], data: bytes,
             headers: Mapping[str, str]) -> None

Initialize a request.

get_data

def get_data() -> bytes

Return the raw request body bytes.

get_json

def get_json(silent: bool = False) -> Any

Parse the body as JSON.

Response Objects

class Response()

Minimal Flask-compatible Response.

__init__

def __init__(response: Union[str, bytes, None] = None,
             status: int = 200,
             mimetype: Optional[str] = None,
             headers: Optional[Mapping[str, str]] = None) -> None

Initialize a response.

get_json

def get_json(silent: bool = False) -> Any

Parse the body as JSON.

jsonify

def jsonify(*args: Any, **kwargs: Any) -> Response

Serialize a value to a JSON Response, mirroring flask.jsonify.

  • jsonify({"a": 1}) — one positional arg is used as the body.
  • jsonify(a=1, b=2) — keyword args form an object.
  • jsonify(1, 2, 3) — multiple positional args form a list.

Arguments:

  • args: positional value(s) to serialize.
  • kwargs: keyword pairs to serialize as an object.

Raises:

  • TypeError: when both positional and keyword args are given.

Returns:

a :class:Response with application/json mimetype.

_Route Objects

class _Route()

A single route: method(s), URL pattern, handler.

__init__

def __init__(methods: List[str], pattern: str, handler: _Handler) -> None

Initialize a route and compile its URL pattern.

match

def match(path: str) -> Optional[Dict[str, Any]]

Return captured path params if path matches, else None.

_AppContext Objects

class _AppContext()

Stand-in for flask.ctx.AppContext — a no-op context manager.

push

def push() -> None

No-op.

pop

def pop() -> None

No-op.

__enter__

def __enter__() -> "_AppContext"

Return self.

__exit__

def __exit__(exc_type: Any, exc_val: Any, exc_tb: Any) -> None

No-op.

App Objects

class App()

Minimal Flask-compatible application.

__init__

def __init__(name: str) -> None

Initialize the app with an identifying name.

route

def route(
        rule: str,
        methods: Optional[List[str]] = None) -> Callable[[_Handler], _Handler]

Register a handler for rule (GET by default).

get

def get(rule: str) -> Callable[[_Handler], _Handler]

Register a GET handler.

post

def post(rule: str) -> Callable[[_Handler], _Handler]

Register a POST handler.

errorhandler

def errorhandler(code: int) -> Callable[[_Handler], _Handler]

Register a handler for HTTP code (e.g. 404, 500).

app_context

def app_context() -> _AppContext

Return a no-op application context.

test_client

def test_client() -> "_TestClient"

Return an in-process test client.

run

def run(host: str = "127.0.0.1", port: int = 5000) -> None

Serve this app on host:port — matches flask.Flask.run.

dispatch

def dispatch(req: Request) -> Response

Dispatch req to the matching handler (or an error handler).

_TestClient Objects

class _TestClient()

In-process test client mirroring flask.testing.FlaskClient.

__init__

def __init__(app: App) -> None

Initialize the test client.

__enter__

def __enter__() -> "_TestClient"

Return self (context-manager compatibility).

__exit__

def __exit__(exc_type: Any, exc_val: Any, exc_tb: Any) -> None

No-op cleanup.

get

def get(path: str, **kwargs: Any) -> "_TestResponse"

Send a GET request.

post

def post(path: str, **kwargs: Any) -> "_TestResponse"

Send a POST request.

_TestResponse Objects

class _TestResponse()

Test response mirroring flask.wrappers.Response.

__init__

def __init__(response: Response) -> None

Wrap response for test-client consumers.

get_json

def get_json(silent: bool = False) -> Any

Parse the body as JSON.

run_app

def run_app(app: App, host: str, port: int) -> None

Serve app on host:port using a threaded HTTP/1.1 server (blocks).