Skip to content

Commit

Permalink
add new_http_client & fix cosmos_client (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctaggart authored Jun 1, 2021
1 parent 06dc4d3 commit 114df85
Show file tree
Hide file tree
Showing 280 changed files with 18,949 additions and 3,021 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
- name: check core for wasm
run: cargo check -p azure_core --target=wasm32-unknown-unknown

- name: check core for hyper
run: cargo check -p azure_core --no-default-features --features enable_hyper
# - name: check core for hyper
# run: cargo check -p azure_core --no-default-features --features enable_hyper

- name: sdk tests
run: cargo test --all
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ sample/
*.swp
*.swo
*.vim
__azurite*.json
11 changes: 11 additions & 0 deletions sdk/core/src/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ use http::{Request, Response, StatusCode};
#[allow(unused_imports)]
use hyper_rustls::HttpsConnector;
use serde::Serialize;
use std::sync::Arc;

#[cfg(feature = "enable_reqwest")]
pub fn new_http_client() -> Arc<dyn HttpClient> {
Arc::new(reqwest::Client::new())
}

#[cfg(feature = "enable_hyper")]
pub fn new_http_client() -> Arc<dyn HttpClient> {
Arc::new(hyper::Client::builder().build(hyper_rustls::HttpsConnector::with_native_roots()))
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use bytes_stream::*;
pub use context::Context;
pub use errors::*;
pub use headers::AddAsHeader;
pub use http_client::{to_json, HttpClient};
pub use http_client::{new_http_client, to_json, HttpClient};
pub use models::*;
pub use request::*;
pub use response::*;
Expand Down
4 changes: 3 additions & 1 deletion sdk/core/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use crate::etag::Etag;
pub use crate::request_options::*;
pub use crate::{AddAsHeader, AppendToUrlQuery, HttpClient, RequestId, SessionToken, EMPTY_BODY};
pub use crate::{
new_http_client, AddAsHeader, AppendToUrlQuery, HttpClient, RequestId, SessionToken, EMPTY_BODY,
};
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/attachments_00.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;
use std::sync::Arc;

// Now we create a sample struct. The Cow trick
// allows us to use the same struct for serializing
Expand Down Expand Up @@ -44,7 +42,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account, authorization_token);
let client = client.into_database_client(database_name);
let client = client.into_collection_client(collection_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -25,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// authorization token at later time if you need, for example, to escalate the privileges for a
// single operation.
// Here we are using reqwest but other clients are supported (check the documentation).
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

// The Cosmos' client exposes a lot of methods. This one lists the databases in the specified
Expand Down
4 changes: 2 additions & 2 deletions sdk/cosmos/examples/create_delete_database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use collection::*;
use futures::stream::StreamExt;
Expand Down Expand Up @@ -30,7 +29,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Once we have an authorization token you can create a client instance. You can change the
// authorization token at later time if you need, for example, to escalate the privileges for a
// single operation.
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(
http_client.clone(),
account.clone(),
Expand All @@ -45,6 +44,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
println!("list_databases_response = {:#?}", list_databases_response);

let cosmos_client = CosmosClient::with_pipeline(
http_client,
account,
authorization_token,
CosmosOptions::with_client(Arc::new(reqwest::Client::new())),
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/database_00.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use serde_json::Value;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -14,7 +12,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = permission::AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account, authorization_token);

let dbs = client.list_databases().execute().await?;
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/database_01.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -13,7 +11,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account, authorization_token);

let database_client = client.into_database_client("pollo");
Expand Down
4 changes: 2 additions & 2 deletions sdk/cosmos/examples/document_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use serde::{Deserialize, Serialize};
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos
// DB.
use azure_core::prelude::*;
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use azure_cosmos::resources::collection::*;
use azure_cosmos::responses::GetDocumentResponse;
Expand Down Expand Up @@ -50,7 +49,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

// Next we will create a Cosmos client. You need an authorization_token but you can later
// change it if needed.
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = new_http_client();
let client = CosmosClient::new(
http_client.clone(),
account.clone(),
Expand All @@ -70,6 +69,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.find(|db| db.id == DATABASE);

let database_client = CosmosClient::with_pipeline(
http_client,
account,
authorization_token,
CosmosOptions::with_client(Arc::new(reqwest::Client::new())),
Expand Down
3 changes: 1 addition & 2 deletions sdk/cosmos/examples/document_entries_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use futures::stream::StreamExt;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;
use std::sync::Arc;

// Now we create a sample struct. The Cow trick
// allows us to use the same struct for serializing
Expand Down Expand Up @@ -46,7 +45,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = permission::AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = new_http_client();
let client = CosmosClient::new(http_client, account, authorization_token);
let client = client.into_database_client(database_name);
let client = client.into_collection_client(collection_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/document_entries_01.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;
use std::sync::Arc;

#[derive(Serialize, Deserialize, Clone, Debug)]
struct MySampleStruct<'a> {
Expand Down Expand Up @@ -38,7 +36,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account, authorization_token);
let client = client.into_database_client(database_name);
let client = client.into_collection_client(collection_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/get_database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -17,7 +15,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let database_client = client.into_database_client(database_name.clone());
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/key_ranges_00.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -18,7 +16,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let client = client.into_database_client(database);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/permission_00.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -26,7 +24,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let database_client = client.into_database_client(database_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/query_document_00.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use azure_cosmos::responses::QueryDocumentsResponse;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::sync::Arc;

#[derive(Serialize, Deserialize, Debug)]
struct MySampleStructOwned {
Expand Down Expand Up @@ -39,7 +37,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let client = client.into_database_client(database_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/readme.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use serde::{Deserialize, Serialize};
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos.
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use futures::stream::StreamExt;
use std::borrow::Cow;
use std::error::Error;
use std::sync::Arc;

// This is the stuct we want to use in our sample.
// Make sure to have a collection with partition key "a_number" for this example to
Expand Down Expand Up @@ -55,7 +53,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

// Next we will create a Cosmos client.
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

// We know the database so we can obtain a database client.
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/remove_all_documents.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use futures::stream::StreamExt;
use serde_json::Value;
use std::error::Error;
use std::sync::Arc;

// This example expects you to have created a collection
// with partitionKey on "id".
Expand All @@ -26,7 +24,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

// Next we will create a Cosmos client.
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let client = client.into_database_client(database_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/stored_proc_00.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::HttpClient;
/// This sample showcases execution of stored procedure
/// Create stored procedure called test_proc, like so:
/// function f(personToGreet) {
Expand All @@ -8,7 +7,6 @@ use azure_core::HttpClient;
/// }
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -25,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let ret = client
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/stored_proc_01.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::HttpClient;
/// This sample showcases execution of stored procedure
/// Create stored procedure called test_proc, like so:
/// function f(personToGreet) {
Expand All @@ -8,7 +7,6 @@ use azure_core::HttpClient;
/// }
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand Down Expand Up @@ -36,7 +34,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let database_client = client.into_database_client(database_name);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/trigger_00.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use azure_cosmos::resources::trigger::{TriggerOperation, TriggerType};
use futures::stream::StreamExt;
use std::error::Error;
use std::sync::Arc;

const TRIGGER_BODY: &str = r#"
function updateMetadata() {
Expand Down Expand Up @@ -53,7 +51,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let database_client = client.into_database_client(database);
Expand Down
4 changes: 1 addition & 3 deletions sdk/cosmos/examples/user_00.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use azure_core::HttpClient;
use azure_cosmos::prelude::*;
use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Expand All @@ -20,7 +18,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;

let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
let http_client = azure_core::new_http_client();
let client = CosmosClient::new(http_client, account.clone(), authorization_token);

let database_client = client.into_database_client(database_name);
Expand Down
Loading

0 comments on commit 114df85

Please sign in to comment.