Skip to content

Commit

Permalink
Move with_auth method for ClientConfig into each component's feat…
Browse files Browse the repository at this point in the history
…ures (#172)
  • Loading branch information
yoshidan authored Jul 1, 2023
1 parent c4f47b5 commit 8dddd76
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 456 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ jobs:
with:
command: test
args: --release --all-features --manifest-path foundation/gax/Cargo.toml
- uses: actions-rs/cargo@v1
name: default-test
with:
command: test
args: --release --all-features --manifest-path default/Cargo.toml
- name: cargo-deny
uses: EmbarkStudios/cargo-deny-action@v1
with:
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ members = [
"storage",
"bigquery",
"spanner-derive",
"default",
]
7 changes: 5 additions & 2 deletions bigquery/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "google-cloud-bigquery"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["yoshidan <[email protected]>"]
repository = "https://github.com/yoshidan/google-cloud-rust/tree/main/bigquery"
Expand Down Expand Up @@ -28,6 +28,8 @@ bigdecimal = { version="0.3", features=["serde"] }
num-bigint = "0.4"
backon = "0.4"

google-cloud-auth = { optional = true, version = "0.11", path="../foundation/auth", default-features=false }

[dev-dependencies]
tokio = { version="1.20", features=["rt-multi-thread"] }
serial_test = "0.9"
Expand All @@ -38,7 +40,8 @@ google-cloud-auth = { path = "../foundation/auth", default-features=false }
base64-serde = "0.7"

[features]
default = ["default-tls"]
default = ["default-tls", "auth"]
default-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]
trace = []
auth = ["google-cloud-auth"]
6 changes: 2 additions & 4 deletions bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Google Cloud Platform BigQuery Client library.

```toml
[dependencies]
google-cloud-bigquery = <version>
google-cloud-default = { version = <version>, features = ["bigquery"] }
google-cloud-bigquery = version
```

## Quick Start
Expand All @@ -23,7 +22,6 @@ This is also described in [google-cloud-auth](https://github.com/yoshidan/google

```rust
use google_cloud_pubsub::client::{ClientConfig, Client};
use google_cloud_default::biqquery::CreateAuthExt;

async fn run() {
let (config, project_id) = ClientConfig::new_with_auth().await.unwrap();
Expand All @@ -36,8 +34,8 @@ you can parse your own version of the 'credentials-file' and use it like that:

```rust
use google_cloud_auth::credentials::CredentialsFile;
// or google_cloud_bigquery::client::google_cloud_auth::credentials::CredentialsFile
use google_cloud_bigquery::client::{ClientConfig, Client};
use google_cloud_default::biqquery::CreateAuthExt;

async fn run(cred: CredentialsFile) {
let (config, project_id) = ClientConfig::new_with_credentials(cred).await.unwrap();
Expand Down
73 changes: 52 additions & 21 deletions bigquery/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,56 @@ impl ClientConfig {
}
}

#[cfg(feature = "auth")]
pub use google_cloud_auth;

#[cfg(feature = "auth")]
impl ClientConfig {
pub async fn new_with_auth() -> Result<(Self, Option<String>), google_cloud_auth::error::Error> {
let ts_http =
google_cloud_auth::token::DefaultTokenSourceProvider::new(Self::bigquery_http_auth_config()).await?;
let ts_grpc =
google_cloud_auth::token::DefaultTokenSourceProvider::new(Self::bigquery_grpc_auth_config()).await?;
let project_id = ts_grpc.project_id.clone();
let config = Self::new(Box::new(ts_http), Box::new(ts_grpc));
Ok((config, project_id))
}

pub async fn new_with_credentials(
credentials: google_cloud_auth::credentials::CredentialsFile,
) -> Result<(Self, Option<String>), google_cloud_auth::error::Error> {
let ts_http = google_cloud_auth::token::DefaultTokenSourceProvider::new_with_credentials(
Self::bigquery_http_auth_config(),
Box::new(credentials.clone()),
)
.await?;
let ts_grpc = google_cloud_auth::token::DefaultTokenSourceProvider::new_with_credentials(
Self::bigquery_grpc_auth_config(),
Box::new(credentials),
)
.await?;
let project_id = ts_grpc.project_id.clone();
let config = Self::new(Box::new(ts_http), Box::new(ts_grpc));
Ok((config, project_id))
}

fn bigquery_http_auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: None,
scopes: Some(&crate::http::bigquery_client::SCOPES),
sub: None,
}
}

fn bigquery_grpc_auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: Some(crate::grpc::apiv1::conn_pool::AUDIENCE),
scopes: Some(&crate::grpc::apiv1::conn_pool::SCOPES),
sub: None,
}
}
}

#[derive(Clone)]
pub struct Client {
dataset_client: BigqueryDatasetClient,
Expand Down Expand Up @@ -399,14 +449,10 @@ mod tests {
use time::macros::datetime;
use time::{Date, OffsetDateTime, Time};

use google_cloud_auth::project::Config;
use google_cloud_auth::token::DefaultTokenSourceProvider;
use google_cloud_googleapis::cloud::bigquery::storage::v1::read_session::TableReadOptions;

use crate::client::{Client, ClientConfig, ReadTableOption};
use crate::grpc::apiv1;
use crate::http::bigquery_client::test::TestData;
use crate::http::bigquery_client::SCOPES;
use crate::http::job::query::QueryRequest;
use crate::http::table::TableReference;
use crate::query;
Expand All @@ -418,23 +464,8 @@ mod tests {
}

async fn create_client() -> (Client, String) {
let http_tsp = DefaultTokenSourceProvider::new(Config {
audience: None,
scopes: Some(&SCOPES),
sub: None,
})
.await
.unwrap();
let grpc_tsp = DefaultTokenSourceProvider::new(Config {
audience: Some(apiv1::conn_pool::AUDIENCE),
scopes: Some(&apiv1::conn_pool::SCOPES),
sub: None,
})
.await
.unwrap();
let project_id = http_tsp.source_credentials.clone().unwrap().project_id.unwrap();
let client_config = ClientConfig::new(Box::new(http_tsp), Box::new(grpc_tsp)).with_debug(false);
(Client::new(client_config).await.unwrap(), project_id)
let (client_config, project_id) = ClientConfig::new_with_auth().await.unwrap();
(Client::new(client_config).await.unwrap(), project_id.unwrap())
}

#[tokio::test]
Expand Down
15 changes: 3 additions & 12 deletions bigquery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
//!
//! Google Cloud Platform BigQuery Client library.
//!
//! ## Installation
//!
//! ```toml
//! [dependencies]
//! google-cloud-bigquery = <version>
//! google-cloud-default = { version = <version>, features = ["bigquery"] }
//! ```
//!
//! ## Quick Start
//!
//! ### CreateClient
Expand All @@ -19,9 +11,8 @@
//!
//! This is also described in [google-cloud-auth](https://github.com/yoshidan/google-cloud-rust/blob/main/foundation/auth/README.md)
//!
//! ```ignore
//! ```rust
//! use google_cloud_bigquery::client::{ClientConfig, Client};
//! use google_cloud_default::biqquery::CreateAuthExt;
//!
//! async fn run() {
//! let (config, project_id) = ClientConfig::new_with_auth().await.unwrap();
Expand All @@ -32,10 +23,10 @@
//! When you can't use the `gcloud` authentication but you have a different way to get your credentials (e.g a different environment variable)
//! you can parse your own version of the 'credentials-file' and use it like that:
//!
//! ```ignore
//! ```rust
//! use google_cloud_auth::credentials::CredentialsFile;
//! // or google_cloud_bigquery::client::google_cloud_auth::credentials::CredentialsFile
//! use google_cloud_bigquery::client::{ClientConfig, Client};
//! use google_cloud_default::biqquery::CreateAuthExt;
//!
//! async fn run(cred: CredentialsFile) {
//! let (config, project_id) = ClientConfig::new_with_credentials(cred).await.unwrap();
Expand Down
38 changes: 0 additions & 38 deletions default/Cargo.toml

This file was deleted.

19 changes: 0 additions & 19 deletions default/README.md

This file was deleted.

Loading

0 comments on commit 8dddd76

Please sign in to comment.