App-Docker-Client

 view release on metacpan or  search on metacpan

lib/App/Docker/Client.pm  view on Meta::CPAN

package App::Docker::Client;

=head1 NAME

App::Docker::Client - Simple and plain Docker client!

=head1 VERSION

Version 0.010300

=cut

our $VERSION = '0.010300';

use 5.16.0;
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Socket 'tcp_connect';
use AnyEvent::HTTP;
use LWP::UserAgent;
use JSON;
use LWP::Protocol::http::SocketUnixAlt;

=head2 new

Constructor

=cut

sub new {
    my $class = shift;
    my $self  = {@_};
    $self->{_scheme_postfixes} = { file => ':', http => '://', https => '://', };
    $self->{_valid_codes}      = { 200  => 1,   201  => 1,     204   => 1 };
    bless $self, $class;
    return $self;
}

=head1 SUBROUTINES/METHODS

=head2 with_valid_code

=cut

sub with_valid_code {
    my ( $self, $code ) = @_;
    return if $code !~ m/^(\d{3})$/;
    $self->{_valid_codes}->{$code} = 1;
    return $self;
}

=head2 without_valid_code

=cut

sub without_valid_code {
    my ( $self, $code ) = @_;
    return if $code !~ m/^(\d{3})$/;
    $self->{_valid_codes}->{$code} = 0;
    return $self;
}

=head2 show_progress

=cut

sub show_progress {
    my ( $self, $show_progress ) = @_;
    $self->{show_progress} = $show_progress ? $show_progress == 0 ? () : 1 : 1;
    return $self;
}

=head2 attach

=cut

sub attach {
    my ( $self, $path, $query, $input, $output ) = @_;
    my $cv       = AnyEvent->condvar;
    my $callback = sub {
        my ( $fh, $headers ) = @_;

lib/App/Docker/Client.pm  view on Meta::CPAN


=cut

sub authority {
    return $_[0]->{authority} || '/var/run/docker.sock' unless $_[1];
    $_[0]->{authority} = $_[1];
    return $_[0]->{authority};
}

=head2 scheme

Getter/Setter for internal hash key scheme.

=cut

sub scheme {
    return $_[0]->{scheme} || 'http' unless $_[1];
    $_[0]->{scheme} = $_[1];
    return $_[0]->{scheme};
}

=head2 ssl_opts

Getter/Setter for internal hash key ssl_opts.

=cut

sub ssl_opts {
    return $_[0]->{ssl_opts} unless $_[1];
    $_[0]->{ssl_opts} = $_[1];
    return $_[0]->{ssl_opts};
}

=head2 json

Getter/Setter for internal hash key json.

=cut

sub json {
    return $_[0]->{json} || JSON->new->utf8() unless $_[1];
    $_[0]->{json} = $_[1];
    return $_[0]->{json};
}

=head2 user_agent

Getter/Setter for internal hash key UserAgent.

=cut

sub user_agent {
    my ( $self, $user_agent ) = @_;
    if ($user_agent) {
        $self->{UserAgent} = $user_agent;
        return $self->{UserAgent};
    }
    return $self->{UserAgent} if $self->{UserAgent};

    if ( -S $self->authority() ) {
        LWP::Protocol::implementor( http => 'LWP::Protocol::http::SocketUnixAlt' );
    }
    $self->{UserAgent} = LWP::UserAgent->new(
        (
            $self->{show_progress} ? ( show_progress => 1 ) : (),
            $self->ssl_opts ? ( ssl_opts => $self->ssl_opts ) : (),
        )
    );
    return $self->{UserAgent};

}

=head2 get

=cut

sub get {
    my ( $self, $path, $options, $callback ) = @_;
    return $self->_hande_response( $self->user_agent->get( $self->uri( $path, %$options ) ) );
}

=head2 delete

=cut

sub delete {
    my ( $self, $path, $options, $callback ) = @_;
    return $self->_hande_response( $self->user_agent->delete( $self->uri( $path, %$options ) ) );
}

=head2 request

=cut

sub request {
    my ( $self, $request, $callback ) = @_;
    return $self->_hande_response( $self->user_agent->request( $request, $callback ) );
}

=head2 post

=cut

sub post {
    my ( $self, $path, $query, $body, $options, $callback ) = @_;
    return $self->request( $self->_http_request( $path, $query ), $callback ) unless $body;
    return $self->request( $self->_handle_json( $path, $query, $body ), $callback )
      unless ($options);
    return $self->request( $self->_handle_custom( $path, $query, $body, $options ), $callback );
}

=head2 uri

Creating a new URI object.

Internal varibales:
    
 * scheme
    
 * authority



( run in 3.560 seconds using v1.01-cache-2.11-cpan-64ef6c95b5d )