Added wrappers around some common libraries.
[perl4caml.git] / wrappers / pl_LWP_UserAgent.ml
1 (* Wrapper around Perl LWP::UserAgent class.
2  * Copyright (C) 2003 Merjis Ltd.
3  * $Id: pl_LWP_UserAgent.ml,v 1.1 2003-10-12 17:33:15 rich Exp $
4  *)
5
6 open Perl
7
8 open Pl_HTTP_Request
9 open Pl_HTTP_Response
10
11 class lwp_useragent sv =
12
13 object (self)
14
15   method simple_request (request : http_request) =
16     let sv = call_method sv "simple_request" [request#sv] in
17     new http_response sv
18   method request (request : http_request) =
19     let sv = call_method sv "simple_request" [request#sv] in
20     new http_response sv
21   method agent =
22     string_of_sv (call_method sv "agent" [])
23   method set_agent v =
24     call_method_void sv "agent" [sv_of_string v]
25   method from =
26     string_of_sv (call_method sv "from" [])
27   method set_from v =
28     call_method_void sv "from" [sv_of_string v]
29   method timeout =
30     int_of_sv (call_method sv "timeout" [])
31   method set_timeout v =
32     call_method_void sv "timeout" [sv_of_int v]
33   method parse_head =
34     bool_of_sv (call_method sv "parse_head" [])
35   method set_parse_head v =
36     call_method_void sv "parse_head" [sv_of_bool v]
37   method max_size =
38     int_of_sv (call_method sv "max_size" [])
39   method set_max_size v =
40     call_method_void sv "max_size" [sv_of_int v]
41   method env_proxy =
42     call_method_void sv "env_proxy" []
43
44 end
45
46 (* Note that "new" is a reserved word, so I've appended an _ character. *)
47 let new_ ?agent ?from ?timeout ?use_eval ?parse_head ?max_size
48     ?env_proxy ?keep_alive () =
49   let args = ref [] in
50   let may f = function None -> () | Some v -> f v in
51   may (fun v ->
52          args := sv_of_string "agent" :: sv_of_string v :: !args) agent;
53   may (fun v ->
54          args := sv_of_string "from" :: sv_of_string v :: !args) from;
55   may (fun v ->
56          args := sv_of_string "timeout" :: sv_of_int v :: !args) timeout;
57   may (fun v ->
58          args := sv_of_string "use_eval" :: sv_of_bool v :: !args) use_eval;
59   may (fun v ->
60          args := sv_of_string "parse_head" :: sv_of_bool v :: !args)parse_head;
61   may (fun v ->
62          args := sv_of_string "max_size" :: sv_of_int v :: !args) max_size;
63   may (fun v ->
64          args := sv_of_string "env_proxy" :: sv_of_bool v :: !args) env_proxy;
65   may (fun v ->
66          args := sv_of_string "keep_alive" :: sv_of_int v :: !args) keep_alive;
67   let sv = call_class_method "LWP::UserAgent" "new" !args in
68   new lwp_useragent sv