Commits

camlspotter committed 01bd5d6

added xjson

Comments (0)

Files changed (3)

   let buf = Buffer.create 100 in
   h#set_writefunction (fun s -> Buffer.add_string buf s; String.length s);
   h#perform;
+  let code = h#get_httpcode in
   h#cleanup; (* Need to flush out cookies *)
-  ok200 (h#get_httpcode, Buffer.contents buf)
+  ok200 (code, Buffer.contents buf)
 
 let download dst f =
   let h = new Curl.handle in
   h#set_writefunction (fun s -> 
     output_string oc s; String.length s);
   h#perform;
+  let code = h#get_httpcode in
   h#cleanup; (* Need to flush out cookies *)
   close_out oc;
-  match h#get_httpcode with
+  match code with
   | 200 -> Unix.rename tmp dst; `Ok dst
   | e -> `Error (`Http (e, tmp))
   
+open Spotlib.Spot
+open Meta_conv.Result.Open
+open Tiny_json
+
+(* CR jfuruse: can be shared with other json apps *)
+let ocaml_of_t : Json.t Ocaml_conv.encoder = fun j -> Ocaml.String (Json.show j)
+let t_of_ocaml : Json.t Ocaml_conv.decoder = fun o ->
+  try match o with
+  | Ocaml.String s -> `Ok (Json.parse s)
+  | _ -> failwith "Ocaml.String expected"
+  with e -> `Error (Meta_conv.Error.Deconstruction_error e, o)
+
+let parse s = try `Ok (Json.parse s) with e -> `Error (`Json_parse e)
+
+(* CR jfuruse: can be shared with other json apps *)
+module Error = struct
+  open Format
+
+  let wrap_json_conv (convf : 'a Json_conv.decoder) s = match convf s with
+    | `Ok v -> `Ok v
+    | `Error e -> `Error (`Json_conv e)
+
+  let format ppf = function
+    | `Http (n, _err) -> fprintf ppf "HTTP Error %d@." n
+    | `Json_parse exn -> fprintf ppf "Error at Json parse: %s@." (Printexc.to_string exn)
+    | `Other exn      -> fprintf ppf "Error: %s@." (Printexc.to_string exn)
+    | `Json_conv e -> Json_conv.format_error_with_source Format.stderr e 
+
+  let from_Ok = function
+    | `Ok v -> v
+    | `Error e -> failwith (Format.sprintf "%a" format e)
+end
+
+let curl_get_and_parse (convf : 'a Json_conv.decoder) curlf = 
+  Xcurl.get_string curlf >>= parse >>= Error.wrap_json_conv convf
+
+open Spotlib.Spot
+open Tiny_json
+
+val ocaml_of_t : Json.t Ocaml_conv.encoder
+val t_of_ocaml : Json.t Ocaml_conv.decoder
+val parse : string -> [> `Error of [> `Json_parse of exn ] | `Ok of Json.t ]
+
+module Error : sig
+
+  val wrap_json_conv : 
+    'a Json_conv.decoder 
+    -> Json.t 
+    ->  ('a, [> `Json_conv of Json.t Meta_conv.Error.t]) Meta_conv.Result.t
+
+  val format : Format.t 
+    -> [< `Http of int * 'a
+       | `Json_conv of Json.t Meta_conv.Error.t
+       | `Json_parse of exn
+       | `Other of exn ] 
+    -> unit
+  val from_Ok :
+    [< `Error of [< `Http of int * 'a
+                 | `Json_conv of Json.t Meta_conv.Error.t
+                 | `Json_parse of exn
+                 | `Other of exn ]
+    | `Ok of 'b ] 
+    -> 'b
+end
+
+val curl_get_and_parse :
+  'a Json_conv.decoder 
+  -> (Curl.handle -> unit) 
+  -> ('a, [> `Http of int * string
+          | `Json_conv of Json.t Meta_conv.Error.t
+          | `Json_parse of exn ]) Meta_conv.Result.t
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.