CPAN-Mini-Inject-REST-Client
view release on metacpan or search on metacpan
lib/CPAN/Mini/Inject/REST/Client/API.pm view on Meta::CPAN
package CPAN::Mini::Inject::REST::Client::API;
use Moose;
use Carp qw/confess/;
use HTTP::Request::Common;
use JSON;
use MIME::Base64;
use REST::Client;
use Try::Tiny;
use URI;
has 'host' => (isa => 'Str', is => 'ro', required => 1);
has 'protocol' => (isa => 'Str', is => 'ro', required => 1);
has 'port' => (isa => 'Int', is => 'ro', required => 1);
has 'username' => (isa => 'Str', is => 'ro');
has 'password' => (isa => 'Str', is => 'ro');
has 'client' => (isa => 'REST::Client', is => 'ro', lazy => 1, builder => '_build_client');
#--Initiate a REST::Client object with optional HTTP authorisation--------------
sub _build_client {
my $self = shift;
my $client = REST::Client->new;
if ($self->username && $self->password) {
$client->addHeader('Authorization', 'Basic ' . encode_base64($self->username . ":" . $self->password));
}
return $client;
}
#--Define the API version to use------------------------------------------------
sub base_uri {
return '/api/1.0';
}
#--Send an HTTP POST request to the server--------------------------------------
sub post {
my ($self, $path, $params) = @_;
my $request = POST(
$self->uri($path),
Content_Type => 'form-data',
Content => $params,
);
my $response = $self->client->POST(
$self->uri($path),
$request->content,
{'Content-Type' => $request->header('Content-Type')},
);
return $self->process($response);
}
#--Send an HTTP GET request to the server---------------------------------------
sub get {
my ($self, $path, $params) = @_;
( run in 2.033 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )