package azure-cosmos-db

  1. Overview
  2. Docs

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
open Lwt

(*
 Azure cosmos database documentation: https://docs.microsoft.com/en-us/rest/api/cosmos-db/
dune build @check
*)

module type Auth_key = sig
  val master_key : string
  val endpoint : string
end

module type Account = sig
  type verb = Get | Post | Put | Delete
  type resource = Dbs | Colls | Docs | Users

  val authorization : verb -> resource -> string -> string -> string
  val endpoint : string
end

module Auth (Keys : Auth_key) : Account = struct
  type verb = Get | Post | Put | Delete
  type resource = Dbs | Colls | Docs | Users

  let string_of_verb = function
    | Get -> "GET"
    | Post -> "POST"
    | Put -> "PUT"
    | Delete -> "DELETE"

  let string_of_resource = function
    | Dbs -> "dbs"
    | Colls -> "colls"
    | Docs -> "docs"
    | Users -> "users"

  let authorization verb resource date db_name =
    (* "type=master&ver=1.0&sig=" ^ key *)
    let verb = string_of_verb verb in
    (* get, post, put *)
    let resource_type = string_of_resource resource in
    (* "dbs", "colls", "docs". *)
    let resource_id = db_name 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

type cosmos_error = Timeout_error | Azure_error of int

let timeout_error = Lwt.return_error Timeout_error

let wrap_timeout timeout command =
  match timeout with
  | None -> command
  | Some t ->
      let timeout = Lwt_unix.sleep t >>= fun () -> Lwt.return_none in
      Lwt.pick [ timeout; command ]

module Response_headers = struct
  type t = {
    content_type : string option;
    date : string option;
    etag : string option;
    x_ms_activity_id : string option;
    x_ms_alt_content_path : string option;
    x_ms_continuation : string option;
    x_ms_item_count : string option;
    x_ms_request_charge : string option;
    x_ms_resource_quota : string option;
    x_ms_resource_usage : string option;
    x_ms_retry_after_ms : string option;
    x_ms_schemaversion : string option;
    x_ms_serviceversion : string option;
    x_ms_session_token : string option;
  }

  let empty =
    {
      content_type = None;
      date = None;
      etag = None;
      x_ms_activity_id = None;
      x_ms_alt_content_path = None;
      x_ms_continuation = None;
      x_ms_item_count = None;
      x_ms_request_charge = None;
      x_ms_resource_quota = None;
      x_ms_resource_usage = None;
      x_ms_retry_after_ms = None;
      x_ms_schemaversion = None;
      x_ms_serviceversion = None;
      x_ms_session_token = None;
    }

  let update t value_tuple =
    let key, value = value_tuple in
    match key with
    | "Content-Type" -> { t with content_type = Some value }
    | "Date" -> { t with date = Some value }
    | "etag" -> { t with etag = Some value }
    | "x-ms-activity-id" -> { t with x_ms_activity_id = Some value }
    | "x-ms-alt-content-path" -> { t with x_ms_alt_content_path = Some value }
    | "x-ms-continuation" -> { t with x_ms_continuation = Some value }
    | "x-ms-item-count" -> { t with x_ms_item_count = Some value }
    | "x-ms-request-charge" -> { t with x_ms_request_charge = Some value }
    | "x-ms-resource-quota" -> { t with x_ms_resource_quota = Some value }
    | "x-ms-resource-usage" -> { t with x_ms_resource_usage = Some value }
    | "x-ms-retry-after-ms" -> { t with x_ms_retry_after_ms = Some value }
    | "x-ms-schemaversion" -> { t with x_ms_schemaversion = Some value }
    | "x-ms-serviceversion" -> { t with x_ms_serviceversion = Some value }
    | "x-ms-session-token" -> { t with x_ms_session_token = Some value }
    | _ -> t

  let get_header resp =
    resp |> Cohttp_lwt_unix.Response.headers |> Cohttp.Header.to_list
    |> List.fold_left update empty

  let content_type t = t.content_type
  let date t = t.date
  let etag t = t.etag
  let x_ms_activity_id t = t.x_ms_activity_id
  let x_ms_alt_content_path t = t.x_ms_alt_content_path
  let x_ms_continuation t = t.x_ms_continuation
  let x_ms_item_count t = t.x_ms_item_count
  let x_ms_request_charge t = t.x_ms_request_charge
  let x_ms_resource_quota t = t.x_ms_resource_quota
  let x_ms_resource_usage t = t.x_ms_resource_usage
  let x_ms_retry_after_ms t = t.x_ms_retry_after_ms
  let x_ms_schemaversion t = t.x_ms_schemaversion
  let x_ms_serviceversion t = t.x_ms_serviceversion
  let x_ms_session_token t = t.x_ms_session_token
end

module Database (Auth_key : Auth_key) = struct
  module Account = Auth (Auth_key)

  let host = Utility.adjust_host Account.endpoint

  let headers resource verb db_name =
    let ms_date =
      let now = Unix.time () in
      Utility.x_ms_date now
    in
    let header = Cohttp.Header.init () in
    let header =
      Cohttp.Header.add header "authorization"
        (Account.authorization verb resource ms_date db_name)
    in
    let header = Cohttp.Header.add header "x-ms-version" "2017-02-22" in
    let header = Cohttp.Header.add header "x-ms-date" ms_date in
    header

  let json_headers resource verb db_name =
    let header = headers resource verb db_name in
    let header = 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 code body =
    let r =
      let%lwt result = f body in
      if code = expected_code then Lwt.return_ok (expected_code, result)
      else Lwt.return_error (Azure_error code)
    in
    let%lwt () = Cohttp_lwt.Body.drain_body body in
    r

  let result_or_error expected_code code =
    if code = expected_code then Result.ok expected_code
    else Result.error (Azure_error code)

  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 Account.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 headers = json_headers Account.Dbs Account.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 code = get_code resp in
        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 code 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 Account.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 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 code body

  let create_if_not_exists ?timeout name =
    let%lwt exists = get ?timeout name in
    match exists with
    | Ok (404, _) -> create ?timeout name
    | Ok (code, result) -> Lwt.return_ok (code, result)
    | 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 Account.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
        let code = get_code resp in
        Lwt.return (with_204_do code)

  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 Account.Get ("dbs/" ^ dbname))
          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 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 code 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 headers = json_headers Account.Colls Account.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 code = get_code resp in
          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 code 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 Account.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 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 code body

    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 (404, _) ->
          create ?timeout ~indexing_policy ~partition_key dbname coll_name
      | Ok result -> Lwt.return (Result.ok result)
      | Error x -> Lwt.return_error x

    let delete ?timeout name coll_name =
      let path = "/dbs/" ^ name ^ "/colls/" ^ coll_name in
      let header_path = "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 Account.Delete header_path)
          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%lwt () = Cohttp_lwt.Body.drain_body body in
          Lwt.return (with_204_do code)

    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 apply_to_header_if_some name string_of values headers =
        match values with
        | None -> headers
        | Some value -> Cohttp.Header.add headers name (string_of value)

      let add_header name value header = 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 headers =
          json_headers Account.Docs Account.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 () =
          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_header = 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_unix.sleep (milliseconds /. 1000.) in
                post ()
              else Lwt.return (Result.ok (code, value))
          | None -> timeout_error
        in
        post ()

      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 apply_a_im_to_header_if_some name values headers =
          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 headers =
          json_headers Account.Docs Account.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_header = 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)
            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 headers =
          json_headers Account.Docs Account.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 headers =
          json_headers Account.Docs Account.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"
               (fun x -> x)
               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 =
          "/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id
        in
        let headers =
          headers Account.Docs Account.Delete
            ("dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id)
          |> 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 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
            Lwt.return_ok code
        | None -> timeout_error

      let query ?max_item_count ?continuation ?consistency_level ?session_token
          ?is_partition ?partition_key ?timeout dbname coll_name query =
        let headers s =
          let h = headers Account.Docs Account.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 = 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_header = 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)
            in
            let%lwt () = Cohttp_lwt.Body.drain_body body in
            result
    end
  end

  module User = struct
    let resource = Account.Users
    let headers = 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 headers = json_headers resource Account.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 code = get_code resp in
          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 code body

    let list ?timeout dbname =
      let path = "/dbs/" ^ dbname ^ "/users" in
      let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
      let header_path = "dbs/" ^ dbname in
      let response =
        Cohttp_lwt_unix.Client.get
          ~headers:(headers Account.Get header_path)
          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 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 code body

    let get ?timeout dbname user_name =
      let path = "/dbs/" ^ dbname ^ "/users/" ^ user_name in
      let header_path = "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 Account.Get header_path)
          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 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 code 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 header_path = "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 Account.Put header_path)
          ~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 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 code body

    let delete ?timeout dbname user_name =
      let path = "/dbs/" ^ dbname ^ "/users/" ^ user_name in
      let header_path = "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 Account.Delete header_path)
          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%lwt () = Cohttp_lwt.Body.drain_body body in
          Lwt.return (result_or_error 204 code)
  end
end
OCaml

Innovation. Community. Security.