Skip to content

add HttpClientArc & set user-agent #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions sdk/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hyper-rustls = "0.21"
failure = "0.1"
async-trait = "0.1.36"
oauth2 = { version = "4.0.0-alpha.2" }
reqwest = { version = "0.10", features = ["json"] }

[dev-dependencies]
tokio = "0.2"
Expand Down
22 changes: 22 additions & 0 deletions sdk/core/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::sync::Arc;

#[derive(Clone)]
pub struct HttpClientArc(Arc<reqwest::Client>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we gain too much from this new type. What's the thought behind a new type instead of using Arc<Client> directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. It has some value. It gives us HttpClientArc::default(), which should still work when we change the internals to be HttpClient interface. I think it also communicates how the library should be used.


impl HttpClientArc {
pub fn new(http_client: reqwest::Client) -> HttpClientArc {
HttpClientArc(Arc::new(http_client))
}
}

impl AsRef<reqwest::Client> for HttpClientArc {
fn as_ref(&self) -> &reqwest::Client {
self.0.as_ref()
}
}

impl Default for HttpClientArc {
fn default() -> Self {
Self::new(reqwest::Client::new())
}
}
19 changes: 18 additions & 1 deletion sdk/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate quick_error;
#[macro_use]
extern crate serde_derive;

mod http_client;
#[macro_use]
pub mod errors;
pub mod parsing;
Expand All @@ -25,6 +26,7 @@ pub mod util;

use errors::AzureError;
use headers::*;
pub use http_client::*;
use lease::LeaseId;
use modify_conditions::{IfMatchCondition, IfSinceCondition, SequenceNumberCondition};
pub use stored_access_policy::{StoredAccessPolicy, StoredAccessPolicyList};
Expand All @@ -39,8 +41,8 @@ use hyper::header::{
use oauth2::AccessToken;
use uuid::Uuid;

use std::collections::HashMap;
use std::fmt::Debug;
use std::{collections::HashMap, sync::Arc};

pub type RequestId = Uuid;
pub type SessionToken = String;
Expand Down Expand Up @@ -68,6 +70,21 @@ pub trait TokenCredential {
async fn get_token(&self, resource: &str) -> Result<TokenResponse, AzureError>;
}

#[derive(Clone)]
pub struct TokenCredentialArc(Arc<Box<dyn TokenCredential>>);

impl TokenCredentialArc {
pub fn new(token_credential: Box<dyn TokenCredential>) -> TokenCredentialArc {
TokenCredentialArc(Arc::new(token_credential))
}
}

impl AsRef<Box<dyn TokenCredential>> for TokenCredentialArc {
fn as_ref(&self) -> &Box<dyn TokenCredential> {
self.0.as_ref()
}
}

#[macro_export]
macro_rules! response_from_headers {
($cn:ident, $($fh:path => $na:ident: $typ:ty),+) => {
Expand Down
75 changes: 59 additions & 16 deletions services/mgmt/addons/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![doc = "generated by AutoRust 0.1.0"]
pub const USER_AGENT_SDK: &str = "azsdk-rust-mgmt_addons/0.1.0";
#[cfg(feature = "package-2018-03")]
mod package_2018_03;
#[cfg(feature = "package-2018-03")]
Expand All @@ -7,29 +8,71 @@ pub use package_2018_03::{models, operations, API_VERSION};
mod package_2017_05;
#[cfg(feature = "package-2017-05")]
pub use package_2017_05::{models, operations, API_VERSION};
#[derive(Clone)]
pub struct OperationConfig {
pub api_version: String,
pub client: reqwest::Client,
pub base_path: String,
pub token_credential: Option<Box<dyn azure_core::TokenCredential>>,
pub token_credential_resource: String,
http_client: azure_core::HttpClientArc,
token_credential: azure_core::TokenCredentialArc,
token_credential_resource: String,
base_path: String,
api_version: String,
user_agent: Option<String>,
}
impl OperationConfig {
pub fn new(token_credential: Box<dyn azure_core::TokenCredential>) -> Self {
pub fn new(http_client: azure_core::HttpClientArc, token_credential: azure_core::TokenCredentialArc) -> Self {
Self {
token_credential: Some(token_credential),
..Default::default()
http_client,
token_credential,
token_credential_resource: "https://management.azure.com/".to_owned(),
base_path: "https://management.azure.com".to_owned(),
api_version: API_VERSION.to_owned(),
user_agent: Some(format!("{} ({})", USER_AGENT_SDK, std::env::consts::ARCH)),
}
}
}
impl Default for OperationConfig {
fn default() -> Self {
pub fn new_all(
http_client: azure_core::HttpClientArc,
token_credential: azure_core::TokenCredentialArc,
token_credential_resource: String,
base_path: String,
api_version: String,
user_agent: Option<String>,
) -> Self {
Self {
api_version: API_VERSION.to_owned(),
client: reqwest::Client::new(),
base_path: "https://management.azure.com".to_owned(),
token_credential: None,
token_credential_resource: "https://management.azure.com/".to_owned(),
http_client,
token_credential,
token_credential_resource,
base_path,
api_version,
user_agent,
}
}
pub fn http_client(&self) -> &reqwest::Client {
self.http_client.as_ref()
}
pub fn token_credential(&self) -> &dyn azure_core::TokenCredential {
self.token_credential.as_ref().as_ref()
}
pub fn set_token_credential_resource(&mut self, token_credential_resource: String) {
self.token_credential_resource = token_credential_resource;
}
pub fn token_credential_resource(&self) -> &str {
&self.token_credential_resource
}
pub fn set_base_path(&mut self, base_path: String) {
self.base_path = base_path;
}
pub fn base_path(&self) -> &str {
&self.base_path
}
pub fn set_api_version(&mut self, api_version: String) {
self.api_version = api_version;
}
pub fn api_version(&self) -> &str {
&self.api_version
}
pub fn user_agent(&self) -> Option<&str> {
self.user_agent.as_deref()
}
pub fn set_user_agent(&mut self, user_agent: Option<String>) {
self.user_agent = user_agent;
}
}
111 changes: 66 additions & 45 deletions services/mgmt/addons/src/package_2017_05/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ pub mod operations {
use reqwest::StatusCode;
use snafu::{ResultExt, Snafu};
pub async fn list(operation_config: &crate::OperationConfig) -> std::result::Result<OperationList, list::Error> {
let client = &operation_config.client;
let uri_str = &format!("{}/providers/Microsoft.Addons/operations", &operation_config.base_path,);
let client = operation_config.http_client();
let uri_str = &format!("{}/providers/Microsoft.Addons/operations", operation_config.base_path(),);
let mut req_builder = client.get(uri_str);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(list::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
if let Some(user_agent) = operation_config.user_agent() {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent);
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let token_response = operation_config
.token_credential()
.get_token(operation_config.token_credential_resource())
.await
.context(list::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
req_builder = req_builder.query(&[("api-version", operation_config.api_version())]);
let req = req_builder.build().context(list::BuildRequestError)?;
let rsp = client.execute(req).await.context(list::ExecuteRequestError)?;
match rsp.status() {
Expand Down Expand Up @@ -80,20 +82,25 @@ pub mod support_plan_types {
provider_name: &str,
plan_type_name: &str,
) -> std::result::Result<CanonicalSupportPlanResponseEnvelope, get::Error> {
let client = &operation_config.client;
let client = operation_config.http_client();
let uri_str = &format!(
"{}/subscriptions/{}/providers/Microsoft.Addons/supportProviders/{}/supportPlanTypes/{}",
&operation_config.base_path, subscription_id, provider_name, plan_type_name
operation_config.base_path(),
subscription_id,
provider_name,
plan_type_name
);
let mut req_builder = client.get(uri_str);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
if let Some(user_agent) = operation_config.user_agent() {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent);
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let token_response = operation_config
.token_credential()
.get_token(operation_config.token_credential_resource())
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
req_builder = req_builder.query(&[("api-version", operation_config.api_version())]);
let req = req_builder.build().context(get::BuildRequestError)?;
let rsp = client.execute(req).await.context(get::ExecuteRequestError)?;
match rsp.status() {
Expand Down Expand Up @@ -151,20 +158,25 @@ pub mod support_plan_types {
provider_name: &str,
plan_type_name: &str,
) -> std::result::Result<create_or_update::Response, create_or_update::Error> {
let client = &operation_config.client;
let client = operation_config.http_client();
let uri_str = &format!(
"{}/subscriptions/{}/providers/Microsoft.Addons/supportProviders/{}/supportPlanTypes/{}",
&operation_config.base_path, subscription_id, provider_name, plan_type_name
operation_config.base_path(),
subscription_id,
provider_name,
plan_type_name
);
let mut req_builder = client.put(uri_str);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(create_or_update::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
if let Some(user_agent) = operation_config.user_agent() {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent);
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let token_response = operation_config
.token_credential()
.get_token(operation_config.token_credential_resource())
.await
.context(create_or_update::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
req_builder = req_builder.query(&[("api-version", operation_config.api_version())]);
let req = req_builder.build().context(create_or_update::BuildRequestError)?;
let rsp = client.execute(req).await.context(create_or_update::ExecuteRequestError)?;
match rsp.status() {
Expand Down Expand Up @@ -233,20 +245,25 @@ pub mod support_plan_types {
provider_name: &str,
plan_type_name: &str,
) -> std::result::Result<delete::Response, delete::Error> {
let client = &operation_config.client;
let client = operation_config.http_client();
let uri_str = &format!(
"{}/subscriptions/{}/providers/Microsoft.Addons/supportProviders/{}/supportPlanTypes/{}",
&operation_config.base_path, subscription_id, provider_name, plan_type_name
operation_config.base_path(),
subscription_id,
provider_name,
plan_type_name
);
let mut req_builder = client.delete(uri_str);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(delete::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
if let Some(user_agent) = operation_config.user_agent() {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent);
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let token_response = operation_config
.token_credential()
.get_token(operation_config.token_credential_resource())
.await
.context(delete::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
req_builder = req_builder.query(&[("api-version", operation_config.api_version())]);
let req = req_builder.build().context(delete::BuildRequestError)?;
let rsp = client.execute(req).await.context(delete::ExecuteRequestError)?;
match rsp.status() {
Expand Down Expand Up @@ -312,20 +329,24 @@ pub mod canonical_support_plan_types {
subscription_id: &str,
provider_name: &str,
) -> std::result::Result<CanonicalSupportPlanStatus, get::Error> {
let client = &operation_config.client;
let client = operation_config.http_client();
let uri_str = &format!(
"{}/subscriptions/{}/providers/Microsoft.Addons/supportProviders/{}/supportPlanTypes",
&operation_config.base_path, subscription_id, provider_name
operation_config.base_path(),
subscription_id,
provider_name
);
let mut req_builder = client.get(uri_str);
if let Some(token_credential) = &operation_config.token_credential {
let token_response = token_credential
.get_token(&operation_config.token_credential_resource)
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
if let Some(user_agent) = operation_config.user_agent() {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent);
}
req_builder = req_builder.query(&[("api-version", &operation_config.api_version)]);
let token_response = operation_config
.token_credential()
.get_token(operation_config.token_credential_resource())
.await
.context(get::GetTokenError)?;
req_builder = req_builder.bearer_auth(token_response.token.secret());
req_builder = req_builder.query(&[("api-version", operation_config.api_version())]);
let req = req_builder.build().context(get::BuildRequestError)?;
let rsp = client.execute(req).await.context(get::ExecuteRequestError)?;
match rsp.status() {
Expand Down
Loading