API-CLI

 view release on metacpan or  search on metacpan

examples/bin/metacpancl  view on Meta::CPAN

use 5.010;
use Data::Dumper;
use FindBin '$Bin';
use lib "$Bin/../../lib";

use API::CLI::App::Spec;

package API::CLI::MetaCPAN;
use base 'API::CLI';

sub add_auth {
}

package main;

my $appspec_file = "$Bin/../metacpancl-appspec.yaml";
my $spec = API::CLI::App::Spec->read($appspec_file);
my $runner = App::Spec::Run->new(
    spec => $spec,
    cmd => API::CLI::MetaCPAN->new(
        dir => "$ENV{HOME}/.githubcl",

lib/API/CLI.pm  view on Meta::CPAN

use HTTP::Request;
use App::Spec;
use JSON::XS;
use API::CLI::Request;

use Moo;

has dir => ( is => 'ro' );
has openapi => ( is => 'ro' );

sub add_auth {
    my ($self, $req) = @_;
    my $appconfig = $self->read_appconfig;
    my $token = $appconfig->{token};
    $req->header(Authorization => "Bearer $token");
}

sub read_appconfig {
    my ($self) = @_;
    my $dir = $self->dir;
    my $appconfig = YAML::XS::LoadFile("$dir/config.yaml");
}

sub apicall {
    my ($self, $run) = @_;
    my ($method, $path) = @{ $run->commands };
    my $params = $run->parameters;
    my $opt = $run->options;
    if ($opt->{debug}) {
        warn __PACKAGE__.':'.__LINE__.": apicall($method $path)\n";
        warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$params], ['params']);
        warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$opt], ['opt']);
    }
    $path =~ s{(?::(\w+)|\{(\w+)\})}{$params->{ $1 // $2 }}g;

lib/API/CLI.pm  view on Meta::CPAN


API::CLI - Generic Framework for REST API Command Line Clients

=head1 SYNOPSIS

    use API::CLI::App::Spec;

    package API::CLI::MetaCPAN;
    use base 'API::CLI';

    sub add_auth {
    }

    package main;

    my $appspec_file = "$Bin/../metacpancl-appspec.yaml";
    my $spec = API::CLI::App::Spec->read($appspec_file);
    my $runner = App::Spec::Run->new(
        spec => $spec,
        cmd => API::CLI::MetaCPAN->new(
            dir => "$ENV{HOME}/.githubcl",

lib/API/CLI/App/Spec.pm  view on Meta::CPAN

our $VERSION = '0.001'; # VERSION

use YAML::XS qw/ LoadFile /;

use base 'App::Spec';

use Moo;

has openapi => ( is => 'ro' );

sub from_openapi {
    my ($class, %args) = @_;

    my $file = delete $args{file};
    my $openapi = LoadFile($file);

    my $spec = $class->openapi2appspec(
        openapi => $openapi,
        %args,
    );

    my $appspec = $class->read($spec);

    return $appspec;
}

sub openapi2appspec {
    my ($class, %args) = @_;

    my $openapi = $args{openapi};
    my $name = $args{name};
    my $classname = $args{class};
    my $spec;

    my $paths = $openapi->{paths};
    my $subcommands = $spec->{subcommands} = {};

lib/API/CLI/App/Spec.pm  view on Meta::CPAN

        name => "verbose",
        type => "flag",
        summary => "verbose",
        aliases => ['v'],
    };
    $spec->{apppec}->{version} = '0.001';

    return $spec;
}

sub param2appspec {
    my ($self, $p) = @_;
    my $type = $p->{type};
    my $required = $p->{required};
    my $item = {
        name => $p->{name},
        type => $type,
        required => $required,
        summary => $p->{description},
        $p->{enum} ? (enum => $p->{enum}) : (),
    };

lib/API/CLI/Cmd.pm  view on Meta::CPAN

use strict;
use warnings;

our $VERSION = '0.001'; # VERSION

use feature qw/ say /;
use base 'App::Spec::Run::Cmd';
use API::CLI::App::Spec;
use YAML::XS qw/ LoadFile DumpFile Dump /;

sub appspec_openapi {
    my ($self, $run) = @_;
    my $options = $run->options;
    my $parameters = $run->parameters;

    my $openapi_file = $parameters->{file};
    my $outfile = $options->{out};
    my $class = $options->{class} || 'API::CLI';
    my $name = $options->{name};

    my $openapi = LoadFile($openapi_file);

lib/API/CLI/Request.pm  view on Meta::CPAN


use Moo;

has openapi => ( is => 'ro' );
has method => ( is => 'ro' );
has path => ( is => 'ro' );
has req => ( is => 'rw' );
has url => ( is => 'rw' );
has verbose => ( is => 'ro' );

sub from_openapi {
    my ($class, %args) = @_;

    my $method = $args{method};
    my $path = delete $args{path};
    my $opt = delete $args{options};
    my $params = delete $args{parameters};

    my $self = $class->new(
        openapi => delete $args{openapi},
        method => delete $args{method},

lib/API/CLI/Request.pm  view on Meta::CPAN

    }
    $url->query_form(%query);
    $self->url($url);

    my $req = HTTP::Request->new( $self->method => $self->url );
    $self->req($req);

    return $self;
}

sub request {
    my ($self) = @_;

    my $ua = LWP::UserAgent->new;
    my $req = $self->req;
    my $res = $ua->request($req);
    my $code = $res->code;
    my $content = $res->decoded_content;
    my $status = $res->status_line;

    my $ct = $res->content_type;

lib/API/CLI/Request.pm  view on Meta::CPAN

        if ($ct eq 'application/json') {
            my $coder = JSON::XS->new->ascii->pretty->allow_nonref;
            $data = $coder->decode($content);
            $content = $coder->encode($data);
        }
    }

    return ($ok, $out, $content);
}

sub content {
    shift->req->content(@_);
}

sub header {
    shift->req->header(@_);
}

1;

__END__

=pod

=head1 NAME



( run in 0.593 second using v1.01-cache-2.11-cpan-a5abf4f5562 )