Source file databases.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
open Lwt
module type Auth_key = sig
val master_key : string
val endpoint : string
end
module type Account = sig
type resource = Dbs | Colls | Docs | Users
val authorization :
Utilities.Verb.t -> resource -> Utilities.Ms_time.t -> string -> string
val endpoint : string
end
module Auth (Keys : Auth_key) : Account = struct
type resource = Dbs | Colls | Docs | Users
let string_of_resource = function
| Dbs -> "dbs"
| Colls -> "colls"
| Docs -> "docs"
| Users -> "users"
let authorization verb resource date db_name =
let verb = Utilities.Verb.string_of_verb verb in
let resource_type = string_of_resource resource in
let resource_id = db_name in
let date = Utilities.Ms_time.x_ms_date date in
let result =
Utility.authorization_token_using_master_key verb resource_type
resource_id date Keys.master_key
in
result
let endpoint = Keys.endpoint
end
let wrap_timeout timeout command =
match timeout with
| None -> command
| Some t ->
let timeout = Lwt_unix.sleep t >|= fun () -> Option.none in
Lwt.pick [ timeout; command ]
type cosmos_error =
| Timeout_error
| Connection_error
| Azure_error of int * Response_headers.t
let ( >>= ) = Lwt.bind
let timeout_error = Lwt.return_error Timeout_error
let connection_error = Lwt.return_error Connection_error
module Database (Auth_key : Auth_key) = struct
module Account = Auth (Auth_key)
let host = Utility.adjust_host Account.endpoint
let resource verb db_name =
let ms_date = Utilities.Ms_time.create_now () in
let = Cohttp.Header.init () in
let =
Cohttp.Header.add header "authorization"
(Account.authorization verb resource ms_date db_name)
in
let = Cohttp.Header.add header "x-ms-version" "2017-02-22" in
let =
Cohttp.Header.add header "x-ms-date" (Utilities.Ms_time.x_ms_date ms_date)
in
header
let resource verb db_name =
let = headers resource verb db_name in
let = Cohttp.Header.add header "content_type" "application/json" in
header
let get_code resp =
resp |> Cohttp_lwt_unix.Response.status |> Cohttp.Code.code_of_status
let result_or_error_with_result expected_code f (resp : Cohttp.Response.t)
body =
let code = get_code resp in
let = Response_headers.get_header resp in
let r =
if code = expected_code then
let%lwt result = f body in
Lwt.return_ok (expected_code, result)
else Lwt.return_error (Azure_error (code, headers))
in
let%lwt () = Cohttp_lwt.Body.drain_body body in
r
let result_or_error expected_code resp =
let code = get_code resp in
if code = expected_code then Result.ok expected_code
else
let = Response_headers.get_header resp in
Result.error (Azure_error (code, response_header))
let with_204_do = result_or_error 204
let list_databases ?timeout () =
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:"dbs" () in
let response =
Cohttp_lwt_unix.Client.get
~headers:(headers Account.Dbs Utilities.Verb.Get "")
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| Some (resp, body) ->
let code = get_code resp in
let%lwt body_string = Cohttp_lwt.Body.to_string body in
let value = Json_converter_j.list_databases_of_string body_string in
let%lwt () = Cohttp_lwt.Body.drain_body body in
Lwt.return @@ Result.ok (code, value)
| None -> timeout_error
let create ?timeout name =
let body =
({ id = name } : Json_converter_j.create_database)
|> Json_converter_j.string_of_create_database |> Cohttp_lwt.Body.of_string
in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:"dbs" () in
let = json_headers Account.Dbs Utilities.Verb.Post "" in
let response =
Cohttp_lwt_unix.Client.post ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let result body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return_some (Json_converter_j.database_of_string body_string)
in
result_or_error_with_result 201 result resp body
let get ?timeout name =
let uri =
Uri.make ~scheme:"https" ~host ~port:443 ~path:("dbs/" ^ name) ()
in
let response =
Cohttp_lwt_unix.Client.get
~headers:(headers Account.Dbs Utilities.Verb.Get ("dbs/" ^ name))
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
if code = 200 then
let result body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return_some (Json_converter_j.database_of_string body_string)
in
result_or_error_with_result 200 result resp body
else Lwt.return_error (Azure_error (code, response_header))
let create_if_not_exists ?timeout name =
let%lwt exists = get ?timeout name in
match exists with
| Ok (code, result) -> Lwt.return_ok (code, result)
| Error (Azure_error (404, _)) -> create ?timeout name
| Error x -> Lwt.return_error x
let delete ?timeout name =
let uri =
Uri.make ~scheme:"https" ~host ~port:443 ~path:("dbs/" ^ name) ()
in
let response =
Cohttp_lwt_unix.Client.delete
~headers:(headers Account.Dbs Utilities.Verb.Delete ("dbs/" ^ name))
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let%lwt () = Cohttp_lwt.Body.drain_body body in
Lwt.return (with_204_do resp)
module Collection = struct
let list ?timeout dbname =
let uri =
Uri.make ~scheme:"https" ~host ~port:443
~path:("dbs/" ^ dbname ^ "/colls")
()
in
let response =
Cohttp_lwt_unix.Client.get
~headers:(headers Account.Colls Utilities.Verb.Get ("dbs/" ^ dbname))
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return
@@ Json_converter_j.list_collections_of_string body_string
in
result_or_error_with_result 200 value resp body
let create ?(indexing_policy = None) ?(partition_key = None) ?timeout dbname
coll_name =
let body =
({ id = coll_name; indexing_policy; partition_key }
: Json_converter_j.create_collection)
|> Json_converter_j.string_of_create_collection
|> Cohttp_lwt.Body.of_string
in
let uri =
Uri.make ~scheme:"https" ~host ~port:443
~path:("/dbs/" ^ dbname ^ "/colls")
()
in
let =
json_headers Account.Colls Utilities.Verb.Post ("dbs/" ^ dbname)
in
let response =
Cohttp_lwt_unix.Client.post ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return_some (Json_converter_j.collection_of_string body_string)
in
result_or_error_with_result 201 value resp body
let get ?timeout name coll_name =
let path = "/dbs/" ^ name ^ "/colls/" ^ coll_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.get
~headers:
(headers Account.Colls Utilities.Verb.Get
("dbs/" ^ name ^ "/colls/" ^ coll_name))
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
if code = 200 then
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return_some
(Json_converter_j.collection_of_string body_string)
in
result_or_error_with_result 200 value resp body
else Lwt.return_error (Azure_error (code, headers))
let create_if_not_exists ?(indexing_policy = None) ?(partition_key = None)
?timeout dbname coll_name =
let%lwt exists = get ?timeout dbname coll_name in
match exists with
| Ok result -> Lwt.return (Result.ok result)
| Error (Azure_error (404, _)) ->
create ?timeout ~indexing_policy ~partition_key dbname coll_name
| Error x -> Lwt.return_error x
let delete ?timeout name coll_name =
let path = "/dbs/" ^ name ^ "/colls/" ^ coll_name in
let = "dbs/" ^ name ^ "/colls/" ^ coll_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.delete
~headers:(headers Account.Colls Utilities.Verb.Delete header_path)
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let%lwt () = Cohttp_lwt.Body.drain_body body in
Lwt.return (with_204_do resp)
module Document = struct
type indexing_directive = Include | Exclude
let string_of_indexing_directive = function
| Include -> "Include"
| Exclude -> "Exclude"
let string_of_partition_key s = "[\"" ^ s ^ "\"]"
let name string_of values =
match values with
| None -> headers
| Some value -> Cohttp.Header.add headers name (string_of value)
let name value = Cohttp.Header.add header name value
let create ?is_upsert ?indexing_directive ?partition_key ?timeout dbname
coll_name content =
let body = Cohttp_lwt.Body.of_string content in
let path = "/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs" in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let =
json_headers Account.Docs Utilities.Verb.Post
("dbs/" ^ dbname ^ "/colls/" ^ coll_name)
|> apply_to_header_if_some "x-ms-documentdb-is-upsert"
Utility.string_of_bool is_upsert
|> apply_to_header_if_some "x-ms-indexing-directive"
string_of_indexing_directive indexing_directive
|> apply_to_header_if_some "x-ms-documentdb-partitionkey"
string_of_partition_key partition_key
in
let rec post retry () =
try%lwt
let post_respons =
Cohttp_lwt_unix.Client.post ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt post_respons with
| Some (resp, body) ->
let code = get_code resp in
let%lwt value =
match code with
| 200 ->
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return
(Some
(Json_converter_j.collection_of_string body_string))
| _ -> Lwt.return None
in
let%lwt () = Cohttp_lwt.Body.drain_body body in
if code = 429 then
let = Response_headers.get_header resp in
let milliseconds =
Response_headers.x_ms_retry_after_ms response_header
|> Option.value ~default:"0" |> int_of_string_opt
|> Option.value ~default:0 |> Int.to_float
in
let%lwt () = Lwt_unix.sleep (milliseconds /. 1000.) in
if retry > 0 then post (retry - 1) () else timeout_error
else Lwt.return (Result.ok (code, value))
| None -> timeout_error
with Unix.Unix_error (ECONNREFUSED, _, _) ->
if retry > 0 then
let () = Random.self_init () in
let sleep = Random.int 5 |> float_of_int in
let%lwt () = Lwt_unix.sleep sleep in
post (retry - 1) ()
else connection_error
in
post 10 ()
let create_multiple ?is_upsert ?indexing_directive ?partition_key ?timeout
?(chunk_size = 100) dbname coll_name content_list =
let rec take_first n acc = function
| [] -> Lwt.return @@ acc
| x ->
let first, rest = Utilities.take_first n x in
let%lwt r =
Lwt_list.map_p
(create ?is_upsert ?indexing_directive ?partition_key ?timeout
dbname coll_name)
first
in
take_first n (r @ acc) rest
in
take_first chunk_size [] content_list
type list_result_meta_data = {
rid : string;
self : string;
etag : string;
ts : int;
attachments : string;
}
let convert_to_list_result_meta_data json =
let open Yojson.Basic.Util in
try
let rid = json |> member "_rid" |> to_string in
let self = json |> member "_self" |> to_string in
let etag = json |> member "_etag" |> to_string in
let ts = json |> member "_ts" |> to_int in
let attachments = json |> member "_attachments" |> to_string in
Some { rid; self; etag; ts; attachments }
with Type_error _ -> None
type list_result = {
rid : string;
documents : (string * list_result_meta_data option) list;
count : int;
}
let convert_to_list_result value =
let open Yojson.Basic.Util in
let json = Yojson.Basic.from_string value in
let rid = json |> member "_rid" |> to_string in
let count = json |> member "_count" |> to_int in
let docs = json |> member "Documents" |> to_list in
let string_docs =
List.map
(fun x ->
(Yojson.Basic.to_string x, convert_to_list_result_meta_data x))
docs
in
{ rid; documents = string_docs; count }
let list ?max_item_count ?continuation ?consistency_level ?session_token
?a_im ?if_none_match ?partition_key_range_id ?timeout dbname coll_name
=
let name values =
match values with
| None -> headers
| Some false -> headers
| Some true -> Cohttp.Header.add headers name "Incremental feed"
in
let path = "/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs" in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let =
json_headers Account.Docs Utilities.Verb.Get
("dbs/" ^ dbname ^ "/colls/" ^ coll_name)
|> apply_to_header_if_some "x-ms-max-item-count" string_of_int
max_item_count
|> apply_to_header_if_some "x-ms-continuation"
(fun x -> x)
continuation
|> apply_to_header_if_some "x-ms-consistency-level"
(fun x -> x)
consistency_level
|> apply_to_header_if_some "x-ms-session-token"
(fun x -> x)
session_token
|> apply_a_im_to_header_if_some "A-IM" a_im
|> apply_to_header_if_some "If-None-Match" (fun x -> x) if_none_match
|> apply_to_header_if_some "x-ms-documentdb-partitionkeyrangeid"
(fun x -> x)
partition_key_range_id
in
let get_action =
Cohttp_lwt_unix.Client.get ~headers uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt get_action with
| None -> timeout_error
| Some (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ convert_to_list_result body_string
in
let expected_code = 200 in
let result =
if code = expected_code then
let%lwt result = value body in
Lwt.return_ok (expected_code, response_header, result)
else Lwt.return_error (Azure_error (code, response_header))
in
let%lwt () = Cohttp_lwt.Body.drain_body body in
result
type consistency_level = Strong | Bounded | Session | Eventual
let string_of_consistency_level = function
| Strong -> "Strong"
| Bounded -> "Bounded"
| Session -> "Session"
| Eventual -> "Eventual"
let get ?if_none_match ?partition_key ?consistency_level ?session_token
?timeout dbname coll_name doc_id =
let =
json_headers Account.Docs Utilities.Verb.Get
("dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id)
|> apply_to_header_if_some "If-None-Match" (fun x -> x) if_none_match
|> apply_to_header_if_some "x-ms-documentdb-partitionkey"
(fun x -> x)
partition_key
|> apply_to_header_if_some "x-ms-consistency-level"
string_of_consistency_level consistency_level
|> apply_to_header_if_some "x-ms-session-token"
(fun x -> x)
session_token
in
let path =
"/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id
in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.get ~headers uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| Some (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
Result.ok (code, body)
| None -> timeout_error
let replace ?indexing_directive ?partition_key ?if_match ?timeout dbname
coll_name doc_id content =
let body = Cohttp_lwt.Body.of_string content in
let =
json_headers Account.Docs Utilities.Verb.Put
("dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id)
|> apply_to_header_if_some "x-ms-indexing-directive"
string_of_indexing_directive indexing_directive
|> apply_to_header_if_some "x-ms-documentdb-partitionkey"
string_of_partition_key partition_key
|> apply_to_header_if_some "If-Match" (fun x -> x) if_match
in
let path =
"/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id
in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.put ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| Some (resp, body) ->
let code = get_code resp in
Lwt.return_ok (code, body)
| None -> timeout_error
let delete ?partition_key ?timeout dbname coll_name doc_id =
let path =
Printf.sprintf "/dbs/%s/colls/%s/docs/%s" dbname coll_name doc_id
in
let =
Printf.sprintf "dbs/%s/colls/%s/docs/%s" dbname coll_name doc_id
|> headers Account.Docs Utilities.Verb.Delete
|> apply_to_header_if_some "x-ms-documentdb-partitionkey"
string_of_partition_key partition_key
in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let rec delete retry () =
let response =
Cohttp_lwt_unix.Client.delete ~headers uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| Some (resp, body) ->
let code = get_code resp in
let%lwt () = Cohttp_lwt.Body.drain_body body in
if code = 429 then
let = Response_headers.get_header resp in
let milliseconds =
Response_headers.x_ms_retry_after_ms response_header
|> Option.value ~default:"0" |> int_of_string_opt
|> Option.value ~default:0 |> Int.to_float
in
let%lwt () = Lwt_unix.sleep (milliseconds /. 1000.) in
if retry > 0 then delete (retry - 1) () else timeout_error
else Lwt.return_ok code
| None -> timeout_error
in
delete 3 ()
let delete_multiple ?partition_key ?timeout ?(chunk_size = 100) dbname
coll_name doc_ids =
let rec take_first n acc = function
| [] -> Lwt.return @@ acc
| x ->
let first, rest = Utilities.take_first n x in
let%lwt r =
Lwt_list.map_p
(delete ?partition_key ?timeout dbname coll_name)
first
in
take_first n (r @ acc) rest
in
take_first chunk_size [] doc_ids
let query ?max_item_count ?continuation ?consistency_level ?session_token
?is_partition ?partition_key ?timeout dbname coll_name query =
let s =
let h = headers Account.Docs Utilities.Verb.Post s in
Cohttp.Header.add h "x-ms-documentdb-isquery"
(Utility.string_of_bool true)
|> apply_to_header_if_some "x-ms-max-item-count" string_of_int
max_item_count
|> apply_to_header_if_some "x-ms-continuation"
(fun x -> x)
continuation
|> apply_to_header_if_some "x-ms-consistency-level"
(fun x -> x)
consistency_level
|> apply_to_header_if_some "x-ms-session-token"
(fun x -> x)
session_token
|> apply_to_header_if_some
"x-ms-documentdb-query-enablecrosspartition"
Utility.string_of_bool is_partition
|> apply_to_header_if_some "x-ms-documentdb-partitionkey"
string_of_partition_key partition_key
|> add_header "content-type" "application/query+json"
in
let path = "/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs" in
let = headers ("dbs/" ^ dbname ^ "/colls/" ^ coll_name) in
let body =
Json_converter_j.string_of_query query |> Cohttp_lwt.Body.of_string
in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.post ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
let value () =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ convert_to_list_result body_string
in
let expected_code = 200 in
let result =
if code = expected_code then
let%lwt result = value () in
Lwt.return_ok (expected_code, response_header, result)
else Lwt.return_error (Azure_error (code, response_header))
in
let%lwt () = Cohttp_lwt.Body.drain_body body in
result
end
end
module User = struct
let resource = Account.Users
let = headers resource
let create ?timeout dbname user_name =
let body =
({ id = user_name } : Json_converter_j.create_user)
|> Json_converter_j.string_of_create_user |> Cohttp_lwt.Body.of_string
in
let uri =
Uri.make ~scheme:"https" ~host ~port:443
~path:("/dbs/" ^ dbname ^ "/users")
()
in
let =
json_headers resource Utilities.Verb.Post ("dbs/" ^ dbname)
in
let response =
Cohttp_lwt_unix.Client.post ~headers ~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ Json_converter_j.user_of_string body_string
in
result_or_error_with_result 201 value resp body
let list ?timeout dbname =
let path = "/dbs/" ^ dbname ^ "/users" in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let = "dbs/" ^ dbname in
let response =
Cohttp_lwt_unix.Client.get
~headers:(headers Utilities.Verb.Get header_path)
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ Json_converter_j.list_users_of_string body_string
in
result_or_error_with_result 200 value resp body
let get ?timeout dbname user_name =
let path = "/dbs/" ^ dbname ^ "/users/" ^ user_name in
let = "dbs/" ^ dbname ^ "/users/" ^ user_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.get
~headers:(headers Utilities.Verb.Get header_path)
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ Json_converter_j.user_of_string body_string
in
result_or_error_with_result 200 value resp body
let replace ?timeout dbname user_name new_user_name =
let body =
({ id = new_user_name } : Json_converter_j.create_user)
|> Json_converter_j.string_of_create_user |> Cohttp_lwt.Body.of_string
in
let path = "/dbs/" ^ dbname ^ "/users/" ^ user_name in
let = "dbs/" ^ dbname ^ "/users/" ^ user_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.put
~headers:(headers Utilities.Verb.Put header_path)
~body uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let value body =
let%lwt body_string = Cohttp_lwt.Body.to_string body in
Lwt.return @@ Json_converter_j.user_of_string body_string
in
result_or_error_with_result 200 value resp body
let delete ?timeout dbname user_name =
let path = "/dbs/" ^ dbname ^ "/users/" ^ user_name in
let = "dbs/" ^ dbname ^ "/users/" ^ user_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
let response =
Cohttp_lwt_unix.Client.delete
~headers:(headers Utilities.Verb.Delete header_path)
uri
>>= Lwt.return_some |> wrap_timeout timeout
in
match%lwt response with
| None -> timeout_error
| Some (resp, body) ->
let%lwt () = Cohttp_lwt.Body.drain_body body in
Lwt.return (result_or_error 204 resp)
end
end