diff options
Diffstat (limited to 'lib/MooseX/Net')
-rw-r--r-- | lib/MooseX/Net/API/Role/Request.pm | 52 |
1 files changed, 52 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..eabb32d --- /dev/null +++ b/lib/MooseX/Net/API/Role/Request.pm @@ -0,0 +1,52 @@ +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; |