package ClientRest; use JSON; use Carp; use strict; use warnings; use MIME::Base64; use Data::Dumper; use version; our $VERSION; $VERSION = qv('0.0.1'); use REST::Client; sub new { my $class = shift; my $self = { _host => shift, }; _parameterValidation($self); # instatiate Rest::Client and create constructor my $client = REST::Client->new({ host => $self->{_host}, timeout => 10, }); $self->{_client} = $client; bless $self, $class; return $self; } sub _parameterValidation { my( $self ) = @_; croak "Invalid host syntax: sample 'http://:' " unless ( $self->{_host} =~ /^http:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}$/ ); } sub postSnippetsFile { my ( $self, %options ) = @_; my $headers = { "Content" => {'file' => $options{file}}, "Content-type" => 'multipart/form-data', "Authorization" => 'Basic ' . encode_base64($options{username} . ':' . $options{password}) }; $self->{_client}->POST( $options{url}, $headers ); return $self->{_client}->responseContent(); } 1;