@@ -5,30 +5,37 @@ use crate::{
55 constants,
66 models:: { ContainerProperties , Item , QueryResults } ,
77 options:: { QueryOptions , ReadContainerOptions } ,
8- pipeline:: { CosmosPipeline , ResourceType } ,
9- utils :: AppendPathSegments ,
8+ pipeline:: CosmosPipeline ,
9+ resource_context :: { ResourceLink , ResourceType } ,
1010 DeleteContainerOptions , ItemOptions , PartitionKey , Query , QueryPartitionStrategy ,
1111} ;
1212
1313use azure_core:: { Context , Method , Pager , Request , Response } ;
1414use serde:: { de:: DeserializeOwned , Serialize } ;
15- use typespec_client_core:: http:: PagerResult ;
16- use url:: Url ;
1715
1816/// A client for working with a specific container in a Cosmos DB account.
1917///
2018/// You can get a `Container` by calling [`DatabaseClient::container_client()`](crate::clients::DatabaseClient::container_client()).
2119pub struct ContainerClient {
22- container_url : Url ,
20+ link : ResourceLink ,
21+ items_link : ResourceLink ,
2322 pipeline : CosmosPipeline ,
2423}
2524
2625impl ContainerClient {
27- pub ( crate ) fn new ( pipeline : CosmosPipeline , database_url : & Url , container_name : & str ) -> Self {
28- let container_url = database_url. with_path_segments ( [ "colls" , container_name] ) ;
26+ pub ( crate ) fn new (
27+ pipeline : CosmosPipeline ,
28+ database_link : & ResourceLink ,
29+ container_id : & str ,
30+ ) -> Self {
31+ let link = database_link
32+ . feed ( ResourceType :: Containers )
33+ . item ( container_id) ;
34+ let items_link = link. feed ( ResourceType :: Items ) ;
2935
3036 Self {
31- container_url,
37+ link,
38+ items_link,
3239 pipeline,
3340 }
3441 }
@@ -58,9 +65,10 @@ impl ContainerClient {
5865 // REASON: This is a documented public API so prefixing with '_' is undesirable.
5966 options : Option < ReadContainerOptions > ,
6067 ) -> azure_core:: Result < Response < ContainerProperties > > {
61- let mut req = Request :: new ( self . container_url . clone ( ) , Method :: Get ) ;
68+ let url = self . pipeline . url ( & self . link ) ;
69+ let mut req = Request :: new ( url, Method :: Get ) ;
6270 self . pipeline
63- . send ( Context :: new ( ) , & mut req, ResourceType :: Containers )
71+ . send ( Context :: new ( ) , & mut req, self . link . clone ( ) )
6472 . await
6573 }
6674
@@ -76,9 +84,10 @@ impl ContainerClient {
7684 // REASON: This is a documented public API so prefixing with '_' is undesirable.
7785 options : Option < DeleteContainerOptions > ,
7886 ) -> azure_core:: Result < Response > {
79- let mut req = Request :: new ( self . container_url . clone ( ) , Method :: Delete ) ;
87+ let url = self . pipeline . url ( & self . link ) ;
88+ let mut req = Request :: new ( url, Method :: Delete ) ;
8089 self . pipeline
81- . send ( Context :: new ( ) , & mut req, ResourceType :: Containers )
90+ . send ( Context :: new ( ) , & mut req, self . link . clone ( ) )
8291 . await
8392 }
8493
@@ -126,12 +135,12 @@ impl ContainerClient {
126135 // REASON: This is a documented public API so prefixing with '_' is undesirable.
127136 options : Option < ItemOptions > ,
128137 ) -> azure_core:: Result < Response < Item < T > > > {
129- let url = self . container_url . with_path_segments ( [ "docs" ] ) ;
138+ let url = self . pipeline . url ( & self . items_link ) ;
130139 let mut req = Request :: new ( url, Method :: Post ) ;
131140 req. insert_headers ( & partition_key. into ( ) ) ?;
132141 req. set_json ( & item) ?;
133142 self . pipeline
134- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
143+ . send ( Context :: new ( ) , & mut req, self . items_link . clone ( ) )
135144 . await
136145 }
137146
@@ -181,15 +190,12 @@ impl ContainerClient {
181190 // REASON: This is a documented public API so prefixing with '_' is undesirable.
182191 options : Option < ItemOptions > ,
183192 ) -> azure_core:: Result < Response < Item < T > > > {
184- let url = self
185- . container_url
186- . with_path_segments ( [ "docs" , item_id. as_ref ( ) ] ) ;
193+ let link = self . items_link . item ( item_id) ;
194+ let url = self . pipeline . url ( & link) ;
187195 let mut req = Request :: new ( url, Method :: Put ) ;
188196 req. insert_headers ( & partition_key. into ( ) ) ?;
189197 req. set_json ( & item) ?;
190- self . pipeline
191- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
192- . await
198+ self . pipeline . send ( Context :: new ( ) , & mut req, link) . await
193199 }
194200
195201 /// Creates or replaces an item in the container.
@@ -239,13 +245,13 @@ impl ContainerClient {
239245 // REASON: This is a documented public API so prefixing with '_' is undesirable.
240246 options : Option < ItemOptions > ,
241247 ) -> azure_core:: Result < Response < Item < T > > > {
242- let url = self . container_url . with_path_segments ( [ "docs" ] ) ;
248+ let url = self . pipeline . url ( & self . items_link ) ;
243249 let mut req = Request :: new ( url, Method :: Post ) ;
244250 req. insert_header ( constants:: IS_UPSERT , "true" ) ;
245251 req. insert_headers ( & partition_key. into ( ) ) ?;
246252 req. set_json ( & item) ?;
247253 self . pipeline
248- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
254+ . send ( Context :: new ( ) , & mut req, self . items_link . clone ( ) )
249255 . await
250256 }
251257
@@ -288,14 +294,11 @@ impl ContainerClient {
288294 // REASON: This is a documented public API so prefixing with '_' is undesirable.
289295 options : Option < ItemOptions > ,
290296 ) -> azure_core:: Result < Response < Item < T > > > {
291- let url = self
292- . container_url
293- . with_path_segments ( [ "docs" , item_id. as_ref ( ) ] ) ;
297+ let link = self . items_link . item ( item_id) ;
298+ let url = self . pipeline . url ( & link) ;
294299 let mut req = Request :: new ( url, Method :: Get ) ;
295300 req. insert_headers ( & partition_key. into ( ) ) ?;
296- self . pipeline
297- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
298- . await
301+ self . pipeline . send ( Context :: new ( ) , & mut req, link) . await
299302 }
300303
301304 /// Deletes an item from the container.
@@ -326,14 +329,11 @@ impl ContainerClient {
326329 // REASON: This is a documented public API so prefixing with '_' is undesirable.
327330 options : Option < ItemOptions > ,
328331 ) -> azure_core:: Result < Response > {
329- let url = self
330- . container_url
331- . with_path_segments ( [ "docs" , item_id. as_ref ( ) ] ) ;
332+ let link = self . items_link . item ( item_id) ;
333+ let url = self . pipeline . url ( & link) ;
332334 let mut req = Request :: new ( url, Method :: Delete ) ;
333335 req. insert_headers ( & partition_key. into ( ) ) ?;
334- self . pipeline
335- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
336- . await
336+ self . pipeline . send ( Context :: new ( ) , & mut req, link) . await
337337 }
338338
339339 /// Executes a single-partition query against items in the container.
@@ -399,40 +399,12 @@ impl ContainerClient {
399399 // REASON: This is a documented public API so prefixing with '_' is undesirable.
400400 options : Option < QueryOptions > ,
401401 ) -> azure_core:: Result < Pager < QueryResults < T > > > {
402- let mut url = self . container_url . clone ( ) ;
403- url. append_path_segments ( [ "docs" ] ) ;
404- let mut base_req = Request :: new ( url, Method :: Post ) ;
405-
406- base_req. insert_header ( constants:: QUERY , "True" ) ;
407- base_req. add_mandatory_header ( & constants:: QUERY_CONTENT_TYPE ) ;
408-
402+ let url = self . pipeline . url ( & self . items_link ) ;
403+ let mut base_request = Request :: new ( url, Method :: Post ) ;
409404 let QueryPartitionStrategy :: SinglePartition ( partition_key) = partition_key. into ( ) ;
410- base_req . insert_headers ( & partition_key) ?;
405+ base_request . insert_headers ( & partition_key) ?;
411406
412- base_req. set_json ( & query. into ( ) ) ?;
413-
414- // We have to double-clone here.
415- // First we clone the pipeline to pass it in to the closure
416- let pipeline = self . pipeline . clone ( ) ;
417- Ok ( Pager :: from_callback ( move |continuation| {
418- // Then we have to clone it again to pass it in to the async block.
419- // This is because Pageable can't borrow any data, it has to own it all.
420- // That's probably good, because it means a Pageable can outlive the client that produced it, but it requires some extra cloning.
421- let pipeline = pipeline. clone ( ) ;
422- let mut req = base_req. clone ( ) ;
423- async move {
424- if let Some ( continuation) = continuation {
425- req. insert_header ( constants:: CONTINUATION , continuation) ;
426- }
427-
428- let response = pipeline
429- . send ( Context :: new ( ) , & mut req, ResourceType :: Items )
430- . await ?;
431- Ok ( PagerResult :: from_response_header (
432- response,
433- & constants:: CONTINUATION ,
434- ) )
435- }
436- } ) )
407+ self . pipeline
408+ . send_query_request ( query. into ( ) , base_request, self . items_link . clone ( ) )
437409 }
438410}
0 commit comments