| 1 | # ABSTRACT: Generic Framework for REST API Command Line Clients | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | package API::CLI; | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.001_001'; # TRIAL VERSION | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use base 'App::Spec::Run::Cmd'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use YAML::XS (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use App::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use JSON::XS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use API::CLI::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | has dir => ( is => 'ro' ); | |||||||||||||||||||
| 22 | 1 | has openapi => ( is => 'ro' ); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | ▶ | sub add_auth { | ||||||||||||||||||
|
Subroutine
add_auth: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 25 | 0 | my ($self, $req) = @_; | |||||||||||||||||||
| 26 | 0 | my $appconfig = $self->read_appconfig; | |||||||||||||||||||
| 27 | 0 | my $token = $appconfig->{token}; | |||||||||||||||||||
| 28 | 0 | $req->header(Authorization => "Bearer $token"); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | sub read_appconfig { | ||||||||||||||||||
|
Subroutine
read_appconfig: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 32 | 0 | my ($self) = @_; | |||||||||||||||||||
| 33 | 0 | my $dir = $self->dir; | |||||||||||||||||||
| 34 | 0 | my $appconfig = YAML::XS::LoadFile("$dir/config.yaml"); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | ▶ | sub apicall { | ||||||||||||||||||
|
Subroutine
apicall: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||
| 38 | 0 | my ($self, $run) = @_; | |||||||||||||||||||
| 39 | 0 | my ($method, $path) = @{ $run->commands }; | |||||||||||||||||||
| 40 | 0 | my $params = $run->parameters; | |||||||||||||||||||
| 41 | 0 | my $opt = $run->options; | |||||||||||||||||||
| 42 | 0 | ▶ | if ($opt->{debug}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | warn __PACKAGE__.':'.__LINE__.": apicall($method $path)\n"; | |||||||||||||||||||
| 44 | 0 | warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$params], ['params']); | |||||||||||||||||||
| 45 | 0 | warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$opt], ['opt']); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | 0 | ▶ | $path =~ s{(?::(\w+)|\{(\w+)\})}{$params->{ $1 // $2 }}g; | ||||||||||||||||||
|
Condition0%0/3
$1 // $2
MC/DC0%0/2
$1 // $2
$1
$2
Truth table0%0/3
A // B
A= $1B= $2
| |||||||||||||||||||||
| 48 | 0 | ▶ | if ($opt->{debug}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | warn __PACKAGE__.':'.__LINE__.": apicall($method $path)\n"; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | my $REQ = API::CLI::Request->from_openapi( | ||||||||||||||||||||
| 53 | openapi => $run->spec->openapi, | ||||||||||||||||||||
| 54 | method => $method, | ||||||||||||||||||||
| 55 | path => $path, | ||||||||||||||||||||
| 56 | options => $opt, | ||||||||||||||||||||
| 57 | parameters => $params, | ||||||||||||||||||||
| 58 | 0 | ▶ | verbose => $opt->{verbose} ? 1 : 0, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | $self->add_auth($REQ); | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | if ($method =~ m/^(POST|PUT|PATCH|DELETE)$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | my $data_file = $opt->{'data-file'}; | |||||||||||||||||||
| 65 | 0 | ▶ | if (defined $data_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | ▶ | open my $fh, '<', $data_file or die "Could not open '$data_file': $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | my $data = do { local $/; <$fh> }; | |||||||||||||||||||
| 68 | 0 | close $fh; | |||||||||||||||||||
| 69 | 0 | $REQ->content($data); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | my ($ok, $out, $content) = $REQ->request; | |||||||||||||||||||
| 74 | 0 | ▶ | if (defined $out) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | ▶ | unless ($ok) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | $run->err($out); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||
| 79 | 0 | $run->out($out); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | 0 | say $content; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||