diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-03 10:01:01 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-03 10:01:01 +0200 |
commit | 3da11a8153d3b42af2f2a250008be6cc52e57b09 (patch) | |
tree | 4da02b541e9f8d35e5f20d63908cd33fe64dc7f8 /lib/MooseX/Net/API/Role/Request.pm | |
parent | replace remainging with nothing (diff) | |
parent | fix attribute declaration (diff) | |
download | moosex-net-api-3da11a8153d3b42af2f2a250008be6cc52e57b09.tar.gz |
merge
Diffstat (limited to '')
-rw-r--r-- | lib/MooseX/Net/API/Role/Request.pm | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/MooseX/Net/API/Role/Request.pm b/lib/MooseX/Net/API/Role/Request.pm new file mode 100644 index 0000000..214411c --- /dev/null +++ b/lib/MooseX/Net/API/Role/Request.pm @@ -0,0 +1,94 @@ +package MooseX::Net::API::Role::Request; + +use Moose::Role; +use HTTP::Request; +use MooseX::Net::API::Error; +use MooseX::Types::URI qw(Uri); + +has api_base_url => ( + is => 'rw', + isa => Uri, + coerce => 1, + lazy => 1, + default => sub { + my $self = shift; + my $api_base_url = $self->meta->get_option('api_base_url'); + if (!$api_base_url) { + die MooseX::Net::API::Error->new( + reason => "'api_base_url' have not been defined"); + } + $api_base_url; + } +); + +sub http_request { + my ($self, $method, $uri, $params_in_url, $args) = @_; + + my $request; + + if ( $method =~ /^(?:GET|DELETE)$/ || $params_in_url ) { + $uri->query_form(%$args); + $request = HTTP::Request->new( $method => $uri ); + } + elsif ( $method =~ /^(?:POST|PUT)$/ ) { + $request = HTTP::Request->new( $method => $uri ); + my $content = $self->serialize($args); + $request->content($content); + } + else { + die MooseX::Net::API::Error->new( + reason => "$method is not defined" ); + } + + $request->header( + 'Content-Type' => $self->content_type->{$self->api_format}->{value}) + if $self->api_format_mode eq 'content-type'; + + # XXX lwp hook! + my $result = $self->api_useragent->request($request); + return $result; +} + +1; +__END__ + +=head1 NAME + +MooseX::Net::API::Role::Request + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=head2 METHODS + +=over 4 + +=item B<http_request> + +=back + +=head2 ATTRIBUTES + +=over 4 + +=item B<api_base_url> + +=back + +=head1 AUTHOR + +franck cuny E<lt>franck@lumberjaph.netE<gt> + +=head1 SEE ALSO + +=head1 LICENSE + +Copyright 2009, 2010 by Linkfluence + +http://linkfluence.net + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut |