私はOCamlを使ってYahoo Finance APIにアクセスしたいと考えています。本質的には、Yahoo Financeからの引用を得るためのHTTPリクエストの集まりにすぎません。OCamlのHttpRequestに最適なモジュールは何ですか
どのモジュールを使用する必要がありますか?
私は非同期HTTP要求をしたいと思っています。
私はOCamlを使ってYahoo Finance APIにアクセスしたいと考えています。本質的には、Yahoo Financeからの引用を得るためのHTTPリクエストの集まりにすぎません。OCamlのHttpRequestに最適なモジュールは何ですか
どのモジュールを使用する必要がありますか?
私は非同期HTTP要求をしたいと思っています。
lwtを使用しての可能性があります:
を欠い:
$ 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の実装はまた、念のため"+ ../toplevel"は少し誤解を招いています:これはOPAMによってインストールされたコンパイラには必要ではなく、システムコンパイラでは機能しません( 'Topdirs.dir_directory(Sys.getenv" OCAML_TOPLEVEL_PATH " )を代わりに_ - >() 'で置き換えます)。 – Thomas
非常に便利なコードのピエールありがとうございました。 – rgrinberg
@トーマスあなたの提案はとても良いと思います。コードを実行することはできませんが、 'Topdirs.dir_directory(Sys.getenv" OCAML_TOPLEVEL_PATH ")を_ - >()'で試すことができます。彼の他のコードを修正してください。 – Jack
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
あなたは 'Ocamlnet'を考えましたか?http://projects.camlcity.org/projects/ocamlnet.html –
@BasileStarynkevitch本当に、私は全く新しい学習者であり、何も知りません。 Ocamlnetは一番ですか? –
私はそれが最高かどうかわかりませんが、それはとても良いです。 –