Crates.io | twurst-client |
lib.rs | twurst-client |
version | 0.0.1 |
source | src |
created_at | 2024-11-27 13:03:22.588696 |
updated_at | 2024-11-27 13:03:22.588696 |
description | Twirp client related code |
homepage | |
repository | |
max_upload_size | |
id | 1463000 |
size | 28,814 |
Crate implementing the needed runtime for the code generated by twurst-build
in order to run Twirp clients.
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()))
);
}
reqwest-012
allows to use reqwest
0.12 HTTP implementation.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.