19
votes

I wish to use OCaml to access the Yahoo Finance API. Essentially, it will be just a bunch of HTTP requests to get quotes from Yahoo Finance.

Which module I should use?

I wish to have async HTTP requests.

2
@BasileStarynkevitch Not really, I am a total new learner and know nothing. Is Ocamlnet the best? - Jackson Tale
I don't know if it is the best, but it is very good. - Basile Starynkevitch
@BasileStarynkevitch does it provide async ability? - Jackson Tale
I've used to ocamlnet for this exact purpose recently there was no async ability. - rgrinberg

2 Answers

23
votes

There are possibilities using lwt:

  • ocsigen has a quite complete and a bit complex implementation
  • cohttp is a bit simpler but lacks some usefull parts

using opam to install:

$ opam install ocsigenserver cohttp

For instance in a toplevel:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#thread;;
#require "ocsigenserver";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | { Ocsigen_http_frame.frame_content = Some v } ->
      Ocsigen_stream.string_of_stream 100000 (Ocsigen_stream.get v)
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Ocsigen_http_client.get_url
  [ "http://ocsigen.org/";
    "http://stackoverflow.com/" ]

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let result = Lwt_main.run t2

and using cohttp:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#require "cohttp.lwt";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | Some (_, body) -> Cohttp_lwt_unix.Body.string_of_body body
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
  (List.map Uri.of_string
     [ "http://example.org/";
       "http://example2.org/" ])

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let v = Lwt_main.run t2

Notice that an implementation of cohttp for jane street async library is also available

3
votes

Just for the record, there is also ocurl with curl multi API support.