Подтвердить что ты не робот

Какой лучший модуль для HttpRequest в OCaml

Я хочу использовать OCaml для доступа к API Yahoo Finance. По сути, это будет просто куча HTTP-запросов для получения котировок из Yahoo Finance.

Какой модуль я должен использовать?

Я хочу иметь асинхронные HTTP-запросы.

4b9b3361

Ответ 1

Существуют возможности с использованием lwt:

  • ocsigen имеет довольно полную и немного сложную реализацию
  • cohttp немного проще, но не хватает некоторых полезных частей

используя opam для установки:

$ opam install ocsigenserver cohttp

Например, в верхнем слое:

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

и используя 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

Обратите внимание, что также доступна реализация cohttp для асинхронной библиотеки jane street