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
open Lwt
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
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
let string_of_verb = function
| Get -> "GET"
| Post -> "POST"
| Put -> "PUT"
| Delete -> "DELETE"
let string_of_resource = function
| Dbs -> "dbs"
| Colls -> "colls"
| Docs -> "docs"
let authorization verb resource date db_name =
let verb = string_of_verb verb in
let resource_type = string_of_resource resource in
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
let convert_list_databases s =
Json_converter_j.list_databases_of_string s
module Database (Auth_key : Auth_key) = struct
module Account = Auth(Auth_key)
let host = Account.endpoint ^ ".documents.azure.com"
let resource verb db_name =
let ms_date =
let now = Unix.time () in
Utility.x_ms_date 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" 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 list_databases () =
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:"dbs" () in
Cohttp_lwt_unix.Client.get ~headers:(headers Account.Dbs Account.Get "") uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = Json_converter_j.list_databases_of_string body in
(code, value)
let create 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 Account.Post "") in
Cohttp_lwt_unix.Client.post ~headers ~body uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> Some (Json_converter_j.create_database_result_of_string body)
| _ -> None
in
(code, value)
let get name =
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:("dbs/" ^ name) () in
Cohttp_lwt_unix.Client.get ~headers:(headers Account.Dbs Account.Get ("dbs/" ^ name)) uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> Some (Json_converter_j.database_of_string body)
| _ -> None
in
(code, value)
let delete name =
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:("dbs/" ^ name) () in
Cohttp_lwt_unix.Client.delete ~headers:(headers Account.Dbs Account.Delete ("dbs/" ^ name)) uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun _ ->
code
module Collection = struct
let list dbname =
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path:("dbs/" ^ dbname ^ "/colls") () in
Cohttp_lwt_unix.Client.get ~headers:(headers Account.Colls Account.Get ("dbs/" ^ dbname)) uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = Json_converter_j.list_collections_of_string body in
(code, value)
let create ?(indexing_policy=None) ?(partition_key=None) 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 Account.Post ("dbs/" ^ dbname)) in
Cohttp_lwt_unix.Client.post ~headers ~body uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> Some (Json_converter_j.create_collection_result_of_string body)
| _ -> None
in
(code, value)
let get name coll_name =
let path = "/dbs/" ^ name ^ "/colls/" ^ coll_name in
let uri = Uri.make ~scheme:"https" ~host ~port:443 ~path () in
Cohttp_lwt_unix.Client.get ~headers:(headers Account.Colls Account.Get ("dbs/" ^ name^ "/colls/" ^ coll_name)) uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> Some (Json_converter_j.collection_of_string body)
| _ -> None
in
(code, value)
let delete 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
Cohttp_lwt_unix.Client.delete ~headers:(headers Account.Colls Account.Delete header_path) uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun _ ->
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 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 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 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
Cohttp_lwt_unix.Client.post ~headers ~body uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> Some (Json_converter_j.create_collection_result_of_string body)
| _ -> None
in
code, value
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
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
{ rid; self; etag; ts; attachments; }
type list_result = {
rid: string;
documents: (string * list_result_meta_data) 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
Some { rid; documents = string_docs; count }
let list ?max_item_count ?continuation ?consistency_level ?session_token ?a_im ?if_none_match ?partition_key_range_id 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 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
Cohttp_lwt_unix.Client.get ~headers uri >>= fun (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> convert_to_list_result body
| _ -> None
in
code, response_header, value
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 dbname coll_name doc_id =
let =
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
Cohttp_lwt_unix.Client.get ~headers uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
code, body
let replace ?indexing_directive ?partition_key ?if_match dbname coll_name doc_id content =
let body = Cohttp_lwt.Body.of_string content in
let =
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
Cohttp_lwt_unix.Client.put ~headers ~body uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun _ ->
code, body
let delete ?partition_key dbname coll_name doc_id =
let path = "/dbs/" ^ dbname ^ "/colls/" ^ coll_name ^ "/docs/" ^ doc_id in
let = 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
Cohttp_lwt_unix.Client.delete ~headers uri >>= fun (resp, body) ->
let code = get_code resp in
body |> Cohttp_lwt.Body.to_string >|= fun _ ->
code
let query ?max_item_count ?continuation ?consistency_level ?session_token ?is_partition dbname coll_name query =
let 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
|> 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
Cohttp_lwt_unix.Client.post ~headers ~body uri >>= fun (resp, body) ->
let code = get_code resp in
let = Response_headers.get_header resp in
body |> Cohttp_lwt.Body.to_string >|= fun body ->
let value = match code with
| 200 -> convert_to_list_result body
| _ -> None
in
code, response_header, value
end
end
end