@@ -10,13 +10,11 @@ use std::error::Error;
10
10
11
11
use tokio_core:: reactor:: Core ;
12
12
13
- use azure_sdk_for_rust:: azure:: cosmos:: authorization_token:: { AuthorizationToken , TokenType } ;
14
- use azure_sdk_for_rust:: azure:: cosmos:: client:: Client ;
15
- use azure_sdk_for_rust:: azure:: cosmos:: list_documents:: LIST_DOCUMENTS_OPTIONS_DEFAULT ;
16
- use azure_sdk_for_rust:: azure:: cosmos:: partition_key:: PartitionKey ;
17
- use azure_sdk_for_rust:: azure:: cosmos:: get_document:: GetDocumentOptions ;
18
- use azure_sdk_for_rust:: azure:: cosmos:: request_response:: { GetDocumentResponse ,
19
- ListDocumentsResponse } ;
13
+ use azure_sdk_for_rust:: cosmos:: {
14
+ AuthorizationToken , TokenType , Client ,
15
+ PartitionKey , GetDocumentOptions , CreateDocumentOptions , ReplaceDocumentOptions , ListDocumentsOptions , DeleteDocumentOptions ,
16
+ request_response:: { GetDocumentResponse , ListDocumentsResponse }
17
+ } ;
20
18
21
19
#[ macro_use]
22
20
extern crate serde_derive;
@@ -83,96 +81,131 @@ fn code() -> Result<(), Box<Error>> {
83
81
84
82
// let's add an entity. we ignore the errors at this point and just
85
83
// notify the user.
86
- match core. run ( client. create_document_as_entity (
84
+ core. run ( client. create_document (
87
85
& database_name,
88
86
& collection_name,
89
- false ,
90
- None ,
91
- & partition_key,
87
+ & CreateDocumentOptions { partition_key, ..Default :: default ( ) } ,
92
88
& doc,
93
- ) ) {
94
- Ok ( _) => {
95
- println ! ( "entity added" ) ;
96
- }
97
- Err ( error) => {
98
- println ! ( "entity add failed (maybe already there?) {:?}" , error) ;
99
- }
100
- } ;
89
+ ) ) . expect ( "entity creation failed" ) ;
101
90
}
91
+ // core.run(
92
+ // futures::future::join_all((0..5).map(|i| {
93
+ // let doc = MySampleStruct {
94
+ // id: &format!("unique_id{}", i),
95
+ // a_string: "Something here",
96
+ // a_number: i,
97
+ // a_timestamp: chrono::Utc::now().timestamp(),
98
+ // };
99
+
100
+ // let mut cdo = CreateDocumentOptions::default();
101
+ // cdo.partition_key.push(doc.id);
102
+
103
+ // // let's add an entity. we ignore the errors at this point and just
104
+ // // notify the user.
105
+ // client.create_document(
106
+ // &database_name,
107
+ // &collection_name,
108
+ // &cdo,
109
+ // &doc
110
+ // )
111
+ // }))
112
+ // ).unwrap();
113
+ println ! ( "Created 5 documents." ) ;
102
114
103
115
// let's get 3 entries at a time
104
- let mut ldo = LIST_DOCUMENTS_OPTIONS_DEFAULT . clone ( ) ;
105
- ldo. max_item_count = Some ( 3 ) ;
106
-
107
116
let response: ListDocumentsResponse < MySampleStructOwned > = core. run ( client. list_documents (
108
117
& database_name,
109
118
& collection_name,
110
- & ldo ,
119
+ & ListDocumentsOptions { max_item_count : Some ( 3 ) , .. Default :: default ( ) } ,
111
120
) ) . unwrap ( ) ;
112
121
113
122
assert_eq ! ( response. documents. len( ) , 3 ) ;
114
- println ! ( "response == {:?}" , response) ;
123
+ println ! ( "response == {:# ?}" , response) ;
115
124
116
125
// we inserted 5 documents and retrieved the first 3.
117
126
// continuation_token must be present
118
127
assert_eq ! (
119
128
response. additional_headers. continuation_token. is_some( ) ,
120
129
true
121
130
) ;
122
- if let Some ( ct) = response. additional_headers . continuation_token {
123
- println ! ( "ct == {}" , ct) ;
131
+
132
+ let ct = response. additional_headers . continuation_token . unwrap ( ) ;
133
+ println ! ( "ct == {}" , ct) ;
124
134
125
- let mut ldo = LIST_DOCUMENTS_OPTIONS_DEFAULT . clone ( ) ;
126
- ldo. continuation_token = Some ( & ct) ;
135
+ let response: ListDocumentsResponse < MySampleStructOwned > = core. run ( client. list_documents (
136
+ & database_name,
137
+ & collection_name,
138
+ & ListDocumentsOptions { continuation_token : Some ( & ct) , ..Default :: default ( ) } ,
139
+ ) ) . unwrap ( ) ;
127
140
128
- let response: ListDocumentsResponse < MySampleStructOwned > = core. run ( client. list_documents (
129
- & database_name,
130
- & collection_name,
131
- & ldo,
132
- ) ) . unwrap ( ) ;
141
+ assert_eq ! ( response. documents. len( ) , 2 ) ;
142
+ println ! ( "response == {:#?}" , response) ;
133
143
134
- assert_eq ! ( response. documents. len( ) , 47 ) ;
135
- println ! ( "response == {:?}" , response) ;
144
+ // we got the last 2 entries. Now continuation_token
145
+ // must be absent
146
+ assert_eq ! (
147
+ response. additional_headers. continuation_token. is_some( ) ,
148
+ false
149
+ ) ;
136
150
137
- // we got the last 47 entries. Now continuation_token
138
- // must be absent
139
- assert_eq ! (
140
- response. additional_headers. continuation_token. is_some( ) ,
141
- false
142
- ) ;
151
+ println ! ( "\n \n Looking for a specific item" ) ;
152
+ let id = format ! ( "unique_id{}" , 3 ) ;
153
+ let mut gdo = GetDocumentOptions :: default ( ) ;
154
+ gdo. partition_key . push ( & id) ;
143
155
156
+ let response: GetDocumentResponse < MySampleStructOwned > = core. run ( client. get_document (
157
+ & database_name,
158
+ & collection_name,
159
+ & id,
160
+ & gdo,
161
+ ) ) . unwrap ( ) ;
144
162
145
- println ! ( "\n \n Looking for a specific item" ) ;
146
- let id = format ! ( "unique_id{}" , 3 ) ;
147
- let mut gdo = GetDocumentOptions :: default ( ) ;
148
- gdo. partition_key . push ( & id) ;
163
+ assert_eq ! ( response. document. is_some( ) , true ) ;
164
+ println ! ( "response == {:#?}" , response) ;
165
+ let mut doc = response. document . unwrap ( ) ;
166
+ doc. entity . a_string = "Something else here" . into ( ) ;
167
+ let mut rdo = ReplaceDocumentOptions :: default ( ) ;
168
+ rdo. if_match = Some ( & doc. document_attributes . etag ) ; // use optimistic concurrency check
169
+ rdo. partition_key . push ( & id) ;
149
170
150
- let response : GetDocumentResponse < MySampleStructOwned > = core. run ( client. get_document (
151
- & database_name,
152
- & collection_name,
153
- & id ,
154
- & gdo ,
155
- ) ) . unwrap ( ) ;
171
+ let _response = core. run ( client. replace_document (
172
+ & database_name,
173
+ & collection_name,
174
+ & rdo ,
175
+ & doc
176
+ ) ) . unwrap ( ) ;
156
177
157
- assert_eq ! ( response. document. is_some( ) , true ) ;
158
- println ! ( "response == {:?}" , response) ;
178
+ // This id should not be found. We expect None as result
179
+ println ! ( "\n \n Looking for non-existing item" ) ;
180
+ let id = format ! ( "unique_id{}" , 100 ) ;
181
+ let mut gdo = GetDocumentOptions :: default ( ) ;
182
+ gdo. partition_key . push ( & id) ;
159
183
160
- // This id should not be found. We expect None as result
161
- println ! ( "\n \n Looking for non-existing item" ) ;
162
- let id = format ! ( "unique_id{}" , 100 ) ;
163
- let mut gdo = GetDocumentOptions :: default ( ) ;
164
- gdo. partition_key . push ( & id) ;
184
+ let response: GetDocumentResponse < MySampleStructOwned > = core. run ( client. get_document (
185
+ & database_name,
186
+ & collection_name,
187
+ & id,
188
+ & gdo,
189
+ ) ) . unwrap ( ) ;
165
190
166
- let response: GetDocumentResponse < MySampleStructOwned > = core. run ( client. get_document (
167
- & database_name,
168
- & collection_name,
169
- & id,
170
- & gdo,
171
- ) ) . unwrap ( ) ;
191
+ assert_eq ! ( response. document. is_some( ) , false ) ;
192
+ println ! ( "response == {:#?}" , response) ;
172
193
173
- assert_eq ! ( response. document. is_some( ) , false ) ;
174
- println ! ( "response == {:?}" , response) ;
194
+ for i in 0 ..5 {
195
+ let id = format ! ( "unique_id{}" , i) ;
196
+ let mut ddo = DeleteDocumentOptions :: default ( ) ;
197
+ ddo. partition_key . push ( & id) ;
198
+ core. run (
199
+ client. delete_document (
200
+ & database_name,
201
+ & collection_name,
202
+ & id,
203
+ & ddo
204
+ )
205
+ ) . unwrap ( ) ;
175
206
}
176
207
208
+ println ! ( "Cleaned up" ) ;
209
+
177
210
Ok ( ( ) )
178
211
}
0 commit comments