App-Docker-Client
view release on metacpan or search on metacpan
lib/App/Docker/Client.pm view on Meta::CPAN
sub uri {
my ( $self, $path, %opts ) = @_;
require URI;
my $uri =
URI->new(
$self->scheme() . $self->{_scheme_postfixes}->{ lc $self->scheme() } . $self->authority() . '/' . $path );
$uri->query_form(%opts);
return $uri;
}
=head2 to_hashref
Getter/Setter for internal hash key ua.
=cut
sub to_hashref {
my ( $self, $content ) = @_;
return if !$content;
my $data = eval { $self->json->decode($content) };
return $@ ? $content : $data;
require Carp;
Carp::cluck;
Carp::croak "JSON ERROR: $@";
}
{
=head2 post
=cut
sub _handle_json {
my ( $self, $path, $query, $body ) = @_;
my $req = $self->_http_request( $path, $query );
$req->content_type('application/json');
my $json = $self->json->encode($body);
$json =~ s/"(false|true)"/$1/g;
$req->content($json);
return $req;
}
=head2 post
=cut
sub _handle_custom {
my ( $self, $path, $query, $body, $options ) = @_;
my $req = $self->_http_request( $path, $query );
$req->content_type( $options->{content_type} );
$req->content_length(
do { use bytes; length $body }
);
$req->content($body);
return $req;
}
=head2 _http_request
create HTTP::Request by uri params
=cut
sub _http_request {
my ( $self, $path, $query ) = @_;
require HTTP::Request;
return HTTP::Request->new( POST => $self->uri( $path, %$query ) );
}
=head2 _hande_response
=cut
sub _hande_response {
my ( $self, $response ) = @_;
$self->_error_code( $response->code, $response->message, $response->content );
my $content = $response->content();
return $self->to_hashref($content);
}
=head2 _error_code
Simple error handler returns undef if everything is ok dies on error.
=cut
sub _error_code {
my ( $self, $code, $message, $content ) = @_;
return $code if $self->{_valid_codes}->{$code};
require Carp;
Carp::cluck;
Carp::croak "FAILURE: $code - " . qq~$message\n$content~;
}
}
1; # End of App::Docker::Client
__END__
=head1 SYNOPSIS
Sample to inspect a conatainer, for mor posibilities see at the Docker API
documentation L<https://docs.docker.com/engine/api/v1.25/>
use App::Docker::Client;
my $client = App::Docker::Client->new();
my $hash_ref = $client->get('/containers/<container_id>/json');
Create a new container:
$client->post('/containers/create', {}, {
Name => 'container_name',
Tty => 1,
OpenStdin => 1,
Image => 'perl',
});
For a remote authority engine use it like that:
use App::Docker::Client;
my %hash = ( authority => '0.0.0.0:5435' );
my $client = App::Docker::Client->new( %hash );
( run in 0.741 second using v1.01-cache-2.11-cpan-140bd7fdf52 )