Indeed, XMLRPC and JSONRPC are a bit different on error handling, but abstract these minor differences away.
Signed-off-by: Thomas Gazagnaire <thomas.gazagnaire@citrix.com>
] in
to_string json
-let string_of_response id response =
- let json = match response with
- | Success v ->
+let string_of_response response =
+ let json =
+ if response.Rpc.success then
`Dict [
- "result", v;
+ "result", response.Rpc.contents;
"error", `None;
- "id", `Int id
+ "id", `Int 0L
]
- | Fault f ->
+ else
`Dict [
"result", `None;
- "error", f;
- "id", `Int id
+ "error", response.Rpc.contents;
+ "id", `Int 0L
] in
to_string json
| `Dict d ->
let name = match get "method" d with `String s -> s | _ -> raise (Malformed_method_request str) in
let params = match get "params" d with `List l -> l | _ -> raise (Malformed_method_request str) in
- let id = match get "id" d with `Int i -> i | _ -> raise (Malformed_method_request str) in
- id, { name = name; params = params }
+ let (_:int64) = match get "id" d with `Int i -> i | _ -> raise (Malformed_method_request str) in
+ { name = name; params = params }
| _ -> raise (Malformed_method_request str)
let response_of_string str =
| `Dict d ->
let result = get "result" d in
let error = get "error" d in
- let id = match get "id" d with `Int i -> i | _ -> raise (Malformed_method_response str) in
+ let (_:int64) = match get "id" d with `Int i -> i | _ -> raise (Malformed_method_response str) in
begin match result, error with
| `None, `None -> raise (Malformed_method_response str)
- | `None, v -> id, Fault v
- | v, `None -> id, Success v
+ | `None, v -> { Rpc.success = false; contents = v }
+ | v, `None -> { Rpc.success = true; contents = v }
| _ -> raise (Malformed_method_response str)
end
| _ -> raise (Malformed_method_response str)
val of_string : string -> Rpc.Val.t
val string_of_call: Rpc.call -> string
-val call_of_string: string -> int64 * Rpc.call
+val call_of_string: string -> Rpc.call
-val string_of_response: int64 -> Rpc.Val.t Rpc.response -> string
-val response_of_string: string -> int64 * Rpc.Val.t Rpc.response
+val string_of_response: Rpc.response -> string
+val response_of_string: string -> Rpc.response
params: Val.t list
}
-type 'a response =
- | Success of Val.t
- | Fault of 'a
+type response = {
+ success: bool;
+ contents: Val.t
+}
let module B = Buffer in
let buf = B.create 256 in
let add = B.add_string buf in
- begin match response with
- | Rpc.Success v ->
- add "<?xml version=\"1.0\"?><methodResponse><params><param>";
- add (to_string v);
- add "</param></params></methodResponse>";
- | Rpc.Fault (i,s) ->
- add "<?xml version=\"1.0\"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><int>";
- add (Int64.to_string i);
- add "</int></value></member><member><name>faultString</name><value><string>";
- add s;
- add "</string></value></member></struct></value></fault></methodResponse>";
- end;
+ let v = `Dict [ (if response.Rpc.success then "success" else "failure"), response.Rpc.contents ] in
+ add "<?xml version=\"1.0\"?><methodResponse><params><param>";
+ add (to_string v);
+ add "</param></params></methodResponse>";
B.contents buf
exception Parse_error of string * Xmlm.signal * Xmlm.input
| `Dtd _ -> ignore (Xmlm.input input)
| _ -> () end;
Parser.map_tag "methodResponse" (fun input ->
- match Xmlm.peek input with
- | `El_start ((_,"fault"),_) ->
- Parser.map_tag "fault" (fun input ->
+ Parser.map_tag "params" (fun input ->
+ Parser.map_tag "param" (fun input ->
let signal = Xmlm.peek input in
match Parser.of_xml ?callback [] input with
- | `Dict [ "faultCode", `Int i; "faultString", `String s ] -> Rpc.Fault (i, s)
- | s -> parse_error (to_string s) signal input
- ) input
- | `El_start ((_,"params"),_) ->
- Parser.map_tag "params" (fun input ->
- Parser.map_tag "param" (fun input -> Rpc.Success (Parser.of_xml ?callback [] input)) input
+ | `Dict [ "success", v ] -> { Rpc.success = true; Rpc.contents = v }
+ | `Dict [ "failure", v ] -> { Rpc.success = false; Rpc.contents = v }
+ | v -> parse_error "response" signal input
) input
- | s -> parse_error "response" s input
- ) input
+ ) input
+ ) input
val string_of_call: Rpc.call -> string
val call_of_string: ?callback:Rpc.callback -> string -> Rpc.call
-val string_of_response: (int64 * string) Rpc.response -> string
-val response_of_string: ?callback:Rpc.callback -> string -> (int64 * string) Rpc.response
+val string_of_response: Rpc.response -> string
+val response_of_string: ?callback:Rpc.callback -> string -> Rpc.response