API-Eulerian
view release on metacpan or search on metacpan
lib/API/Eulerian/EDW/Request.pm view on Meta::CPAN
if( length( $data ) > 0 ) {
$data = encode( 'utf-8', $data );
$data = decode_json( $data );
} else {
$data = undef;
}
}
}
return $data;
}
#
# @brief Do HTTP Get on given URL.
#
# @param $class - API::Eulerian::EDW::HTTP class.
# @param $url - Remote URL.
# @param $headers - HTTP::Headers.
# @param $file - Local file path.
#
# @return API::Eulerian::EDW::Status instance.
#
sub get
{
my ( $class, $url, $headers, $file ) = @_;
return $class->_request( 'GET', $url, $headers, undef, undef, $file );
}
#
# @brief Do HTTP Post on given URL.
#
# @param $class - API::Eulerian::EDW::HTTP class.
# @param $url - Remote URL.
# @param $headers - HTTP::Headers.
# @param $what - Request Data.
# @param $type - Request Data Type.
#
# @return API::Eulerian::EDW::Status instance.
#
sub post
{
my ( $class, $url, $headers, $what, $type ) = @_;
return $class->_request( 'POST', $url, $headers, $what, $type );
}
#
# @brief Send HTTP request on given url.
#
# @param $class - API::Eulerian::EDW Request class.
# @param $method - HTTP method.
# @param $url - Remote URL.
# @param $headers - HTTP headers.
# @param $what - Data of POST request.
# @param $type - Data type of POST request
# @param $file - Local file path used to store HTTP reply.
#
# @return API::Eulerian::EDW::Status instance.
#
use Data::Dumper;
sub _request
{
my ( $class, $method, $url, $headers, $what, $type, $file ) = @_;
my $status = API::Eulerian::EDW::Status->new();
my $endpoint;
my $request;
# Ensure default type
$type = $type || 'application/json';
# Sanity check POST arguments
if( $method eq 'POST' ) {
if( ! ( defined( $what ) && defined( $type ) ) ) {
$status->error( 1 );
$status->msg( "Mandatory argument to post request is/are missing" );
$status->code( 400 );
return $status;
} else {
# Setup Content_Length and Content_Type
$headers->push_header( Content_Length => length( $what ) );
$headers->push_header( Content_Type => $type );
}
}
# Create HTTP Request
$request = HTTP::Request->new( $method, $url, $headers, $what );
# Create End Point used to communicate with remote server
$endpoint = LWP::UserAgent->new(
keep_alive => 0,
cookie_jar => {},
ssl_opts => {
SSL_verifycn_publicsuffix => '',
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0,
SSL_hostname => '',
},
);
# Increase Read Timeout on TCP socket to avoid being disconnected
$endpoint->timeout( 1800 );
# Send Request, wait response if file is defined reply content is
# writen into local file.
my $response = $endpoint->request( $request, $file );
my $json = API::Eulerian::EDW::Request->json( $response );
$status->{ response } = $response;
if( $response->code != HTTP_OK ) {
$status->error( 1 );
$status->code( $response->code );
$status->msg(
defined( $json ) ?
encode_json( $json ) : $response->content()
);
} else {
if( defined( $response->header( 'content-encoding' ) ) ) {
$status->{ encoding } = $response->header( 'content-encoding' );
}
}
return $status;
}
#
# End up module properly
#
1;
__END__
=pod
=head1 NAME
API::Eulerian::EDW::Request - API::Eulerian::EDW Request module.
=head1 DESCRIPTION
This module is used to send HTTP request to remote Peer.
=head1 METHODS
=head2 get()
I<Send HTTP GET request on given url>
=head3 input
=over 4
=item * url : Remote url.
=item * headers : HTTP headers.
=item * file : [optional] Local file path used to store HTTP reply.
=back
=head3 output
=over 4
=item * API::Eulerian::EDW::Status. On success a new entry named 'response' is inserted into the status.
( run in 0.983 second using v1.01-cache-2.11-cpan-39bf76dae61 )