Expand description
Calling the app’s other services.
A Client calls one service. It is const, so it can be a static:
use encore::services;
static BILLING: services::Client = services::client("billing");
async fn invoices() -> anyhow::Result<Vec<Invoice>> {
Ok(BILLING.get("/invoices").send().await?.error_for_status()?.json().await?)
}Its calls carry the trace and Request Context (crate::reqctx) of the
context they are sent in to the app’s services, so the services they reach
continue the request they are made for. A request to anything else carries
no Request Context: who a request is made for is no business of a third
party’s. Every client shares the same connections, so making one where it
is needed costs no more than a static.
A tower client, as hyper-util’s or a tonic channel, does the same through
PropagateLayer.
Looking up a service’s URL, or making a client for it, makes the calling service depend on it. Encore reads the dependency from the code, as it reads which resources a service uses: so the name must be a string literal, the function must be called directly rather than passed around, and the call must be in a service.
§Generated clients
A service that lists another in its encore.service.json clients gets
clients for calling it in encore_gen::clients: a module per service,
with a module per API the service declares. Each operation is a function
of its module, returning the call, which .await sends, so there is no
client to make first:
mod encore_gen;
use encore_gen::clients::catalog;
let product = catalog::openapi::get_product("xyz").await?;
let users = catalog::openapi::list_users().limit(10).await?;A call that fails is a CallError, whatever kind of API it calls, and
? turns it into an encore::Error with the same code:
let product = match catalog::openapi::get_product("xyz").await {
Ok(product) => Some(product),
Err(err) if err.code == encore::ErrCode::NotFound => None,
Err(err) => return Err(err.into()),
};UnlessNotFound makes a NotFound failure None, for a call for
something that may not be there:
use encore::services::UnlessNotFound;
let product = catalog::openapi::get_product("xyz").await.unless_not_found()?;A call is a Future once into_future makes it one, for what takes a
future rather than something to await, as tokio::spawn and join_all
do. It carries the context of the code that made it, as an awaited call
does:
use std::future::IntoFuture;
let product = tokio::spawn(catalog::openapi::get_product("xyz").into_future());Every call also takes a timeout and headers of its own, which go over its
client’s, and a module’s Client holds settings for every call made with
it:
let product = catalog::openapi::get_product("xyz")
.timeout(Duration::from_secs(2))
.await?;
let storefront = catalog::storefront::Client::new().header("x-tenant", "a");The clients send as a Client does, so their calls carry the trace and
Request Context of the context they are made in, and share its
connections; a gRPC API’s calls, which are Unary, ServerStreaming,
ClientStreaming and BidiStreaming, share one connection per base
URL. They speak gRPC over HTTP/2, through tonic, as every language’s
generated clients do: in the clear (h2c) to an http:// base URL, and
with TLS to an https:// one, trusting the system’s roots, or those of
SSL_CERT_FILE. A gRPC call’s timeout is its deadline, which holds for
the whole call, connecting and each message of a stream too. A
bidirectional call is ready before the service answers, as a service may
wait for a message first. A gRPC message keeps the fields its schema
lacks, as a newer schema’s, and sends them on; a failed call keeps its
status’s details, in CallError::grpc_details. A client holds only its
settings, so one made for a single call costs nothing to make, and there
is nothing to close.
§Tests
No service runs under encore test. Every service still has a base URL
there, so a client can be made as usual, but a call to it is answered with
501 Not Implemented and an error saying which service and path the test
called: mock the call in the test instead.
Re-exports§
pub use reqwest;
Structs§
- Bidi
Streaming - A call sending a stream of messages, answered with another.
- Call
Error - A call to another service that failed: an error answer, or none at all.
- Client
- A client for one of the app’s services, made with
client. - Client
Streaming - A call sending a stream of messages, answered with one.
- Propagate
- The service of a
PropagateLayer. - Propagate
Layer - A tower
Layerwhose services send the trace and Request Context of the context a request is sent in with each request for one of the app’s services, and no Request Context with any other: for hyper-util’s client, a tonic channel, or any tower HTTP client. - Request
Builder - A request a
Clientsends: reqwest’s builder, whosesendcarries the call’s context. - Server
Streaming - A call answered with a stream of messages.
- Streaming
- The messages a call is answered with: a
Streamof them, which ends after the last, or with the call’s error. The call’s deadline holds for each message: one that has not arrived by then is the call’sDeadlineExceeded, which ends the call. - Unary
- A unary call: one message each way.
Traits§
- Stream
- The Stream trait a gRPC API’s streams implement and its client streams take. A stream of values produced asynchronously.
- Unless
NotFound - Makes a
NotFoundfailure of a callNone, for a call for something that may not be there:
Functions§
- base_
url - The base URL the calling service reaches
serviceat, for service-to-service calls, without a trailing slash: a call’s path goes after it,{base}/users/42. - client
- A client for
service, for service-to-service calls: its requests take the service’s paths, and carry the trace and Request Context of the context they are sent in.