| 1 | package OpenAPI::Client; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use Mojo::EventEmitter -base; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use JSON::Validator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Mojo::Promise; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use constant DEBUG => $ENV{OPENAPI_CLIENT_DEBUG} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'OPENAPI_CLIENT_DEBUG'} || 0
MC/DC0%0/1
$ENV{'OPENAPI_CLIENT_DEBUG'} || 0
$ENV{'OPENAPI_CLIENT_DEBUG'}
Truth table50%1/2
A
A= $ENV{'OPENAPI_CLIENT_DEBUG'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | our $VERSION = '1.09'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | has base_url => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | my $validator = $self->validator; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 19 | ▶ | my $url = $validator->can('base_url') ? $validator->base_url->clone : Mojo::URL->new; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 19 | ▶ | $url->scheme('http') unless $url->scheme; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 19 | ▶ | $url->host('localhost') unless $url->host; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | return $url; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 24 | ▶ | has ua => sub { Mojo::UserAgent->new }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | my ($self, $op) = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | my $code = $self->can($op) or Carp::croak('[OpenAPI::Client] No such operationId'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | return $self->$code(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub call_p { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_p: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | my ($self, $op) = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | my $code = $self->can("${op}_p") or return Mojo::Promise->reject('[OpenAPI::Client] No such operationId'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | return $self->$code(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 25 | my ($parent, $specification) = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 25 | ▶ | my $attrs = @_ == 1 ? shift : {@_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 25 | my $class = $parent->_url_to_class($specification); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | ▶ | $parent->_generate_class($class, $specification, $attrs) unless $class->isa($parent); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 25 | my $self = $class->SUPER::new($attrs); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 25 | ▶ | $self->base_url(Mojo::URL->new($self->{base_url})) if $self->{base_url} and !blessed $self->{base_url}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"base_url"} and not builtin::blessed($self->{"base_url"})
MC/DC100%2/2
$self->{"base_url"} and not builtin::blessed($self->{"base_url"})
$self->{"base_url"}
not builtin::blessed($self->{"base_url"})
Truth table100%3/3
A and not(B)
A= $self->{"base_url"}B= not builtin::blessed($self->{"base_url"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 25 | ▶ | $self->ua->transactor->name('Mojo-OpenAPI (Perl)') unless $self->{ua}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 25 | ▶ | if (my $app = delete $self->{app}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | $self->base_url->host(undef)->scheme(undef)->port(undef); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | $self->ua->server->app($app); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 25 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub validator { Carp::confess("validator() is not defined for $_[0]") } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | sub _generate_class { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_class: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | my ($parent, $class, $specification, $attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 11 | my $jv = JSON::Validator->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 11 | ▶ | $jv->coerce($attrs->{coerce} // 'booleans,numbers,strings'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attrs->{"coerce"} // "booleans,numbers,strings"
MC/DC0%0/1
$attrs->{"coerce"} // "booleans,numbers,strings"
$attrs->{"coerce"}
Truth table50%1/2
A
A= $attrs->{"coerce"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | $jv->store->ua->server->app($attrs->{app}) if $attrs->{app}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | my $schema = $jv->schema($specification)->schema; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | die "Invalid schema: $specification has the following errors:\n", join "\n", @{$schema->errors} if @{$schema->errors}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | eval <<"HERE" or Carp::confess("package $class: $@"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | package $class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | use Mojo::Base '$parent'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | HERE | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 11 | ▶ | Mojo::Util::monkey_patch($class => validator => sub {$schema}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
validator: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 11 | ▶ | return unless $schema->can('routes'); # In case it is not an OpenAPI spec | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 9 | for my $route ($schema->routes->each) { | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 15 | my $operation_id = $route->{operation_id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | unless ( $route->{operation_id} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $operation_id = join '_', $route->{method}, $route->{path}; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $operation_id =~ s|\{[^}]+\}|_|g; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $operation_id =~ s|[/-]|_|g; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $operation_id =~ s|__+|_|g; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 15 | warn "[$class] Add method $operation_id() for $route->{method} $route->{path}\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 15 | $class->_generate_method_bnb($operation_id => $route); | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 15 | $class->_generate_method_p("${operation_id}_p" => $route); | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | ▶ | sub _generate_method_bnb { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_method_bnb: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | my ($class, $method_name, $route) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 14 | ▶ | Mojo::Util::monkey_patch $class => $method_name => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 14 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $tx = $self->_build_tx($route, @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 14 | ▶ | if ($tx->error) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return $tx unless $cb; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | Mojo::IOLoop->next_tick(sub { $self->$cb($tx) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | return $self->ua->start($tx) unless $cb; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $self->ua->start($tx, sub { $self->$cb($_[1]) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 15 | ▶ | sub _generate_method_p { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_method_p: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | my ($class, $method_name, $route) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | Mojo::Util::monkey_patch $class => $method_name => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | my $tx = $self->_build_tx($route, @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | ▶ | return $self->ua->start_p($tx) unless my $err = $tx->error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | return Mojo::Promise->new->reject($err->{message}) unless $err->{code}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | return Mojo::Promise->new->reject('WebSocket handshake failed') if $tx->req->is_handshake && !$tx->is_websocket; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$tx->req->is_handshake and not $tx->is_websocket
$tx->req->is_handshake and not $tx->is_websocket
MC/DC0%0/2
$tx->req->is_handshake and not $tx->is_websocket
$tx->req->is_handshake
not $tx->is_websocket
MC/DC0%0/2
$tx->req->is_handshake and not $tx->is_websocket
$tx->req->is_handshake
not $tx->is_websocket
Truth table33%1/3
A and not(B)
A= $tx->req->is_handshakeB= not $tx->is_websocket
Truth table0%0/3
A and not(B)
A= $tx->req->is_handshakeB= not $tx->is_websocket
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | return Mojo::Promise->new->resolve($tx); | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 27 | ▶ | sub _build_tx { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_tx: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 27 | my ($self, $route, $params, %content) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 27 | my $v = $self->validator; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | 27 | my $url = $self->base_url->clone; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 27 | my ($tx, %headers); | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 27 | ▶ | push @{$url->path}, map { local $_ = $_; s,\{([-\w]+)\},{$params->{$1}//''},ge; $_ } grep {length} split '/', | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params->{$1} // ""
MC/DC100%1/1
$params->{$1} // ""
$params->{$1}
Truth table100%2/2
A
A= $params->{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 27 | $route->{path}; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | my @errors = $self->validator->validate_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | [@$route{qw(method path)}], | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | ▶ | body => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | my ($name, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | ▶ | if (exists $params->{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 4 | $content{json} = $params->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | for ('body', sort keys %{$self->ua->transactor->generators}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 14 | ▶ | next unless exists $content{$_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | $params->{$name} = $content{$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 8 | return {exists => $params->{$name}, value => $params->{$name}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | ▶ | formData => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 8 | my ($name, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 8 | my $value = _param_as_array($name => $params); | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 8 | $content{form}{$name} = $params->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 8 | return {exists => !!@$value, value => $value}; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | header => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | my ($name, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | my $value = _param_as_array($name => $params); | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | $headers{$name} = $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | return {exists => !!@$value, value => $value}; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 10 | ▶ | path => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 10 | my ($name, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 10 | return {exists => exists $params->{$name}, value => $params->{$name}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 22 | ▶ | query => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 22 | my ($name, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # An undefined name means JSON::Validator wants the whole parameter | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # hash so it can reassemble an exploded object parameter (style | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # "deepObject", or "form" with explode). The matching keys are passed | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # through to the query string as-is, while the complete hash is handed | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # back for validation. See | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # JSON::Validator::Schema::OpenAPIv3::_get_parameter_value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 22 | ▶ | unless (defined $name) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | my @keys | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | = +($param->{style} // '') eq 'deepObject' | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | ? grep {/^\Q$param->{name}\E\[/} keys %$params | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | ▶ | : grep { exists $params->{$_} } keys %{$param->{schema}{properties} || {}}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$param->{"style"} // ""
$param->{'schema'}{'properties'} || {}
MC/DC0%0/1
$param->{"style"} // ""
$param->{"style"}
MC/DC0%0/1
$param->{'schema'}{'properties'} || {}
$param->{'schema'}{'properties'}
Truth table50%1/2
A
A= $param->{"style"}
Truth table50%1/2
A
A= $param->{'schema'}{'properties'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | $url->query->param($_ => $params->{$_}) for sort @keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | ▶ | return {exists => @keys ? 1 : 0, value => {%$params}}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 18 | my $value = _param_as_array($name => $params); | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 18 | $url->query->param($name => _coerce_collection_format($value, $param)); | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 18 | return {exists => !!@$value, value => $value}; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 27 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 27 | ▶ | if (@errors) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 10 | warn "[@{[ref $self]}] Validation for $route->{method} $url failed: @errors\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 10 | $tx = Mojo::Transaction::HTTP->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 10 | $tx->req->method(uc $route->{method}); | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 10 | $tx->req->url($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 10 | $tx->res->headers->content_type('application/json'); | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | 10 | $tx->res->body(Mojo::JSON::encode_json({errors => \@errors})); | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 10 | $tx->res->code(400)->message($tx->res->default_message); | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | 10 | $tx->res->error({message => 'Invalid input', code => 400}); | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 17 | warn "[@{[ref $self]}] Validation for $route->{method} $url was successful\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 17 | ▶ | $tx = $self->ua->build_tx($route->{method}, $url, \%headers, defined $content{body} ? $content{body} : %content); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 27 | $tx->req->env->{operationId} = $route->{operation_id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 27 | $self->emit(after_build_tx => $tx); | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 27 | return $tx; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 18 | ▶ | sub _coerce_collection_format { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_collection_format: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 18 | my ($value, $param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 18 | ▶ | my $format = $param->{collectionFormat} || (+($param->{type} // '') eq 'array' ? 'csv' : ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$param->{'collectionFormat'} || (($param->{'type'} // '') eq 'array' ? 'csv' : '')
$param->{"type"} // ""
MC/DC0%0/2
$param->{'collectionFormat'} || (($param->{'type'} // '') eq 'array' ? 'csv' : '')
$param->{'collectionFormat'}
$param->{"type"}
Truth table33%1/3
A || B
A= $param->{'collectionFormat'}B= $param->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 18 | ▶ | return $value if !$format or $format eq 'multi'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $format or $format eq "multi"
MC/DC50%1/2
not $format or $format eq "multi"
not $format
$format eq "multi"
Truth table66%2/3
not(A) or B
A= not $formatB= $format eq "multi"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | ▶ | return join "|", @$value if $format eq 'pipes'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | ▶ | return join " ", @$value if $format eq 'ssv'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | ▶ | return join "\t", @$value if $format eq 'tsv'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | return join ",", @$value; | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 28 | ▶ | sub _param_as_array { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_param_as_array: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 28 | my ($name, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 28 | ▶ | return !exists $params->{$name} ? [] : ref $params->{$name} eq 'ARRAY' ? $params->{$name} : [$params->{$name}]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 25 | ▶ | sub _url_to_class { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_url_to_class: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 25 | my ($self, $package) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 25 | $package =~ s!^\w+?://!!; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 25 | $package =~ s!\W!_!g; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 25 | ▶ | $package = Mojo::Util::md5_sum($package) if length $package > 110; # 110 is a bit random, but it cannot be too long | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 25 | return "$self\::$package"; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | OpenAPI::Client - A client for talking to an Open API powered server | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | L<OpenAPI::Client> generates objects that can talk to Open API servers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | For each fresh OpenAPI contract given to the L</new> method, a custom subclass | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | is created from the Open API specification, with methods corresponding | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | to the operationIds. Parameters received by these methods are transformed | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | into HTTP requests. Input validation is performed, so invalid data won't be | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | sent to the server. | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | Note that this implementation is currently EXPERIMENTAL, but unlikely to change! | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | Feedback is appreciated. | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | =head2 Creating a client | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | use OpenAPI::Client; | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | $client = OpenAPI::Client->new("file:///path/to/api.json"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | The specification given to L</new> must point to a valid OpenAPI document. | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | Several syntax variants are admitted -- see the L</new> method. | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 Open API specification | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | The OpenAPI document can be in OpenAPI version v2.x or v3.x, and it can be in either JSON or YAML | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | format. Example: | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | openapi: 3.0.1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | info: | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | title: Swagger Petstore | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | version: 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | servers: | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | - url: http://petstore.swagger.io/v1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | paths: | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | /pets: | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | get: | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | operationId: listPets | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | The url specified in the OpenAPI document can be altered at any time, if you need to send data to another | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | custom endpoint -- see the L</base_url> method. | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | =head2 Client | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | The OpenAPI API specification will be used to generate a sub-class of | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | L<OpenAPI::Client> with additional methods corresponding to "operationId" inside of each path definition : | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Blocking | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | $tx = $client->listPets; | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # Non-blocking | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | $client = $client->listPets(sub { my ($client, $tx) = @_; }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # Promises | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | $promise = $client->listPets_p->then(sub { my $tx = shift }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # With parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | $tx = $client->listPets({limit => 10}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | See L<Mojo::Transaction> for more information about what you can do with the | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | C<$tx> object. Often you just want something like this: | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # Check for errors | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | die $tx->error->{message} if $tx->error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # Extract data from the JSON responses | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | say $tx->res->json->{pets}[0]{name}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Check out L<Mojo::Transaction/error>, L<Mojo::Transaction/req> and | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | L<Mojo::Transaction/res> for some of the most used methods in that class. | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | =head1 CUSTOMIZATION | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | =head2 Custom server URL | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | If you want to request another server than the one specified in the Open | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | API document, you can change the L</base_url>: | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # Pass on a Mojo::URL object to the constructor | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | $base_url = Mojo::URL->new("http://example.com"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | $client1 = OpenAPI::Client->new("file:///path/to/api.json", base_url => $base_url); | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # A plain string will be converted to a Mojo::URL object | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | $client2 = OpenAPI::Client->new("file:///path/to/api.json", base_url => "http://example.com"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # Change the base_url after the client has been created | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | $client3 = OpenAPI::Client->new("file:///path/to/api.json"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | $client3->base_url->host("other.example.com"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | =head2 Custom content | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | You can send XML or any format you like, but this requires you to add a new | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | "generator": | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | use Your::XML::Library "to_xml"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | $client->ua->transactor->add_generator(xml => sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | my ($t, $tx, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | $tx->req->body(to_xml $data); | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | return $tx; | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | $client->addHero({}, xml => {name => "Supergirl"}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | See L<Mojo::UserAgent::Transactor> for more details. | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | =head1 EVENTS | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | =head2 after_build_tx | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||
| 361 | $client->on(after_build_tx => sub { my ($client, $tx) = @_ }) | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | This event is emitted after a L<Mojo::UserAgent::Transactor> object has been | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | built, just before it is passed on to the L</ua>. Note that all validation has | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | already been run, so altering the C<$tx> too much might cause an invalid | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | request on the server side. | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | A special L<Mojo::Message::Request/env> variable will be set, to reference the | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | operationId: | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | $tx->req->env->{operationId}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | Note that this usage of C<env()> is currently EXPERIMENTAL: | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||
| 375 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | =head2 base_url | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | $base_url = $client->base_url; | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | Returns a L<Mojo::URL> object with the base URL to the API. The default value | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | comes from C<schemes>, C<basePath> and C<host> in the OpenAPI v2 specification | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | or from C<servers> in the OpenAPI v3 specification. | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | =head2 ua | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | $ua = $client->ua; | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | Returns a L<Mojo::UserAgent> object which is used to execute requests. | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||
| 393 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | $client = OpenAPI::Client->new($specification, \%attributes); | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | $client = OpenAPI::Client->new($specification, %attributes); | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | Returns an object of a dynamic subclass of C<OpenAPI::Client>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | with methods generated from the OpenAPI specification located at C<$specification>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | Such subclasses are cached, so several invocations of C<new()> with the same | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | C<$specification> URL will result in several instances of the same subclass. | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | The C<$specification> argument can accept various syntaxes -- see L<JSON::Validator/schema>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | Extra C<%attributes> can be: | ||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | =item app | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | Can be used to run against a local L<Mojolicious> instance instead of issuing real | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | HTTP calls to a remote server. | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | =item coerce | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | See L<JSON::Validator/coerce>. Default to "booleans,numbers,strings". | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | =head1 INSTANCE METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =head2 call | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | $tx = $client->call($operationId => \%params, %content); | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | $client = $client->call($operationId => \%params, %content, sub { my ($client, $tx) = @_; }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||
| 428 | Used to call an C<$operationId> that is not a proper Perl method name, such as | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | "list pets" instead of "listPets", or to check if an C<$operationId> is supported. | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | Unsupported operationIds throw an exception matching text "No such operationId". | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||
| 432 | C<$operationId> is the name of the resource defined in the | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | L<OpenAPI specification|https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | C<$params> is optional, but must be a hash ref, where the keys should match a | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | named parameter in the L<OpenAPI specification|https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | C<%content> is used for the body of the request, where the key needs to be | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | either "body" or a matching L<Mojo::UserAgent::Transactor/generators>. Example: | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | $client->addHero({}, body => "Some data"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | $client->addHero({}, json => {name => "Supergirl"}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||
| 444 | Like in L<Mojo::UserAgent>, an additional coderef argument can be supplied in last position. | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | In that case the call is asynchronous, and the coderef will be called as a continuation | ||||||||||||||||||||||||||||||||||||||||||||||
| 446 | callback, with two arguments C<$client> (the current openAPI client) and C<$tx> | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | (a L<Mojo::Transaction> object). See L<Mojolicious::Guides::Cookbook/Non-blocking> for details. | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | =head2 call_p | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | $promise = $client->call_p($operationId => $params, %content); | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | $promise->then(sub { my $tx = shift }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||
| 454 | As L</call> above, but returns a L<Mojo::Promise> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =head2 validator | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | $validator = $client->validator; | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | $validator = $class->validator; | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||
| 461 | Returns the L<JSON::Validator::Schema> object associated with the current generated class. | ||||||||||||||||||||||||||||||||||||||||||||||
| 462 | Depending on the openAPI specification, this object will belong to the | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | L<OpenAPIv2|JSON::Validator::Schema::OpenAPIv2> or | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | L<OpenAPIv3|JSON::Validator::Schema::OpenAPIv3> subclass. | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | This object global to the class, so changing it will affect all instances returned by L</new>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||
| 467 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||
| 469 | Copyright (C) 2017-2021, Jan Henning Thorsen | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||
| 476 | =head2 Project Founder | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||
| 478 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 Contributors | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | =item * Clive Holloway <clhollow@estee.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||
| 487 | =item * Ed J <mohawk2@users.noreply.github.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||
| 489 | =item * Jan Henning Thorsen <jan.henning@thorsen.pm> | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | =item * Jan Henning Thorsen <jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | =item * Mohammad S Anwar <mohammad.anwar@yahoo.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =item * Reneeb <info@perl-services.de> | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | =item * Roy Storey <kiwiroy@users.noreply.github.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | =item * Veesh Goldman <rabbiveesh@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | =item * Laurent Dami <dami@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||