| Title: | Client for 'gRPC' Remote Procedure Calls over HTTP/2 |
|---|---|
| Description: | A client for 'gRPC' remote procedure calls, written in 'R' on top of the HTTP/2 requests that 'zuhttp' makes. Implements the 'gRPC' wire protocol: length-prefixed messages, request headers and metadata, deadlines, cancellation, and the call status carried in HTTP trailers, with error details decoded into structured conditions. Supports unary and server-streaming calls, with streamed responses read one message at a time. Messages are encoded and decoded by the caller, for example with 'zuproto'. |
| Authors: | Pedro Baltazar [aut, cre, cph], Google LLC [cph] (google/rpc protocol definitions in inst/descriptors) |
| Maintainer: | Pedro Baltazar <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-12-05 18:19:15 UTC |
| Source: | https://github.com/pedrobtz/zugrpc |
Credentials authenticate a channel's calls without becoming part of the
channel. The secret stays in this R session: printing, saving or sending a
channel or a credentials object to another process carries only a key, and
a copy that reaches another process refuses every call with
zugrpc_error_argument until it is created again there.
grpc_bearer_token(token) grpc_call_credentials(provider)grpc_bearer_token(token) grpc_call_credentials(provider)
token |
The token, one non-empty string of printable ASCII. |
provider |
A function of one argument, the method being called (as
|
grpc_bearer_token() sends a fixed token as authorization: Bearer <token>. grpc_call_credentials() calls provider before every call, so
a token that expires can be refreshed. Its result is sent and never
stored.
Credentials need TLS: grpc_channel() accepts them for an https:// URL,
or for an http:// URL whose host is localhost, a 127.x.x.x address
or [::1].
A zugrpc_credentials, for credentials in grpc_channel().
creds <- grpc_bearer_token("my-token") creds ch <- grpc_channel("https://grpc.example.org:443", credentials = creds) ch grpc_close(ch) # A token read when each call is made, for one that expires: refresh <- grpc_call_credentials(function(method) { c(authorization = paste("Bearer", Sys.getenv("MY_TOKEN"))) })creds <- grpc_bearer_token("my-token") creds ch <- grpc_channel("https://grpc.example.org:443", credentials = creds) ch grpc_close(ch) # A token read when each call is made, for one that expires: refresh <- grpc_call_credentials(function(method) { c(authorization = paste("Bearer", Sys.getenv("MY_TOKEN"))) })
Sends one request message to a method and returns the one response
message, with the server's initial and trailing metadata. The request is
already encoded, for example by zuproto::proto_encode(); zugrpc only
frames it. A call that ends with a status other than OK raises a
condition instead of returning.
grpc_call(channel, method, request, metadata = NULL, deadline = NULL)grpc_call(channel, method, request, metadata = NULL, deadline = NULL)
channel |
A channel from |
method |
The full method name, |
request |
The encoded request message, a raw vector. |
metadata |
Request metadata: a named character vector, or a named
list whose |
deadline |
Seconds from now, or an absolute |
A list with elements message (raw, the response message),
headers and trailers (named lists of the initial and trailing
metadata, with -bin values as raw vectors) and status ("OK"). A
-bin value holding several, joined by commas, gives one entry each. One
that is not base64 is dropped with a zugrpc_warning_metadata warning.
A non-OK status raises zugrpc_error_status, whose class also carries
the status as zugrpc_error_<name>, for example
zugrpc_error_unavailable. Its fields are grpc_status, grpc_code,
grpc_message, details, http_status, method, headers and
trailers. details is the decoded google.rpc.Status from the
grpc-status-details-bin trailer, or NULL when there is none: a list of
code, message and details. Its details are the decoded error
details, named by type, such as ErrorInfo with its reason, domain
and metadata. Two details of one type share its name, so
d[names(d) == "BadRequest"] selects them all. A trailer that does not
decode gives a zugrpc_warning_details warning, and details is NULL.
A failure below gRPC, such as a refused connection or a TLS error,
raises zugrpc_error_transport, which is also zugrpc_error_unavailable.
The zuhttp condition is its parent. A local timeout is
DEADLINE_EXCEEDED, and an interrupt raises zugrpc_error_interrupt.
A response that is not gRPC, or breaks its framing, raises
zugrpc_error_protocol. See grpc_status_codes().
# A test server, from zuhttp's mock transport: it answers every call with # an empty message and status OK. fake <- zuhttp::zu_mock_transport(function(req) { zuhttp::zu_response( 200L, headers = c("content-type" = "application/grpc+proto"), body = as.raw(c(0, 0, 0, 0, 0)), trailers = c("grpc-status" = "0") ) }) ch <- grpc_channel( "http://localhost:50051", client = zuhttp::zu_client(transport = fake) ) res <- grpc_call(ch, "/grpc.testing.TestService/EmptyCall", raw()) res$status res$message# A test server, from zuhttp's mock transport: it answers every call with # an empty message and status OK. fake <- zuhttp::zu_mock_transport(function(req) { zuhttp::zu_response( 200L, headers = c("content-type" = "application/grpc+proto"), body = as.raw(c(0, 0, 0, 0, 0)), trailers = c("grpc-status" = "0") ) }) ch <- grpc_channel( "http://localhost:50051", client = zuhttp::zu_client(transport = fake) ) res <- grpc_call(ch, "/grpc.testing.TestService/EmptyCall", raw()) res$status res$message
A channel holds what every call to one server shares: the server's
address, the headers sent on every call, the message size limit, and the
zuhttp client whose connection pool carries the calls. An http:// URL
speaks cleartext HTTP/2 with prior knowledge (h2c), for a local server.
An https:// URL negotiates HTTP/2 over TLS. gRPC has no HTTP/1.1
fallback, so the channel's client always requires HTTP/2.
grpc_channel( url, headers = NULL, credentials = NULL, tls = NULL, keepalive = grpc_keepalive(), max_message = 128 * 1024^2, client = NULL )grpc_channel( url, headers = NULL, credentials = NULL, tls = NULL, keepalive = grpc_keepalive(), max_message = 128 * 1024^2, client = NULL )
url |
The server, as |
headers |
Metadata sent on every call: a named character vector or a
named list, as |
credentials |
Call credentials from |
tls |
Certificate trust settings from |
keepalive |
HTTP/2 keepalive from |
max_message |
The largest response message accepted, in bytes. The
default, 128 MiB, is Spark Connect's. A larger message fails the call
with |
client |
A |
A zugrpc_channel.
grpc_call() to make a call, grpc_close() to close it.
ch <- grpc_channel("http://localhost:50051") ch grpc_close(ch)ch <- grpc_channel("http://localhost:50051") ch grpc_close(ch)
Cancels the channel's open streams and closes its idle connections. A call
on a closed channel raises zugrpc_error_argument, and a cancelled stream
raises zugrpc_error_cancelled when it is next read. Closing a channel
twice does nothing.
grpc_close(channel)grpc_close(channel)
channel |
A channel from |
channel, invisibly.
ch <- grpc_channel("http://localhost:50051") grpc_close(ch)ch <- grpc_channel("http://localhost:50051") grpc_close(ch)
A channel's connection can sit idle between calls, and a server or a
middlebox may drop it without saying so. With keepalive, a connection
idle for time seconds is sent an HTTP/2 PING before its next call, and
replaced by a new one if no answer comes within timeout seconds.
grpc_keepalive(time = 60, timeout = 20)grpc_keepalive(time = 60, timeout = 20)
time |
Seconds of idleness before a PING, from 0.001 (1 ms) to 2147483.647 (about 24.8 days). |
timeout |
Seconds to wait for the answer, in the same range. |
A zugrpc_keepalive, for grpc_channel().
grpc_keepalive() grpc_keepalive(time = 30, timeout = 10)grpc_keepalive() grpc_keepalive(time = 30, timeout = 10)
The 17 status codes a gRPC call can end with, as the gRPC specification
defines them. A failed call raises a condition of class
zugrpc_error_<name> in lower case, for example zugrpc_error_unavailable.
grpc_status_codes()grpc_status_codes()
A data frame with one row per code and columns code (integer),
name and description (character).
grpc_status_codes()grpc_status_codes()
Sends one request message and returns a stream that yields the response messages one at a time, as they arrive. The server sends only as far ahead as you read, so a stream you stop reading holds the server back rather than filling memory.
grpc_stream(channel, method, request, metadata = NULL, deadline = NULL)grpc_stream(channel, method, request, metadata = NULL, deadline = NULL)
channel |
A channel from |
method |
The full method name, |
request |
The encoded request message, a raw vector. |
metadata |
Request metadata: a named character vector, or a named
list whose |
deadline |
Seconds from now, or an absolute |
The stream is an object with five methods:
$headers() returns the initial metadata, a named list.
$next_message() returns the next message as a raw vector, or NULL
at the end of the stream. It raises a condition, as grpc_call() does,
when the call ends with a status other than OK. A stream that fails
any other way, such as a lost connection, a passed deadline or a broken
message, raises the same condition again on every later call. The
complete messages that arrived before a broken one are returned first.
$trailers() returns the trailing metadata once $next_message() has
returned NULL, and NULL before.
$status() returns the final status name once the stream has ended,
and NULL before: "OK", the server's status, or the status the stream
failed with, such as "CANCELLED" or "DEADLINE_EXCEEDED". A stream
that failed without a status, on a protocol error, stays NULL.
$cancel() stops the call. The connection stays open for other calls.
A second $cancel() does nothing. A later $next_message() raises
zugrpc_error_cancelled.
A stream that is no longer referenced is cancelled when it is
garbage-collected, and grpc_close() cancels every open stream of its
channel.
A zugrpc_stream.
# A test server, from zuhttp's mock transport: it streams two messages # and ends with status OK. fake <- zuhttp::zu_mock_transport(function(req) { zuhttp::zu_response( 200L, headers = c("content-type" = "application/grpc+proto"), body = as.raw(c(0, 0, 0, 0, 1, 0x61, 0, 0, 0, 0, 1, 0x62)), trailers = c("grpc-status" = "0") ) }) ch <- grpc_channel( "http://localhost:50051", client = zuhttp::zu_client(transport = fake) ) st <- grpc_stream(ch, "/grpc.testing.TestService/StreamingOutputCall", raw()) while (!is.null(m <- st$next_message())) print(rawToChar(m)) st$status()# A test server, from zuhttp's mock transport: it streams two messages # and ends with status OK. fake <- zuhttp::zu_mock_transport(function(req) { zuhttp::zu_response( 200L, headers = c("content-type" = "application/grpc+proto"), body = as.raw(c(0, 0, 0, 0, 1, 0x61, 0, 0, 0, 0, 1, 0x62)), trailers = c("grpc-status" = "0") ) }) ch <- grpc_channel( "http://localhost:50051", client = zuhttp::zu_client(transport = fake) ) st <- grpc_stream(ch, "/grpc.testing.TestService/StreamingOutputCall", raw()) while (!is.null(m <- st$next_message())) print(rawToChar(m)) st$status()
Reports the zuhttp that carries zugrpc's calls: its version, whether it
speaks HTTP/2, which gRPC requires, and its TLS backend.
zugrpc_info()zugrpc_info()
A list with elements version (zugrpc's, a string), zuhttp
(its version, a string), http2 (logical), tls_backend (a string)
and tls_capabilities (character), printed as a short report.
zuhttp::zu_info() for the full transport report.
zugrpc_info()zugrpc_info()