| 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'} || {}
$$data{'paths'} || {}
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
Truth table0%0/2
A
A= $$data{'paths'}
Truth table0%0/2
A
A= $$data{'paths'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $op = $paths->{$path}{$method}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $operation_id = $op->{operationId} or next; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $ct; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($op->{requestBody} && $op->{requestBody}{content}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
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"}
Truth table0%0/3
A and B
A= $$op{"requestBody"}B= $$op{"requestBody"}{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $ct = 'application/json' if $op->{requestBody}{content}{'application/json'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $ct //= 'multipart/form-data' if $op->{requestBody}{content}{'multipart/form-data'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/4
$ct //= "multipart/form-data"
$ct //= "multipart/form-data"
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
Truth table0%0/2
A
A= $ct
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $operations{$operation_id} = { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'}
$self->url || $$ops{'server_url'}
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'}
Truth table0%0/3
A || B
A= $self->urlB= $$ops{'server_url'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return $self->generate_http_request( $method, $url, $response_call, %args ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 169 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||