API-Eulerian
view release on metacpan or search on metacpan
lib/API/Eulerian/EDW/Request.pm view on Meta::CPAN
#
# @author Thorillon Xavier:x.thorillon@eulerian.com
#
# @date 26/11/2021
#
# @version 1.0
#
###############################################################################
#
# Setup module name.
#
package API::Eulerian::EDW::Request;
#
# Enforce compilor rules
#
use strict; use warnings;
#
# Import API::Eulerian::EDW::Status
#
use API::Eulerian::EDW::Status;
#
# Import HTTP::Headers
#
use HTTP::Headers;
#
# Import HTTP::Request
#
use HTTP::Request;
#
# Import LWP::UserAgent
#
use LWP::UserAgent;
#
# Import IO::Socket::SSL
#
use IO::Socket::SSL;
#
# Import HTTP::Status
#
use HTTP::Status qw( :constants :is status_message );
#
# Import JSON
#
use JSON;
#
# Import Encode
#
use Encode;
#
# @brief Create new HTTP Headers.
#
# @param $class - API::Eulerian::EDW::HTTP class.
#
# @return HTTP Headers.
#
sub headers
{
return HTTP::Headers->new();
}
#
# @brief Test if the content type of given HTTP response is a
# JSON format.
#
# @param $class - API::Eulerian::EDW::Request Class.
# @param $response - HTTP response.
#
# @return 1 - Content type is JSON.
# @return 0 - Content type isnt JSON.
#
sub is_json
{
my ( $class, $response ) = @_;
my $type;
# Get content type value from HTTP response
$type = $response->header( 'content-type' );
if( defined( $type ) ) {
# Split content type into an array.
my @subtypes = split( '; ', $type );
# Iterate on subtypes entries
foreach my $subtype ( @subtypes ) {
# Test if subtype is JSON format
if( $subtype eq 'application/json' ) {
return 1;
}
}
}
return 0;
}
#
# @brief Get JSON object from HTTP response.
#
# @param $class - API::Eulerian::EDW::Request class.
# @param $response - HTTP response.
#
# @return JSON object.
#
sub json
{
my ( $class, $response ) = @_;
my $data = undef;
if( $class->is_json( $response ) ) {
$data = $response->decoded_content;
if( defined( $data ) ) {
chomp( $data );
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.
#
lib/API/Eulerian/EDW/Request.pm view on Meta::CPAN
=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.
=back
=head2 post()
I<Send HTTP POST request on given url>
=head3 input
=over 4
=item * url : Remote url.
=item * headers : HTTP headers.
=item * what : Content of POST request.
=item * type : Content type of POST request.
=back
=head3 output
=over 4
=item * API::Eulerian::EDW::Status. On success a new entry named 'response' is inserted into the status.
=back
=head2 headers()
I<Create a new HTTP::Headers instance>
=head3 output
=over 4
=item * Instance of an HTTP::Headers.
=back
=head2 is_json()
I<Test if given HTTP response content is a JSON format>
=head3 input
=over 4
=item * HTTP response.
=back
=head3 output
=over 4
=item * 1 - HTTP response content is in JSON format.
=item * 0 - HTTP response content isnt in JSON format.
=back
=head2 json()
I<Get JSON message from HTTP response>
=head3 input
=over 4
=item * HTTP response.
=back
=head3 output
=over 4
=item * JSON message.
=back
=head1 SEE ALSO
L<API::Eulerian::EDW::Status>
L<HTTP::Headers>
L<HTTP::Request>
L<HTTP::Status>
L<LWP::UserAgent>
L<IO::Socket::SSL>
L<JSON>
L<Encode>
=head1 AUTHOR
Xavier Thorillon <x.thorillon@eulerian.com>
( run in 2.307 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )