2013-01-03 15 views
19

HttpRequest en iyi modülü nedir. Esasen, Yahoo Finance'den teklif almak için bir grup HTTP isteği olacak.Ben Yahoo Finance API erişmek için <strong>OCaml</strong> kullanmak isteyen OCaml

kullanmalıyım modül ?

ben zaman uyumsuz HTTP isteklerini olmasını diliyoruz.

yüklemek için opam kullanarak bazı faydalı parçaları

  • ocsigen biraz daha basit bir oldukça tam ve biraz karmaşık uygulama
  • cohttp var ama yoksun:

+1

'Ocamlnet' http://projects.camlcity.org/projects/ocamlnet.html –

+0

@BasileStarynkevitch'i düşündüğünüz oldu Gerçekten, ben yeni bir öğrenenim ve hiçbir şey bilmiyorum. Ocamlnet en iyisi mi? –

+0

En iyisi olup olmadığını bilmiyorum, ama çok iyi. –

cevap

23

Orada lwt kullanarak olasılıklar şunlardır:

$ opam install ocsigenserver cohttp 
Bir toplevel Mesela

:

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 

ve kullanma cohttp:

Jane sokak zaman uyumsuz kitaplığı için cohttp bir uygulamasıdır da Sadece kayıt için
+2

"+ ../toplevel" Burada yanıltıcı biraz) _ ->() 'yerine). – Thomas

+0

Çok teşekkürler Pierre, çok kullanışlı kod. – rgrinberg

+0

@Thomas Bence önerileriniz çok iyi. Ve kodları çalıştıramıyorum, ama ''() 'ile Topdirs.dir_dizinini (Sys.getenv" OCAML_TOPLEVEL_PATH ") ile yapabilirsiniz. Diğer kodunu değiştirir misiniz lütfen? – Jack

3

, orada mevcuttur Bildirimi olduğunu
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 

ayrıca ocurl with curl multi API support.