11use super :: DatabaseClient ;
2- use crate :: authorization_policy:: { generate_authorization, generate_resource_link} ;
3- use crate :: headers:: * ;
42use crate :: operations:: * ;
53use 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
138use std:: fmt:: Debug ;
149use std:: sync:: Arc ;
@@ -18,13 +13,10 @@ use std::sync::Arc;
1813pub 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 ) ]
2518pub 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