twurst-client

Crates.iotwurst-client
lib.rstwurst-client
version0.0.1
sourcesrc
created_at2024-11-27 13:03:22.588696
updated_at2024-11-27 13:03:22.588696
descriptionTwirp client related code
homepage
repository
max_upload_size
id1463000
size28,814
Helsing SA (helsing-ai-user)

documentation

README

Crate implementing the needed runtime for the code generated by twurst-build in order to run Twirp clients.

Getting started

To start you need first to have a gRPC .proto file (e.g. service.proto).

Then build your proto files by creating a build.rs file with:

fn main() -> std::io::Result<()> {
    twurst_build::TwirpBuilder::new()
        .with_client()
        .compile_protos(&["proto/service.proto"], &["proto"])
}

and add to your Cargo.toml:

[dependencies]
prost = ""
prost-types = ""
prost-reflect = ""
twurst-client = { version = "", features=["reqwest-012"] }

[build-dependencies]
twurst-build = ""

Note that protoc must be available, see prost-build documentation on this topic. If you are using Nix, nix-shell -p protobuf is enough to provide protoc.

Then you can use the Twirp client with:

use twurst_client::TwirpHttpClient;

mod proto {
    include!(concat!(env!("OUT_DIR"), "/example.rs")); // example is the name of your proto package
}

async fn main() {
    let twirp_client = TwirpHttpClient::new_using_reqwest_012("http://example.com/twirp");
    let client = proto::ExampleServiceClient::new(twirp_client); // ExampleServiceClient is the name of the gRPC service
    let response = client.test(TestRequest {}).await?; // Does a Twirp request
}

Note that you can custom the HTTP client with any tower or tower-http layer. For example to add a basic authorization header to all requests:

use twurst_client::{TwirpHttpClient, Reqwest012Service};
use tower::ServiceBuilder;
use tower_http::auth::AddAuthorizationLayer;

fn main() {
    let twirp_client = TwirpHttpClient::new_with_base(
        ServiceBuilder::new()
            .layer(AddAuthorizationLayer::basic("username", "password"))
            .service(Reqwest012Service::from(reqwest::Client::new()))
    );
}

Cargo features

  • reqwest-012 allows to use reqwest 0.12 HTTP implementation.

License

Copyright 2024 Helsing GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Commit count: 0

cargo fmt