@@ -2,24 +2,34 @@ use crate::headers::from_headers::*;
2
2
use crate :: prelude:: * ;
3
3
use crate :: resources:: collection:: { IndexingPolicy , PartitionKey } ;
4
4
use azure_core:: headers:: { etag_from_headers, session_token_from_headers} ;
5
- use azure_core:: { collect_pinned_stream, Request as HttpRequest , Response as HttpResponse } ;
5
+ use azure_core:: { collect_pinned_stream, Context , Response as HttpResponse } ;
6
6
use chrono:: { DateTime , Utc } ;
7
7
8
8
#[ derive( Debug , Clone ) ]
9
- pub struct CreateCollectionOptions {
9
+ pub struct CreateCollectionBuilder {
10
+ client : DatabaseClient ,
10
11
partition_key : PartitionKey ,
11
12
consistency_level : Option < ConsistencyLevel > ,
12
13
indexing_policy : Option < IndexingPolicy > ,
14
+ collection_name : String ,
13
15
offer : Option < Offer > ,
16
+ context : Context ,
14
17
}
15
18
16
- impl CreateCollectionOptions {
17
- pub fn new < P : Into < PartitionKey > > ( partition_key : P ) -> Self {
19
+ impl CreateCollectionBuilder {
20
+ pub ( crate ) fn new (
21
+ client : DatabaseClient ,
22
+ collection_name : String ,
23
+ partition_key : PartitionKey ,
24
+ ) -> Self {
18
25
Self {
19
- partition_key : partition_key. into ( ) ,
26
+ client,
27
+ collection_name,
28
+ partition_key,
20
29
consistency_level : None ,
21
30
indexing_policy : None ,
22
31
offer : None ,
32
+ context : Context :: new ( ) ,
23
33
}
24
34
}
25
35
@@ -29,25 +39,49 @@ impl CreateCollectionOptions {
29
39
offer: Offer => Some ( offer) ,
30
40
}
31
41
32
- pub ( crate ) fn decorate_request (
33
- & self ,
34
- request : & mut HttpRequest ,
35
- collection_name : & str ,
36
- ) -> crate :: Result < ( ) > {
37
- azure_core:: headers:: add_optional_header2 ( & self . offer , request) ?;
38
- azure_core:: headers:: add_optional_header2 ( & self . consistency_level , request) ?;
42
+ pub fn into_future ( self ) -> CreateCollection {
43
+ Box :: pin ( async move {
44
+ let mut request = self . client . cosmos_client ( ) . prepare_request_pipeline (
45
+ & format ! ( "dbs/{}/colls" , self . client. database_name( ) ) ,
46
+ http:: Method :: POST ,
47
+ ) ;
48
+ azure_core:: headers:: add_optional_header2 ( & self . offer , & mut request) ?;
49
+ azure_core:: headers:: add_optional_header2 ( & self . consistency_level , & mut request) ?;
39
50
40
- let collection = CreateCollectionBody {
41
- id : collection_name,
42
- indexing_policy : & self . indexing_policy ,
43
- partition_key : & self . partition_key ,
44
- } ;
51
+ let collection = CreateCollectionBody {
52
+ id : & self . collection_name ,
53
+ indexing_policy : & self . indexing_policy ,
54
+ partition_key : & self . partition_key ,
55
+ } ;
45
56
46
- request. set_body ( bytes:: Bytes :: from ( serde_json:: to_string ( & collection) ?) . into ( ) ) ;
47
- Ok ( ( ) )
57
+ request. set_body ( bytes:: Bytes :: from ( serde_json:: to_string ( & collection) ?) . into ( ) ) ;
58
+
59
+ let response = self
60
+ . client
61
+ . pipeline ( )
62
+ . send (
63
+ self . context . clone ( ) . insert ( ResourceType :: Collections ) ,
64
+ & mut request,
65
+ )
66
+ . await ?;
67
+
68
+ Ok ( CreateCollectionResponse :: try_from ( response) . await ?)
69
+ } )
48
70
}
49
71
}
50
72
73
+ #[ cfg( feature = "into_future" ) ]
74
+ impl std:: future:: IntoFuture for CreateCollectionBuilder {
75
+ type Future = CreateCollection ;
76
+ type Output = <CreateCollection as std:: future:: Future >:: Output ;
77
+ fn into_future ( self ) -> Self :: Future {
78
+ Self :: into_future ( self )
79
+ }
80
+ }
81
+
82
+ type CreateCollection =
83
+ futures:: future:: BoxFuture < ' static , crate :: Result < CreateCollectionResponse > > ;
84
+
51
85
/// Body for the create collection request
52
86
#[ derive( Serialize , Debug ) ]
53
87
struct CreateCollectionBody < ' a > {
0 commit comments