blib/lib/Langertha/Role/OpenAPI.pm

filter: Statementstmt 44.5% 53 / 119 Branchbran 12.5% 6 / 48 Conditioncond 5.0% 1 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 61.9% 13 / 21 Podpod 100% 6 / 6 totaltotal 34.9% 79 / 226 CC 37 SCAR 44.4
CC
37
Cov
32%
_build_openapi_operations
182.0
_build_openapi_operations
182.0
get_operation
8.5
CRAP 85.2
« bin/langertha_image ↑ Summary blib/lib/Langertha/CallResult.pm »
1 package Langertha::Role::OpenAPI;
2 # ABSTRACT: Role for APIs with OpenAPI definition
3 169 our $VERSION = '0.505';
4 169 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 169 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 169 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 169 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 169 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 169 ▶use OpenAPI::Modern;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 169 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 169 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 169 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 169 requires qw(
16 openapi_file
17 generate_http_request
18 url
19 json
20 );
21  
22 169 has openapi_operations => (
23 is => 'ro',
24 isa => 'HashRef',
25 lazy_build => 1,
26 );
27 0 ▶sub _build_openapi_operations {
Subroutine _build_openapi_operations: not called CC=13 SCAR=52.0
28 0 ▶ my ( $self ) = @_;
Subroutine _build_openapi_operations: not called CC=13 SCAR=52.0
29 # Slow path: parse YAML + OpenAPI::Modern
30 0 my $oam = $self->openapi;
31 0 my $data = $oam->openapi_document->get('/');
32 0 my %operations;
33 0 ▶ my $paths = $data->{paths} || {};
Condition0%0/4
$$data{'paths'} || {}
l!l
00
$$data{'paths'} || {}
l!l
00
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
Truth table0%0/2
A
A= $$data{'paths'}
Aresult
11
00
Truth table0%0/2
A
A= $$data{'paths'}
Aresult
11
00
34 0 for my $path (keys %$paths) {
35 0 for my $method (keys %{$paths->{$path}}) {
36 0 ▶ next unless ref $paths->{$path}{$method} eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $$paths{$path}{$method} eq "HASH" 0 0 0
unless ref $$paths{$path}{$method} eq "HASH" 0 0 0
37 0 my $op = $paths->{$path}{$method};
38 0 ▶ my $operation_id = $op->{operationId} or next;
Branch: 2 branches
BranchTrueFalseTotal
my $operation_id = $$op{"operationId"} or next 0 0 0
my $operation_id = $$op{"operationId"} or next 0 0 0
39 0 my $ct;
40 0 ▶ if ($op->{requestBody} && $op->{requestBody}{content}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$op{"requestBody"} and $$op{"requestBody"}{"content"}) 0 0 0
if ($$op{"requestBody"} and $$op{"requestBody"}{"content"}) 0 0 0
Condition0%0/6
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
!ll&&!rl&&r
000
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
$$op{"requestBody"} $$op{"requestBody"}{"content"}
MC/DC0%0/2
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
$$op{"requestBody"} $$op{"requestBody"}{"content"}
Truth table0%0/3
A and B
A= $$op{"requestBody"}B= $$op{"requestBody"}{"content"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $$op{"requestBody"}B= $$op{"requestBody"}{"content"}
ABresult
0X0
100
111
41 0 ▶ $ct = 'application/json' if $op->{requestBody}{content}{'application/json'};
Branch: 2 branches
BranchTrueFalseTotal
if $$op{"requestBody"}{"content"}{"application/json"} 0 0 0
if $$op{"requestBody"}{"content"}{"application/json"} 0 0 0
42 0 ▶ $ct //= 'multipart/form-data' if $op->{requestBody}{content}{'multipart/form-data'};
Branch: 2 branches
BranchTrueFalseTotal
if $$op{"requestBody"}{"content"}{"multipart/form-data"} 0 0 0
if $$op{"requestBody"}{"content"}{"multipart/form-data"} 0 0 0
Condition0%0/4
$ct //= "multipart/form-data"
l!l
00
$ct //= "multipart/form-data"
l!l
00
MC/DC0%0/1
$ct //= "multipart/form-data"
$ct
MC/DC0%0/1
$ct //= "multipart/form-data"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
Truth table0%0/2
A
A= $ct
Aresult
11
00
43 }
44 0 ▶ $operations{$operation_id} = {
Branch: 2 branches
BranchTrueFalseTotal
defined $ct ? : 0 0 0
defined $ct ? : 0 0 0
45 method => uc($method),
46 path => $path,
47 defined $ct ? (content_type => $ct) : (),
48 };
49 }
50 }
51 0 ▶ my $server_url = $data->{servers}[0]{url} if $data->{servers};
Branch: 2 branches
BranchTrueFalseTotal
if $$data{"servers"} 0 0 0
if $$data{"servers"} 0 0 0
52 0 return { server_url => $server_url, operations => \%operations };
53 }
54  
55  
56 169 has openapi => (
57 is => 'ro',
58 lazy_build => 1,
59 );
60 0 ▶sub _build_openapi {
Subroutine _build_openapi: not called CC=2 SCAR=17.9
61 0 ▶ my ( $self ) = @_;
Subroutine _build_openapi: not called CC=2 SCAR=17.9
62 0 my ( $format, $file ) = $self->openapi_file;
63 0 ▶ croak "".(ref $self)." can only do format yaml for the OpenAPI spec currently" unless $format eq 'yaml';
Branch: 2 branches
BranchTrueFalseTotal
unless $format eq "yaml" 0 0 0
unless $format eq "yaml" 0 0 0
64 0 my $yaml = $file;
65 0 return OpenAPI::Modern->new(
66 openapi_uri => $yaml,
67 openapi_schema => YAML::PP->new(boolean => 'JSON::PP')->load_string(path($yaml)->slurp_utf8),
68 );
69 }
70  
71  
72 169 has supported_operations => (
73 is => 'ro',
74 isa => 'ArrayRef[Str]',
75 lazy_build => 1,
76 );
77 684 ▶sub _build_supported_operations {
Subroutine _build_supported_operations: called CC=1 SCAR=1.2
78 0 ▶ my ( $self ) = @_;
Subroutine _build_supported_operations: not called CC=1 SCAR=1.2
79 0 return [];
80 }
81  
82  
83 2473 ▶sub can_operation {
Subroutine can_operation: called CC=2 SCAR=9.2
Pod documented
84 0 ▶ my ( $self, $operation_id ) = @_;
Subroutine can_operation: not called CC=2 SCAR=9.2
Pod documented
85 0 ▶ return 1 unless scalar @{$self->supported_operations} > 0;
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @{$self->supported_operations;} > 0 1064 1409 2473
unless scalar @{$self->supported_operations;} > 0 0 0 0
86 0 my %so = map { $_, 1 } @{$self->supported_operations};
87 0 return $so{$operation_id};
88 }
89  
90  
91 2452 ▶sub get_operation {
Subroutine get_operation: called CC=4 SCAR=21.4
Pod documented
92 0 ▶ my ( $self, $operation_id ) = @_;
Subroutine get_operation: not called CC=4 SCAR=21.4
Pod documented
93 0 ▶ croak "".(ref $self)." runs in compatibility mode and is unable to perform this OpenAPI operation"
Branch: 2 branches
BranchTrueFalseTotal
unless $self->can_operation($operation_id) 0 2452 2452
unless $self->can_operation($operation_id) 0 0 0
94 unless ($self->can_operation($operation_id));
95 0 my $ops = $self->openapi_operations;
96 0 ▶ my $op = $ops->{operations}{$operation_id}
Branch: 2 branches
BranchTrueFalseTotal
my $op = $$ops{"operations"}{$operation_id} or croak('' . ref($self) . ": operationId '${operation_id}' not found in spec") 2452 0 2452
my $op = $$ops{"operations"}{$operation_id} or croak('' . ref($self) . ": operationId '${operation_id}' not found in spec") 0 0 0
97 or croak "".(ref $self).": operationId '$operation_id' not found in spec";
98 0 ▶ my $url = $self->url || $ops->{server_url};
Condition16%1/6
$self->url || $$ops{'server_url'}
l!l&&r!l&&!r
245200
$self->url || $$ops{'server_url'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->url || $$ops{'server_url'}
$self->url $$ops{'server_url'}
MC/DC0%0/2
$self->url || $$ops{'server_url'}
$self->url $$ops{'server_url'}
Truth table33%1/3
A || B
A= $self->urlB= $$ops{'server_url'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->urlB= $$ops{'server_url'}
ABresult
1X1
011
000
99 0 return ( $op->{method}, $url.$op->{path}, $op->{content_type} );
100 }
101  
102  
103 2452 ▶sub generate_request {
Subroutine generate_request: called CC=2 SCAR=9.2
Pod documented
104 0 ▶ my ( $self, $operation_id, $response_call, %args ) = @_;
Subroutine generate_request: not called CC=2 SCAR=9.2
Pod documented
105 0 my ( $method, $url, $content_type ) = $self->get_operation($operation_id);
106 0 $log->debugf("[%s] %s %s (%s)", ref $self, $method, $url, $operation_id);
107 0 ▶ $args{content_type} = $content_type if defined $content_type;
Branch: 2 branches
BranchTrueFalseTotal
if defined $content_type 2447 5 2452
if defined $content_type 0 0 0
108 0 return $self->generate_http_request( $method, $url, $response_call, %args );
109 }
110  
111  
112  
113 169 1;
114  
115 __END__
« bin/langertha_image ↑ Summary blib/lib/Langertha/CallResult.pm »