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