11use super :: { AttachmentClient , CollectionClient , CosmosClient , DatabaseClient } ;
2- use crate :: prelude :: { GetDocumentOptions , GetDocumentResponse } ;
2+ use crate :: operations :: * ;
33use crate :: resources:: ResourceType ;
44use crate :: { requests, ReadonlyString } ;
55use azure_core:: { Context , HttpClient , PipelineContext , Request } ;
@@ -16,10 +16,11 @@ pub struct DocumentClient {
1616
1717impl DocumentClient {
1818 /// This function creates a new instance of a DocumentClient. A document is identified by its
19- /// primary key and its partition key. Partition key is eagerly evaluated: the json
20- /// representation is generated as soon as you call the `new` function. This avoids doing the
21- /// serialization over and over, saving time. It also releases the borrow since the serialized
22- /// string is owned by the DocumentClient.
19+ /// primary key and its partition key.
20+ ///
21+ /// Partition key is eagerly evaluated: the json representation is generated as soon as you
22+ /// call the `new` function. This avoids doing the serialization over and over, saving time.
23+ /// It also releases the borrow since the serialized string is owned by the `DocumentClient`.
2324 pub ( crate ) fn new < S : Into < String > , PK : Serialize > (
2425 collection_client : CollectionClient ,
2526 document_name : S ,
@@ -57,11 +58,6 @@ impl DocumentClient {
5758 & self . partition_key_serialized
5859 }
5960
60- /// replace a document in a collection
61- pub fn replace_document < ' a > ( & ' a self ) -> requests:: ReplaceDocumentBuilder < ' a , ' _ > {
62- requests:: ReplaceDocumentBuilder :: new ( self )
63- }
64-
6561 /// Get a document
6662 pub async fn get_document < T > (
6763 & self ,
@@ -72,7 +68,7 @@ impl DocumentClient {
7268 T : DeserializeOwned ,
7369 {
7470 let mut request = self . prepare_request_pipeline_with_document_name ( http:: Method :: GET ) ;
75- let mut pipeline_context = PipelineContext :: new ( ctx, ResourceType :: Databases . into ( ) ) ;
71+ let mut pipeline_context = PipelineContext :: new ( ctx, ResourceType :: Documents . into ( ) ) ;
7672
7773 options. decorate_request ( & mut request) ?;
7874
@@ -85,9 +81,45 @@ impl DocumentClient {
8581 GetDocumentResponse :: try_from ( response) . await
8682 }
8783
84+ /// replace a document in a collection
85+ pub async fn replace_document < T : Serialize > (
86+ & self ,
87+ ctx : Context ,
88+ document : & T ,
89+ options : ReplaceDocumentOptions < ' _ > ,
90+ ) -> crate :: Result < ReplaceDocumentResponse > {
91+ let mut request = self . prepare_request_pipeline_with_document_name ( http:: Method :: PUT ) ;
92+ let mut pipeline_context = PipelineContext :: new ( ctx, ResourceType :: Documents . into ( ) ) ;
93+
94+ options. decorate_request ( & mut request, document, self . partition_key_serialized ( ) ) ?;
95+
96+ let response = self
97+ . cosmos_client ( )
98+ . pipeline ( )
99+ . send ( & mut pipeline_context, & mut request)
100+ . await ?;
101+
102+ ReplaceDocumentResponse :: try_from ( response) . await
103+ }
104+
88105 /// Delete a document
89- pub fn delete_document ( & self ) -> requests:: DeleteDocumentBuilder < ' _ > {
90- requests:: DeleteDocumentBuilder :: new ( self )
106+ pub async fn delete_document (
107+ & self ,
108+ ctx : Context ,
109+ options : DeleteDocumentOptions < ' _ > ,
110+ ) -> crate :: Result < DeleteDocumentResponse > {
111+ let mut request = self . prepare_request_pipeline_with_document_name ( http:: Method :: DELETE ) ;
112+ let mut pipeline_context = PipelineContext :: new ( ctx, ResourceType :: Documents . into ( ) ) ;
113+
114+ options. decorate_request ( & mut request, self . partition_key_serialized ( ) ) ?;
115+
116+ let response = self
117+ . cosmos_client ( )
118+ . pipeline ( )
119+ . send ( & mut pipeline_context, & mut request)
120+ . await ?;
121+
122+ DeleteDocumentResponse :: try_from ( response) . await
91123 }
92124
93125 /// List all attachments for a document
@@ -103,22 +135,6 @@ impl DocumentClient {
103135 AttachmentClient :: new ( self , attachment_name)
104136 }
105137
106- pub ( crate ) fn prepare_request_with_document_name (
107- & self ,
108- method : http:: Method ,
109- ) -> http:: request:: Builder {
110- self . cosmos_client ( ) . prepare_request (
111- & format ! (
112- "dbs/{}/colls/{}/docs/{}" ,
113- self . database_client( ) . database_name( ) ,
114- self . collection_client( ) . collection_name( ) ,
115- self . document_name( )
116- ) ,
117- method,
118- ResourceType :: Documents ,
119- )
120- }
121-
122138 fn prepare_request_pipeline_with_document_name ( & self , method : http:: Method ) -> Request {
123139 self . cosmos_client ( ) . prepare_request_pipeline (
124140 & format ! (
0 commit comments