chore: Reorganize examples and interop crates (#180)

* chore: Reorganize examples and interop crates

* fix interop tests
This commit is contained in:
Lucio Franco
2019-12-12 11:53:15 -05:00
committed by GitHub
parent f096a238ac
commit d9a481baef
69 changed files with 25 additions and 22 deletions
+21
View File
@@ -0,0 +1,21 @@
# Google Cloud Pubsub example
This example will attempt to fetch a list of topics using the google
gRPC protobuf specification. This will use an OAuth token and TLS to
fetch the list of topics.
First, you must generate a access token via the [OAuth playground]. From here
select the `Cloud Pub/Sub API v1` and its urls as the scope. This will start
the OAuth flow. Then you must hit the `Exchange authorization code for tokens`
button to generate an `access_token` which will show up in the HTTP response
to the right under the `access_token` field in the response json.
Once, you have this token you must fetch your GCP project id which can be found
from the main page on the dashboard. When you have both of these items you can
run the example like so:
```shell
GCP_AUTH_TOKEN="<access-token>" cargo run --bin gcp-client -- <project-id>
```
[OAuth playground]: https://developers.google.com/oauthplayground/
+55
View File
@@ -0,0 +1,55 @@
pub mod api {
tonic::include_proto!("google.pubsub.v1");
}
use api::{publisher_client::PublisherClient, ListTopicsRequest};
use http::header::HeaderValue;
use tonic::{
transport::{Certificate, Channel, ClientTlsConfig},
Request,
};
const ENDPOINT: &str = "https://pubsub.googleapis.com";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = std::env::var("GCP_AUTH_TOKEN").map_err(|_| {
"Pass a valid 0Auth bearer token via `GCP_AUTH_TOKEN` environment variable.".to_string()
})?;
let project = std::env::args()
.skip(1)
.next()
.ok_or("Expected a project name as the first argument.".to_string())?;
let bearer_token = format!("Bearer {}", token);
let header_value = HeaderValue::from_str(&bearer_token)?;
let certs = tokio::fs::read("tonic-examples/data/gcp/roots.pem").await?;
let tls_config = ClientTlsConfig::with_rustls()
.ca_certificate(Certificate::from_pem(certs.as_slice()))
.domain_name("pubsub.googleapis.com");
let channel = Channel::from_static(ENDPOINT)
.intercept_headers(move |headers| {
headers.insert("authorization", header_value.clone());
})
.tls_config(tls_config)
.connect()
.await?;
let mut service = PublisherClient::new(channel);
let response = service
.list_topics(Request::new(ListTopicsRequest {
project: format!("projects/{0}", project),
page_size: 10,
..Default::default()
}))
.await?;
println!("RESPONSE={:?}", response);
Ok(())
}