2013-01-03 9 views
19

私はOCamlを使ってYahoo Finance APIにアクセスしたいと考えています。本質的には、Yahoo Financeからの引用を得るためのHTTPリクエストの集まりにすぎません。OCamlのHttpRequestに最適なモジュールは何ですか

どのモジュールを使用する必要がありますか?

私は非同期HTTP要求をしたいと思っています。

+1

あなたは 'Ocamlnet'を考えましたか?http://projects.camlcity.org/projects/ocamlnet.html –

+0

@BasileStarynkevitch本当に、私は全く新しい学習者であり、何も知りません。 Ocamlnetは一番ですか? –

+0

私はそれが最高かどうかわかりませんが、それはとても良いです。 –

答えて

23

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:

ジェーンストリート非同期ライブラリのcohttpの実装はまた、念のため
+2

"+ ../toplevel"は少し誤解を招いています:これはOPAMによってインストールされたコンパイラには必要ではなく、システムコンパイラでは機能しません( 'Topdirs.dir_directory(Sys.getenv" OCAML_TOPLEVEL_PATH " )を代わりに_ - >() 'で置き換えます)。 – Thomas

+0

非常に便利なコードのピエールありがとうございました。 – rgrinberg

+0

@トーマスあなたの提案はとても良いと思います。コードを実行することはできませんが、 'Topdirs.dir_directory(Sys.getenv" OCAML_TOPLEVEL_PATH ")を_ - >()'で試すことができます。彼の他のコードを修正してください。 – Jack

3

、そこに利用可能であることに注意し
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 

ocurl with curl multi API supportです。

関連する問題