-
Notifications
You must be signed in to change notification settings - Fork 283
Last into_future
#677
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
Last into_future
#677
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,9 @@ | ||
use super::DatabaseClient; | ||
use crate::authorization_policy::{generate_authorization, generate_resource_link}; | ||
use crate::headers::*; | ||
use crate::operations::*; | ||
use crate::resources::permission::AuthorizationToken; | ||
use crate::resources::ResourceType; | ||
use crate::{ReadonlyString, TimeNonce}; | ||
use crate::ReadonlyString; | ||
|
||
use azure_core::{ClientOptions, HttpClient, Pipeline, Request}; | ||
use http::request::Builder as RequestBuilder; | ||
use http::{header, HeaderValue}; | ||
use azure_core::{ClientOptions, Pipeline, Request}; | ||
|
||
use std::fmt::Debug; | ||
use std::sync::Arc; | ||
|
@@ -18,13 +13,10 @@ use std::sync::Arc; | |
pub const EMULATOR_ACCOUNT_KEY: &str = | ||
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | ||
|
||
const AZURE_VERSION: &str = "2018-12-31"; | ||
|
||
Comment on lines
-21
to
-22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did this go? Has this been replaced by something else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done in the |
||
/// A plain Cosmos client. | ||
#[derive(Debug, Clone)] | ||
pub struct CosmosClient { | ||
pipeline: Pipeline, | ||
auth_token: AuthorizationToken, | ||
cloud_location: CloudLocation, | ||
} | ||
|
||
|
@@ -75,14 +67,9 @@ impl CosmosClient { | |
/// Create a new `CosmosClient` which connects to the account's instance in the public Azure cloud. | ||
pub fn new(account: String, auth_token: AuthorizationToken, options: CosmosOptions) -> Self { | ||
let cloud_location = CloudLocation::Public(account); | ||
// TODO: The AuthorizationToken will only be stored in the pipeline via its policy. | ||
// Right now the AuthorizationToken is a field of the Client. | ||
// This will be corrected once every Cosmos function has been be migrated to the pipeline. | ||
// Once that happens, we will remove the clone below. | ||
let pipeline = new_pipeline_from_options(options, auth_token.clone()); | ||
let pipeline = new_pipeline_from_options(options, auth_token); | ||
Self { | ||
pipeline, | ||
auth_token, | ||
cloud_location, | ||
} | ||
} | ||
|
@@ -108,10 +95,9 @@ impl CosmosClient { | |
options: CosmosOptions, | ||
) -> Self { | ||
let cloud_location = CloudLocation::China(account); | ||
let pipeline = new_pipeline_from_options(options, auth_token.clone()); | ||
let pipeline = new_pipeline_from_options(options, auth_token); | ||
Self { | ||
pipeline, | ||
auth_token, | ||
cloud_location, | ||
} | ||
} | ||
|
@@ -124,10 +110,9 @@ impl CosmosClient { | |
options: CosmosOptions, | ||
) -> Self { | ||
let cloud_location = CloudLocation::Custom { account, uri }; | ||
let pipeline = new_pipeline_from_options(options, auth_token.clone()); | ||
let pipeline = new_pipeline_from_options(options, auth_token); | ||
Self { | ||
pipeline, | ||
auth_token, | ||
cloud_location, | ||
} | ||
} | ||
|
@@ -140,19 +125,15 @@ impl CosmosClient { | |
account: String::from("Custom"), | ||
uri, | ||
}; | ||
let pipeline = new_pipeline_from_options(options, auth_token.clone()); | ||
let pipeline = new_pipeline_from_options(options, auth_token); | ||
Self { | ||
pipeline, | ||
auth_token, | ||
cloud_location, | ||
} | ||
} | ||
|
||
/// Set the auth token used | ||
pub fn auth_token(&mut self, auth_token: AuthorizationToken) { | ||
// TODO: To remove once everything uses the AutorizationPolicy | ||
self.auth_token = auth_token.clone(); | ||
|
||
// we replace the AuthorizationPolicy. This is | ||
// the last-1 policy by construction. | ||
let auth_policy: Arc<dyn azure_core::Policy> = | ||
|
@@ -177,74 +158,23 @@ impl CosmosClient { | |
DatabaseClient::new(self, database_name) | ||
} | ||
|
||
/// Prepares an `http::RequestBuilder`. | ||
/// | ||
/// TODO: Remove once all operations have been moved to pipeline architecture. This is used by | ||
/// legacy operations that have not moved to the use of the pipeline architecture. Once | ||
/// that is complete, this will be superceded by `prepare_request_pipeline`. | ||
pub(crate) fn prepare_request( | ||
&self, | ||
uri_path: &str, | ||
http_method: http::Method, | ||
resource_type: ResourceType, | ||
) -> RequestBuilder { | ||
let time = TimeNonce::default(); | ||
|
||
let auth = { | ||
let resource_link = generate_resource_link(uri_path); | ||
trace!( | ||
"resource_link generated by prepare_request == {}", | ||
resource_link | ||
); | ||
generate_authorization( | ||
&self.auth_token, | ||
&http_method, | ||
&resource_type, | ||
resource_link, | ||
time, | ||
) | ||
}; | ||
trace!("prepare_request::auth == {:?}", auth); | ||
let uri = format!("{}/{}", self.cloud_location.url(), uri_path); | ||
debug!("building request. uri: {}", uri); | ||
|
||
RequestBuilder::new() | ||
.method(http_method) | ||
.uri(uri) | ||
.header(HEADER_DATE, time.to_string()) | ||
.header(HEADER_VERSION, HeaderValue::from_static(AZURE_VERSION)) | ||
.header(header::AUTHORIZATION, auth) | ||
} | ||
|
||
/// Prepares' an `azure_core::Request`. This function will | ||
/// add the cloud location to the URI suffix and generate | ||
/// a Request with the specified HTTP Method. | ||
/// It will also set the body to an empty Bytes instance. | ||
/// *Note*: This call does not handle authorization as | ||
/// it will be done by the `AuthorizationPolicy`. | ||
/// Prepares' an `azure_core::Request`. | ||
/// | ||
/// Note: Eventually this method will replace `prepare_request` fully. | ||
/// This function will add the cloud location to the URI suffix and generate | ||
/// a Request with the specified HTTP Method. It will also set the body | ||
/// to an empty `Bytes` instance. | ||
pub(crate) fn prepare_request_pipeline( | ||
&self, | ||
uri_path: &str, | ||
http_method: http::Method, | ||
) -> Request { | ||
let uri = format!("{}/{}", self.cloud_location.url(), uri_path); | ||
RequestBuilder::new() | ||
.method(http_method) | ||
.uri(uri) | ||
.body(bytes::Bytes::new()) | ||
.unwrap() | ||
.into() | ||
Request::new(uri.parse().unwrap(), http_method) | ||
} | ||
|
||
pub(crate) fn pipeline(&self) -> &Pipeline { | ||
&self.pipeline | ||
} | ||
|
||
pub(crate) fn http_client(&self) -> &dyn HttpClient { | ||
self.pipeline.http_client() | ||
} | ||
} | ||
|
||
/// The cloud with which you want to interact. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it took me a sec to realize this had just been moved to
core/src/request.rs
. That seems good.