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