Skip to main content

Module call

Module call 

Source
Expand description

Typed protocol routes: a handler for an HttpCall is mounted at the call’s own path with the call’s own method, takes the decoded call (Call<C>) and answers the call’s own response type (Reply<C>). Path, method, payload and answer come from the protocol, so they cannot drift between the server, its OpenAPI document and the clients.

Authentication is enforced by the mount: a route whose C::ROUTE.auth is true answers 401 (unauthorized / token_expired, or the authenticator’s 403 banned) to a request without a valid token BEFORE the handler runs, whether or not the handler takes an AuthContext. The flag in the protocol (and in the OpenAPI document) is a guarantee, also for a game’s own HttpCall mounted with NetBackendServer::call or call_route!.

use net_backend_server::http::call::{Call, CallResult, Reply};
use net_backend_server::protocol::chat::{ListRooms, RoomInfo, RoomKind};
use net_backend_server::protocol::{Page, RoomId};
use net_backend_server::{AuthContext, Config, NetBackendServer};

async fn rooms(_who: AuthContext, Call(call): Call<ListRooms>) -> CallResult<ListRooms> {
    let _ = call.page;
    Ok(Reply::new(Page::new(vec![RoomInfo::new(RoomId(1), RoomKind::Room).with_key("world")], None)))
}

// Mounted at GET /v1/chat/rooms (from `ListRooms::ROUTE`), undocumented; documented handlers
// carry `#[utoipa::path(..)]` and go through `call_route!(ListRooms, rooms)`.
let server = NetBackendServer::new(Config::default()).call::<ListRooms, _, _, _>(rooms);

Structs§

Call
The decoded call of a typed route: its path parameters and payload (JSON body or query), shape-checked by the protocol’s HttpCall::from_parts. A malformed body answers 400 bad_request (or 413 / 415 like ApiJson), a bad path parameter 400 / 422.
Reply
The answer of a typed route: 200 with the call’s response as JSON, plus optional headers (e.g. an ETag).

Traits§

CallHandler
A handler for the call C: an async function whose LAST argument is Call<C> (any other extractors before it) and which returns CallResult<C>. Implemented for such functions with up to eight extractors; T is the tuple of the other extractors.

Functions§

documented
handler mounted at C::ROUTE with the OpenAPI operation of doc (the output of utoipa_axum::routes!(handler); only its operation and schemas are used: the path and the method in the document are always C’s). Use call_route!.
method_router
The method router of handler for C’s method; with C::ROUTE.auth, requests without an authenticated caller are answered 401 before the handler runs. A method this server version does not know mounts nothing (every request: 405; logged as an error; NetBackendServer::call refuses it at build).
undocumented
handler mounted at C::ROUTE on router, without OpenAPI documentation.

Type Aliases§

CallResult
What a typed handler returns.