Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Pretty-printing Lwt data structures & using Cohttp_lwt
@ 2013-10-29 17:23 Ollie Frolovs
  0 siblings, 0 replies; only message in thread
From: Ollie Frolovs @ 2013-10-29 17:23 UTC (permalink / raw)
  To: caml-list

Hello

I wrote this little program to fetch some JSON data from Google Distance Matrix and i am in need of some advice.

My questions are –

(a) is there a way to inspect or pretty-print the information (Cohttp_lwt_unix.Client.get uri) and (Lwt_main.run (Cohttp_lwt_unix.Client.get uri)) return? This would help me to verify if my understanding of how Lwt works is right.

(b) do i have to peform Lwt_main.run twice - once to open the connection and once more the get the contents of the document or is there a better option?

Regards
Ollie


open Core.Std
open Lwt

(* TODO these be parameters *)
let base_uri_string = "https://maps.googleapis.com/maps/api/distancematrix/json?"
let origins = ["Bristol"; "London"; "Cambridge UK"]
let destinations = origins

(* Google API wants parameters separated by pipe rather than a comma.
   This is a violation of the RFC. 
   Uri does not provide for this as of 1.3.10
   But Google API seems to be happy with character escaped version. *)
let anti_rfc_fy s = String.concat ~sep:"|" s

let base_uri = Uri.of_string base_uri_string
let uri = Uri.add_query_params base_uri [("origins", [anti_rfc_fy origins]);
					 ("destinations", [anti_rfc_fy destinations]);
					 ("sensor", ["false"])]

let response = Lwt_main.run (Cohttp_lwt_unix.Client.get uri)
let body = match response with
    None -> failwith "error: request failed -- any reason"
  | Some (_,b) -> Lwt_main.run (Cohttp_lwt_body.string_of_body b)

(* TODO json be p-arsed *)
let () = printf "%s" body

— Ollie

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-10-29 17:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-29 17:23 [Caml-list] Pretty-printing Lwt data structures & using Cohttp_lwt Ollie Frolovs

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox