Source file RPC_context.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
open Error_monad
class type ['pr] gen_simple =
object
method call_service :
'm 'p 'q 'i 'o.
(([< Resto.meth] as 'm), 'pr, 'p, 'q, 'i, 'o) RPC_service.t ->
'p ->
'q ->
'i ->
'o tzresult Lwt.t
end
class type ['pr] gen_streamed =
object
method call_streamed_service :
'm 'p 'q 'i 'o.
(([< Resto.meth] as 'm), 'pr, 'p, 'q, 'i, 'o) RPC_service.t ->
on_chunk:('o -> unit) ->
on_close:(unit -> unit) ->
'p ->
'q ->
'i ->
(unit -> unit) tzresult Lwt.t
end
class type ['pr] gen =
object
inherit ['pr] gen_simple
inherit ['pr] gen_streamed
end
class type simple =
object
inherit [unit] gen_simple
end
class type streamed =
object
inherit [unit] gen_streamed
end
class type t =
object
inherit simple
inherit streamed
end
type ('o, 'e) rest =
[ `Ok of 'o
| `Conflict of 'e
| `Error of 'e
| `Forbidden of 'e
| `Not_found of 'e
| `Gone of 'e
| `Unauthorized of 'e ]
type ('o, 'e) rest_result = ('o, 'e) rest tzresult
type generic_call_result =
[ `Json of (Data_encoding.json, Data_encoding.json option) rest
| `Binary of (string, string option) rest
| `Other of (string * string) option * (string, string option) rest ]
class type generic =
object
inherit t
method generic_media_type_call :
RPC_service.meth ->
?body:Data_encoding.json ->
Uri.t ->
generic_call_result tzresult Lwt.t
method base : Uri.t
end
type error +=
| Not_found of {meth : RPC_service.meth; uri : Uri.t}
| Gone of {meth : RPC_service.meth; uri : Uri.t}
| Generic_error of {meth : RPC_service.meth; uri : Uri.t}
let base = Uri.make ~scheme:"ocaml" ()
let not_found s p q =
let {RPC_service.meth; uri; _} =
RPC_service.forge_partial_request s ~base p q
in
Lwt_result_syntax.tzfail (Not_found {meth; uri})
let gone s p q =
let {RPC_service.meth; uri; _} =
RPC_service.forge_partial_request s ~base p q
in
Lwt_result_syntax.tzfail (Gone {meth; uri})
let error_with s p q =
let {RPC_service.meth; uri; _} =
RPC_service.forge_partial_request s ~base p q
in
Lwt_result_syntax.tzfail (Generic_error {meth; uri})
class ['pr] of_directory (dir : 'pr RPC_directory.t) =
object
method call_service
: 'm 'p 'q 'i 'o.
(([< Resto.meth] as 'm), 'pr, 'p, 'q, 'i, 'o) RPC_service.t ->
'p ->
'q ->
'i ->
'o tzresult Lwt.t =
fun s p q i ->
let open Lwt_syntax in
let* r = RPC_directory.transparent_lookup dir s p q i in
match r with
| `Ok v | `OkChunk v -> return_ok v
| `OkStream {next; shutdown} -> (
let* o = next () in
match o with
| Some v ->
shutdown () ;
return_ok v
| None ->
shutdown () ;
not_found s p q)
| `Gone None -> gone s p q
| `Not_found None -> not_found s p q
| `Unauthorized (Some err)
| `Forbidden (Some err)
| `Gone (Some err)
| `Not_found (Some err)
| `Conflict (Some err)
| `Error (Some err) ->
return_error err
| `Unauthorized None
| `Error None
| `Forbidden None
| `Created _
| `Conflict None
| `No_content ->
error_with s p q
method call_streamed_service
: 'm 'p 'q 'i 'o.
(([< Resto.meth] as 'm), 'pr, 'p, 'q, 'i, 'o) RPC_service.t ->
on_chunk:('o -> unit) ->
on_close:(unit -> unit) ->
'p ->
'q ->
'i ->
(unit -> unit) tzresult Lwt.t =
fun s ~on_chunk ~on_close p q i ->
let open Lwt_syntax in
let* r = RPC_directory.transparent_lookup dir s p q i in
match r with
| `OkStream {next; shutdown} ->
let rec loop () =
let* o = next () in
match o with
| None ->
on_close () ;
return_unit
| Some v ->
on_chunk v ;
loop ()
in
let _ = loop () in
return_ok shutdown
| `Ok v | `OkChunk v ->
on_chunk v ;
on_close () ;
return_ok (fun () -> ())
| `Gone None -> gone s p q
| `Not_found None -> not_found s p q
| `Unauthorized (Some err)
| `Forbidden (Some err)
| `Gone (Some err)
| `Not_found (Some err)
| `Conflict (Some err)
| `Error (Some err) ->
return_error err
| `Unauthorized None
| `Error None
| `Forbidden None
| `Created _
| `Conflict None
| `No_content ->
error_with s p q
end
let make_call s (ctxt : #simple) = ctxt#call_service s
let make_call1 s ctxt x = make_call s ctxt ((), x)
let make_call2 s ctxt x y = make_call s ctxt (((), x), y)
let make_call3 s ctxt x y z = make_call s ctxt ((((), x), y), z)
type stopper = unit -> unit
let make_streamed_call s (ctxt : #streamed) p q i =
let open Lwt_result_syntax in
let stream, push = Lwt_stream.create () in
let on_chunk v = push (Some v) and on_close () = push None in
let* spill_all = ctxt#call_streamed_service s ~on_chunk ~on_close p q i in
let close () =
spill_all () ;
if Lwt_stream.is_closed stream then () else on_close ()
in
return (stream, close)
let () =
let open Data_encoding in
register_error_kind
`Branch
~id:"RPC_context.Not_found"
~title:"RPC lookup failed"
~description:
"RPC lookup failed. No RPC exists at the URL or the RPC tried to access \
non-existent data."
(obj2
(req "method" RPC_service.meth_encoding)
(req "uri" RPC_encoding.uri_encoding))
~pp:(fun ppf (meth, uri) ->
Format.fprintf
ppf
"Did not find service: %s %a"
(RPC_service.string_of_meth meth)
Uri.pp_hum
uri)
(function Not_found {meth; uri} -> Some (meth, uri) | _ -> None)
(fun (meth, uri) -> Not_found {meth; uri})
let () =
let open Data_encoding in
register_error_kind
`Branch
~id:"RPC_context.Gone"
~title:"RPC lookup failed because of deleted data"
~description:
"RPC lookup failed. Block has been pruned and requested data deleted."
(obj2
(req "method" RPC_service.meth_encoding)
(req "uri" RPC_encoding.uri_encoding))
~pp:(fun ppf (meth, uri) ->
Format.fprintf
ppf
"RPC lookup failed. Block has been pruned and requested data deleted, \
service: %s %a"
(RPC_service.string_of_meth meth)
Uri.pp_hum
uri)
(function Gone {meth; uri} -> Some (meth, uri) | _ -> None)
(fun (meth, uri) -> Gone {meth; uri})