Commit | Line | Data |
7ad87df9 |
1 | package Test::Rest; |
2 | |
930013e6 |
3 | use Moose; |
4 | use namespace::autoclean; |
7ad87df9 |
5 | |
6 | use LWP::UserAgent; |
7 | use Params::Validate qw(:all); |
8 | |
9 | sub new { |
10 | my $self = shift; |
e601adda |
11 | my %p = validate( @_, { |
12 | content_type => { type => SCALAR }, |
13 | }, |
14 | ); |
d34c067a |
15 | my $ref = { |
16 | 'ua' => LWP::UserAgent->new, |
7ad87df9 |
17 | 'content_type' => $p{'content_type'}, |
18 | }; |
19 | bless $ref, $self; |
20 | } |
21 | |
d34c067a |
22 | { |
23 | my @non_data_methods = qw(GET DELETE OPTIONS); |
24 | foreach my $method (@non_data_methods) { |
25 | no strict 'refs'; |
26 | my $sub = lc($method); |
27 | *$sub = sub { |
28 | my $self = shift; |
29 | my %p = validate( @_, { url => { type => SCALAR }, }, ); |
30 | my $req = HTTP::Request->new( "$method" => $p{'url'} ); |
31 | $req->content_type( $self->{'content_type'} ); |
32 | return $req; |
33 | }; |
34 | } |
7ad87df9 |
35 | |
d34c067a |
36 | my @data_methods = qw(PUT POST); |
37 | foreach my $method (@data_methods) { |
38 | no strict 'refs'; |
39 | my $sub = lc($method); |
40 | *{$sub} = sub { |
41 | my $self = shift; |
42 | my %p = validate( |
43 | @_, |
44 | { |
45 | url => { type => SCALAR }, |
46 | data => 1, |
47 | }, |
48 | ); |
49 | my $req = HTTP::Request->new( "$method" => $p{'url'} ); |
50 | $req->content_type( $self->{'content_type'} ); |
51 | $req->content_length( |
52 | do { use bytes; length( $p{'data'} ) } |
53 | ); |
54 | $req->content( $p{'data'} ); |
55 | return $req; |
56 | }; |
57 | } |
7ad87df9 |
58 | } |
59 | |
7ad87df9 |
60 | 1; |
61 | |