Struct tonic::client::Grpc

source ·
pub struct Grpc<T> { /* private fields */ }
Expand description

A gRPC client dispatcher.

This will wrap some inner GrpcService and will encode/decode messages via the provided codec.

Each request method takes a Request, a PathAndQuery, and a Codec. The request contains the message to send via the Codec::encoder. The path determines the fully qualified path that will be append to the outgoing uri. The path must follow the conventions explained in the gRPC protocol definition under Path →. An example of this path could look like /greeter.Greeter/SayHello.

Implementations§

source§

impl<T> Grpc<T>

source

pub fn new(inner: T) -> Self

Creates a new gRPC client with the provided GrpcService.

source

pub fn with_origin(inner: T, origin: Uri) -> Self

Creates a new gRPC client with the provided GrpcService and Uri.

The provided Uri will use only the scheme and authority parts as the path_and_query portion will be set for each method.

source

pub fn send_compressed(self, encoding: CompressionEncoding) -> Self

Compress requests with the provided encoding.

Requires the server to accept the specified encoding, otherwise it might return an error.

§Example

The most common way of using this is through a client generated by tonic-build:

use tonic::transport::Channel;

let channel = Channel::builder("127.0.0.1:3000".parse().unwrap())
    .connect()
    .await
    .unwrap();

let client = TestClient::new(channel).send_compressed(CompressionEncoding::Gzip);
source

pub fn accept_compressed(self, encoding: CompressionEncoding) -> Self

Enable accepting compressed responses.

Requires the server to also support sending compressed responses.

§Example

The most common way of using this is through a client generated by tonic-build:

use tonic::transport::Channel;

let channel = Channel::builder("127.0.0.1:3000".parse().unwrap())
    .connect()
    .await
    .unwrap();

let client = TestClient::new(channel).accept_compressed(CompressionEncoding::Gzip);
source

pub fn max_decoding_message_size(self, limit: usize) -> Self

Limits the maximum size of a decoded message.

§Example

The most common way of using this is through a client generated by tonic-build:

use tonic::transport::Channel;

let channel = Channel::builder("127.0.0.1:3000".parse().unwrap())
    .connect()
    .await
    .unwrap();

// Set the limit to 2MB, Defaults to 4MB.
let limit = 2 * 1024 * 1024;
let client = TestClient::new(channel).max_decoding_message_size(limit);
source

pub fn max_encoding_message_size(self, limit: usize) -> Self

Limits the maximum size of an encoded message.

§Example

The most common way of using this is through a client generated by tonic-build:

use tonic::transport::Channel;

let channel = Channel::builder("127.0.0.1:3000".parse().unwrap())
    .connect()
    .await
    .unwrap();

// Set the limit to 2MB, Defaults to 4MB.
let limit = 2 * 1024 * 1024;
let client = TestClient::new(channel).max_encoding_message_size(limit);
source

pub async fn ready(&mut self) -> Result<(), T::Error>
where T: GrpcService<BoxBody>,

Check if the inner GrpcService is able to accept a new request.

This will call GrpcService::poll_ready until it returns ready or an error. If this returns ready the inner GrpcService is ready to accept one more request.

source

pub async fn unary<M1, M2, C>( &mut self, request: Request<M1>, path: PathAndQuery, codec: C, ) -> Result<Response<M2>, Status>
where T: GrpcService<BoxBody>, T::ResponseBody: Body + Send + 'static, <T::ResponseBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>, C: Codec<Encode = M1, Decode = M2>, M1: Send + Sync + 'static, M2: Send + Sync + 'static,

Send a single unary gRPC request.

source

pub async fn client_streaming<S, M1, M2, C>( &mut self, request: Request<S>, path: PathAndQuery, codec: C, ) -> Result<Response<M2>, Status>
where T: GrpcService<BoxBody>, T::ResponseBody: Body + Send + 'static, <T::ResponseBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>, S: Stream<Item = M1> + Send + 'static, C: Codec<Encode = M1, Decode = M2>, M1: Send + Sync + 'static, M2: Send + Sync + 'static,

Send a client side streaming gRPC request.

source

pub async fn server_streaming<M1, M2, C>( &mut self, request: Request<M1>, path: PathAndQuery, codec: C, ) -> Result<Response<Streaming<M2>>, Status>
where T: GrpcService<BoxBody>, T::ResponseBody: Body + Send + 'static, <T::ResponseBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>, C: Codec<Encode = M1, Decode = M2>, M1: Send + Sync + 'static, M2: Send + Sync + 'static,

Send a server side streaming gRPC request.

source

pub async fn streaming<S, M1, M2, C>( &mut self, request: Request<S>, path: PathAndQuery, codec: C, ) -> Result<Response<Streaming<M2>>, Status>
where T: GrpcService<BoxBody>, T::ResponseBody: Body + Send + 'static, <T::ResponseBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>, S: Stream<Item = M1> + Send + 'static, C: Codec<Encode = M1, Decode = M2>, M1: Send + Sync + 'static, M2: Send + Sync + 'static,

Send a bi-directional streaming gRPC request.

Trait Implementations§

source§

impl<T: Clone> Clone for Grpc<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Grpc<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Grpc<T>

§

impl<T> RefUnwindSafe for Grpc<T>
where T: RefUnwindSafe,

§

impl<T> Send for Grpc<T>
where T: Send,

§

impl<T> Sync for Grpc<T>
where T: Sync,

§

impl<T> Unpin for Grpc<T>
where T: Unpin,

§

impl<T> UnwindSafe for Grpc<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more