Skip to content

Commit 6945d23

Browse files
authored
Last into_future (#677)
* ExecuteStoredProcedure::into_future * GetPartitionKeyRanges::into_future * CreateOrReplaceAttachment::into_future * Cargo fmt * Fix tests
1 parent 9243927 commit 6945d23

28 files changed

+414
-723
lines changed

sdk/core/src/mock/mock_request.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ use std::str::FromStr;
88

99
const FIELDS: &[&str] = &["uri", "method", "headers", "body"];
1010

11-
impl Request {
12-
fn new(uri: Uri, method: Method, headers: HeaderMap, body: Body) -> Self {
13-
Self {
14-
uri,
15-
method,
16-
headers,
17-
body,
18-
}
19-
}
20-
}
21-
2211
impl<'de> Deserialize<'de> for Request {
2312
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2413
where
@@ -98,12 +87,12 @@ impl<'de> Visitor<'de> for RequestVisitor {
9887
);
9988
}
10089

101-
Ok(Self::Value::new(
102-
Uri::from_str(uri.1).expect("expected a valid uri"),
103-
Method::from_str(method.1).expect("expected a valid HTTP method"),
104-
hm,
105-
bytes::Bytes::from(body).into(),
106-
))
90+
Ok(Self::Value {
91+
uri: Uri::from_str(uri.1).expect("expected a valid uri"),
92+
method: Method::from_str(method.1).expect("expected a valid HTTP method"),
93+
headers: hm,
94+
body: bytes::Bytes::from(body).into(),
95+
})
10796
}
10897
}
10998

sdk/core/src/request.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ pub struct Request {
3636
}
3737

3838
impl Request {
39+
/// Create a new request with an empty body and no headers
40+
pub fn new(uri: Uri, method: Method) -> Self {
41+
Self {
42+
uri,
43+
method,
44+
headers: HeaderMap::default(),
45+
body: Body::Bytes(bytes::Bytes::new()),
46+
}
47+
}
48+
3949
pub fn uri(&self) -> &Uri {
4050
&self.uri
4151
}

sdk/data_cosmos/examples/attachments_00.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
7575
println!("creating");
7676
let attachment_client = document_client.clone().into_attachment_client("myref06");
7777
let resp = attachment_client
78-
.create_reference(
78+
.create_attachment(
7979
"https://cdn.pixabay.com/photo/2020/01/11/09/30/abstract-background-4756987__340.jpg",
8080
"image/jpeg",
8181
)
@@ -100,12 +100,12 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
100100
println!("replacing");
101101
let attachment_client = document_client.clone().into_attachment_client("myref06");
102102
let resp = attachment_client
103-
.replace_reference()
104-
.consistency_level(session_token)
105-
.execute(
103+
.replace_attachment(
106104
"https://Adn.pixabay.com/photo/2020/01/11/09/30/abstract-background-4756987__340.jpg",
107105
"image/jpeg",
108106
)
107+
.consistency_level(session_token)
108+
.into_future()
109109
.await?;
110110
println!("replace reference == {:#?}", resp);
111111

sdk/data_cosmos/examples/key_ranges_00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2525
let client = client.into_database_client(database);
2626
let client = client.into_collection_client(collection);
2727

28-
let resp = client.get_partition_key_ranges().execute().await?;
28+
let resp = client.get_partition_key_ranges().into_future().await?;
2929
println!("resp == {:#?}", resp);
3030

3131
Ok(())

sdk/data_cosmos/examples/stored_proc_00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3535
.into_stored_procedure_client("test_proc")
3636
.execute_stored_procedure()
3737
.parameters(["Robert"])
38-
.execute::<serde_json::Value>()
38+
.into_future::<serde_json::Value>()
3939
.await?;
4040

4141
println!("Response object:\n{:#?}", ret);

sdk/data_cosmos/examples/stored_proc_01.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
7070
let execute_stored_procedure_response = stored_procedure_client
7171
.execute_stored_procedure()
7272
.parameters(["Robert"])
73-
.execute::<serde_json::Value>()
73+
.into_future::<serde_json::Value>()
7474
.await?;
7575

7676
println!(

sdk/data_cosmos/src/clients/attachment_client.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use crate::operations::*;
2-
use crate::requests;
3-
use crate::resources::ResourceType;
42
use crate::ReadonlyString;
5-
use azure_core::HttpClient;
6-
use azure_core::Pipeline;
7-
use azure_core::Request;
3+
use azure_core::{Pipeline, Request};
84
use bytes::Bytes;
95

106
use super::*;
@@ -73,27 +69,35 @@ impl AttachmentClient {
7369
CreateOrReplaceSlugAttachmentBuilder::new(self.clone(), false, body)
7470
}
7571

76-
/// Initiate a request to create ant.
77-
pub fn create_reference<M, C>(
72+
/// Initiate a request to create a reference attachment.
73+
pub fn create_attachment<M, C>(
7874
&self,
7975
media: M,
8076
content_type: C,
81-
) -> CreateReferenceAttachmentBuilder
77+
) -> CreateOrReplaceAttachmentBuilder
8278
where
8379
M: Into<String>,
8480
C: Into<String>,
8581
{
86-
CreateReferenceAttachmentBuilder::new(self.clone(), media.into(), content_type.into())
82+
CreateOrReplaceAttachmentBuilder::new(self.clone(), true, media.into(), content_type.into())
8783
}
8884

8985
/// Initiate a request to replace an attachment.
90-
pub fn replace_reference(&self) -> requests::ReplaceReferenceAttachmentBuilder<'_, '_> {
91-
requests::ReplaceReferenceAttachmentBuilder::new(self)
92-
}
93-
94-
/// Get a raw [`HttpClient`].
95-
pub(crate) fn http_client(&self) -> &dyn HttpClient {
96-
self.cosmos_client().http_client()
86+
pub fn replace_attachment<M, C>(
87+
&self,
88+
media: M,
89+
content_type: C,
90+
) -> CreateOrReplaceAttachmentBuilder
91+
where
92+
M: Into<String>,
93+
C: Into<String>,
94+
{
95+
CreateOrReplaceAttachmentBuilder::new(
96+
self.clone(),
97+
false,
98+
media.into(),
99+
content_type.into(),
100+
)
97101
}
98102

99103
pub(crate) fn prepare_pipeline(&self, method: http::Method) -> Request {
@@ -108,23 +112,6 @@ impl AttachmentClient {
108112
)
109113
}
110114

111-
pub(crate) fn prepare_request_with_attachment_name(
112-
&self,
113-
method: http::Method,
114-
) -> http::request::Builder {
115-
self.cosmos_client().prepare_request(
116-
&format!(
117-
"dbs/{}/colls/{}/docs/{}/attachments/{}",
118-
self.database_client().database_name(),
119-
self.collection_client().collection_name(),
120-
self.document_client().document_name(),
121-
self.attachment_name()
122-
),
123-
method,
124-
ResourceType::Attachments,
125-
)
126-
}
127-
128115
pub(crate) fn prepare_pipeline_with_attachment_name(&self, method: http::Method) -> Request {
129116
self.cosmos_client().prepare_request_pipeline(
130117
&format!(

sdk/data_cosmos/src/clients/collection_client.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use super::{DatabaseClient, UserDefinedFunctionClient};
22
use crate::clients::*;
33
use crate::operations::*;
4-
use crate::requests;
54
use crate::resources::collection::PartitionKey;
65
use crate::resources::document::Query;
76
use crate::CosmosEntity;
87
use crate::ReadonlyString;
9-
use azure_core::{HttpClient, Pipeline, Request};
8+
use azure_core::{Pipeline, Request};
109
use serde::Serialize;
1110

1211
/// A client for Cosmos collection resources.
@@ -94,8 +93,8 @@ impl CollectionClient {
9493
}
9594

9695
/// list the partition key ranges in a collection
97-
pub fn get_partition_key_ranges(&self) -> requests::GetPartitionKeyRangesBuilder<'_, '_> {
98-
requests::GetPartitionKeyRangesBuilder::new(self)
96+
pub fn get_partition_key_ranges(&self) -> GetPartitionKeyRangesBuilder {
97+
GetPartitionKeyRangesBuilder::new(self.clone())
9998
}
10099

101100
/// convert into a [`DocumentClient`]
@@ -141,10 +140,6 @@ impl CollectionClient {
141140
.prepare_request_pipeline(path, http_method)
142141
}
143142

144-
pub(crate) fn http_client(&self) -> &dyn HttpClient {
145-
self.cosmos_client().http_client()
146-
}
147-
148143
pub(crate) fn pipeline(&self) -> &Pipeline {
149144
self.cosmos_client().pipeline()
150145
}

sdk/data_cosmos/src/clients/cosmos_client.rs

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
use super::DatabaseClient;
2-
use crate::authorization_policy::{generate_authorization, generate_resource_link};
3-
use crate::headers::*;
42
use crate::operations::*;
53
use crate::resources::permission::AuthorizationToken;
6-
use crate::resources::ResourceType;
7-
use crate::{ReadonlyString, TimeNonce};
4+
use crate::ReadonlyString;
85

9-
use azure_core::{ClientOptions, HttpClient, Pipeline, Request};
10-
use http::request::Builder as RequestBuilder;
11-
use http::{header, HeaderValue};
6+
use azure_core::{ClientOptions, Pipeline, Request};
127

138
use std::fmt::Debug;
149
use std::sync::Arc;
@@ -18,13 +13,10 @@ use std::sync::Arc;
1813
pub const EMULATOR_ACCOUNT_KEY: &str =
1914
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
2015

21-
const AZURE_VERSION: &str = "2018-12-31";
22-
2316
/// A plain Cosmos client.
2417
#[derive(Debug, Clone)]
2518
pub struct CosmosClient {
2619
pipeline: Pipeline,
27-
auth_token: AuthorizationToken,
2820
cloud_location: CloudLocation,
2921
}
3022

@@ -75,14 +67,9 @@ impl CosmosClient {
7567
/// Create a new `CosmosClient` which connects to the account's instance in the public Azure cloud.
7668
pub fn new(account: String, auth_token: AuthorizationToken, options: CosmosOptions) -> Self {
7769
let cloud_location = CloudLocation::Public(account);
78-
// TODO: The AuthorizationToken will only be stored in the pipeline via its policy.
79-
// Right now the AuthorizationToken is a field of the Client.
80-
// This will be corrected once every Cosmos function has been be migrated to the pipeline.
81-
// Once that happens, we will remove the clone below.
82-
let pipeline = new_pipeline_from_options(options, auth_token.clone());
70+
let pipeline = new_pipeline_from_options(options, auth_token);
8371
Self {
8472
pipeline,
85-
auth_token,
8673
cloud_location,
8774
}
8875
}
@@ -108,10 +95,9 @@ impl CosmosClient {
10895
options: CosmosOptions,
10996
) -> Self {
11097
let cloud_location = CloudLocation::China(account);
111-
let pipeline = new_pipeline_from_options(options, auth_token.clone());
98+
let pipeline = new_pipeline_from_options(options, auth_token);
11299
Self {
113100
pipeline,
114-
auth_token,
115101
cloud_location,
116102
}
117103
}
@@ -124,10 +110,9 @@ impl CosmosClient {
124110
options: CosmosOptions,
125111
) -> Self {
126112
let cloud_location = CloudLocation::Custom { account, uri };
127-
let pipeline = new_pipeline_from_options(options, auth_token.clone());
113+
let pipeline = new_pipeline_from_options(options, auth_token);
128114
Self {
129115
pipeline,
130-
auth_token,
131116
cloud_location,
132117
}
133118
}
@@ -140,19 +125,15 @@ impl CosmosClient {
140125
account: String::from("Custom"),
141126
uri,
142127
};
143-
let pipeline = new_pipeline_from_options(options, auth_token.clone());
128+
let pipeline = new_pipeline_from_options(options, auth_token);
144129
Self {
145130
pipeline,
146-
auth_token,
147131
cloud_location,
148132
}
149133
}
150134

151135
/// Set the auth token used
152136
pub fn auth_token(&mut self, auth_token: AuthorizationToken) {
153-
// TODO: To remove once everything uses the AutorizationPolicy
154-
self.auth_token = auth_token.clone();
155-
156137
// we replace the AuthorizationPolicy. This is
157138
// the last-1 policy by construction.
158139
let auth_policy: Arc<dyn azure_core::Policy> =
@@ -177,74 +158,23 @@ impl CosmosClient {
177158
DatabaseClient::new(self, database_name)
178159
}
179160

180-
/// Prepares an `http::RequestBuilder`.
181-
///
182-
/// TODO: Remove once all operations have been moved to pipeline architecture. This is used by
183-
/// legacy operations that have not moved to the use of the pipeline architecture. Once
184-
/// that is complete, this will be superceded by `prepare_request_pipeline`.
185-
pub(crate) fn prepare_request(
186-
&self,
187-
uri_path: &str,
188-
http_method: http::Method,
189-
resource_type: ResourceType,
190-
) -> RequestBuilder {
191-
let time = TimeNonce::default();
192-
193-
let auth = {
194-
let resource_link = generate_resource_link(uri_path);
195-
trace!(
196-
"resource_link generated by prepare_request == {}",
197-
resource_link
198-
);
199-
generate_authorization(
200-
&self.auth_token,
201-
&http_method,
202-
&resource_type,
203-
resource_link,
204-
time,
205-
)
206-
};
207-
trace!("prepare_request::auth == {:?}", auth);
208-
let uri = format!("{}/{}", self.cloud_location.url(), uri_path);
209-
debug!("building request. uri: {}", uri);
210-
211-
RequestBuilder::new()
212-
.method(http_method)
213-
.uri(uri)
214-
.header(HEADER_DATE, time.to_string())
215-
.header(HEADER_VERSION, HeaderValue::from_static(AZURE_VERSION))
216-
.header(header::AUTHORIZATION, auth)
217-
}
218-
219-
/// Prepares' an `azure_core::Request`. This function will
220-
/// add the cloud location to the URI suffix and generate
221-
/// a Request with the specified HTTP Method.
222-
/// It will also set the body to an empty Bytes instance.
223-
/// *Note*: This call does not handle authorization as
224-
/// it will be done by the `AuthorizationPolicy`.
161+
/// Prepares' an `azure_core::Request`.
225162
///
226-
/// Note: Eventually this method will replace `prepare_request` fully.
163+
/// This function will add the cloud location to the URI suffix and generate
164+
/// a Request with the specified HTTP Method. It will also set the body
165+
/// to an empty `Bytes` instance.
227166
pub(crate) fn prepare_request_pipeline(
228167
&self,
229168
uri_path: &str,
230169
http_method: http::Method,
231170
) -> Request {
232171
let uri = format!("{}/{}", self.cloud_location.url(), uri_path);
233-
RequestBuilder::new()
234-
.method(http_method)
235-
.uri(uri)
236-
.body(bytes::Bytes::new())
237-
.unwrap()
238-
.into()
172+
Request::new(uri.parse().unwrap(), http_method)
239173
}
240174

241175
pub(crate) fn pipeline(&self) -> &Pipeline {
242176
&self.pipeline
243177
}
244-
245-
pub(crate) fn http_client(&self) -> &dyn HttpClient {
246-
self.pipeline.http_client()
247-
}
248178
}
249179

250180
/// The cloud with which you want to interact.

0 commit comments

Comments
 (0)