line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Swagger2::Client; |
2
|
4
|
|
|
4
|
|
195224
|
use Mojo::Base -base; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
22
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
1647
|
use Mojo::JSON; |
|
4
|
|
|
|
|
34815
|
|
|
4
|
|
|
|
|
155
|
|
5
|
4
|
|
|
4
|
|
1483
|
use Mojo::UserAgent; |
|
4
|
|
|
|
|
309890
|
|
|
4
|
|
|
|
|
31
|
|
6
|
4
|
|
|
4
|
|
103
|
use Mojo::Util; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
106
|
|
7
|
4
|
|
|
4
|
|
15
|
use Carp (); |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
41
|
|
8
|
4
|
|
|
4
|
|
1149
|
use Swagger2; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
25
|
|
9
|
4
|
|
|
4
|
|
138
|
use JSON::Validator::OpenAPI; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
153
|
|
10
|
|
|
|
|
|
|
|
11
|
4
|
|
50
|
4
|
|
12
|
use constant DEBUG => $ENV{SWAGGER2_DEBUG} || 0; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
5874
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has base_url => sub { Mojo::URL->new(shift->_swagger->base_url) }; |
14
|
|
|
|
|
|
|
has ua => sub { Mojo::UserAgent->new }; |
15
|
|
|
|
|
|
|
has _validator => sub { JSON::Validator::OpenAPI->new; }; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub generate { |
18
|
4
|
|
|
4
|
1
|
40
|
my $class = shift; |
19
|
4
|
|
|
|
|
12
|
my ($swagger, $url) = _swagger_url(shift); |
20
|
4
|
|
50
|
|
|
29
|
my $paths = $swagger->api_spec->get('/paths') || {}; |
21
|
4
|
|
|
|
|
76
|
my $generated; |
22
|
|
|
|
|
|
|
|
23
|
4
|
50
|
|
|
|
10
|
$generated |
24
|
|
|
|
|
|
|
= 40 < length $url ? Mojo::Util::md5_sum($url) : $url; # 40 is a bit random: not too long |
25
|
4
|
|
|
|
|
113
|
$generated =~ s!\W!_!g; |
26
|
4
|
|
|
|
|
160
|
$generated = "$class\::$generated"; |
27
|
|
|
|
|
|
|
|
28
|
4
|
50
|
|
|
|
34
|
return $generated->new if $generated->isa($class); # already generated |
29
|
4
|
|
|
|
|
11
|
_init_package($generated, $class); |
30
|
4
|
|
|
5
|
|
50
|
Mojo::Util::monkey_patch($generated, _swagger => sub {$swagger}); |
|
5
|
|
|
5
|
|
682
|
|
31
|
|
|
|
|
|
|
|
32
|
4
|
|
|
|
|
63
|
for my $path (keys %$paths) { |
33
|
11
|
|
|
|
|
95
|
for my $http_method (keys %{$paths->{$path}}) { |
|
11
|
|
|
|
|
27
|
|
34
|
20
|
|
|
|
|
148
|
my $op_spec = $paths->{$path}{$http_method}; |
35
|
20
|
|
66
|
|
|
44
|
my $method = $op_spec->{operationId} || $path; |
36
|
20
|
|
|
|
|
61
|
my $code = $generated->_generate_method(lc $http_method, $path, $op_spec); |
37
|
|
|
|
|
|
|
|
38
|
20
|
|
|
|
|
42
|
$method =~ s![^\w]!_!g; |
39
|
20
|
|
|
|
|
13
|
warn "[$generated] Add method $generated\::$method()\n" if DEBUG; |
40
|
20
|
|
|
|
|
39
|
Mojo::Util::monkey_patch($generated, $method => $code); |
41
|
|
|
|
|
|
|
|
42
|
20
|
|
|
|
|
305
|
my $snake = Mojo::Util::decamelize(ucfirst $method); |
43
|
20
|
|
|
|
|
264
|
warn "[$generated] Add method $generated\::$snake()\n" if DEBUG; |
44
|
20
|
|
|
|
|
35
|
Mojo::Util::monkey_patch($generated, $snake => $code); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
4
|
|
|
|
|
92
|
return $generated->new; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _generate_method { |
52
|
20
|
|
|
20
|
|
25
|
my ($class, $http_method, $path, $op_spec) = @_; |
53
|
20
|
|
|
|
|
47
|
my @path = grep {length} split '/', $path; |
|
49
|
|
|
|
|
74
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
return sub { |
56
|
15
|
100
|
|
15
|
|
19721
|
my $cb = ref $_[-1] eq 'CODE' ? pop : undef; |
|
|
|
|
15
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
57
|
15
|
|
|
|
|
19
|
my $self = shift; |
58
|
15
|
|
100
|
|
|
48
|
my $args = shift || {}; |
59
|
15
|
|
|
|
|
37
|
my $req = [$self->base_url->clone]; |
60
|
15
|
|
|
|
|
719
|
my @e = $self->_validate_request($args, $op_spec, $req); |
61
|
|
|
|
|
|
|
|
62
|
15
|
100
|
|
|
|
26
|
if (@e) { |
63
|
5
|
100
|
|
|
|
11
|
unless ($cb) { |
64
|
3
|
100
|
|
|
|
13
|
return _invalid_input_res(\@e) if $self->return_on_error; |
65
|
2
|
|
|
|
|
15
|
Carp::croak('Invalid input: ' . join ' ', @e); |
66
|
|
|
|
|
|
|
} |
67
|
2
|
|
|
|
|
6
|
$self->$cb(\@e, undef); |
68
|
2
|
|
|
|
|
37
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
10
|
|
|
|
|
27
|
push @{$req->[0]->path->parts}, |
72
|
10
|
|
50
|
|
|
11
|
map { local $_ = $_; s,\{(\w+)\},{$args->{$1}//''},ge; $_; } @path; |
|
15
|
|
|
|
|
415
|
|
|
15
|
|
|
|
|
34
|
|
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
15
|
|
|
15
|
|
|
|
|
32
|
|
73
|
|
|
|
|
|
|
|
74
|
10
|
100
|
|
|
|
21
|
if ($cb) { |
75
|
3
|
|
|
|
|
7
|
Scalar::Util::weaken($self); |
76
|
|
|
|
|
|
|
$self->ua->$http_method( |
77
|
|
|
|
|
|
|
@$req, |
78
|
|
|
|
|
|
|
sub { |
79
|
3
|
|
|
3
|
|
4217
|
my ($ua, $tx) = @_; |
80
|
3
|
100
|
|
|
|
5
|
return $self->$cb('', $tx->res) unless my $err = $tx->error; |
81
|
1
|
|
|
|
|
14
|
return $self->$cb($err->{message}, $tx->res); |
82
|
|
|
|
|
|
|
} |
83
|
3
|
|
|
|
|
8
|
); |
84
|
3
|
|
|
|
|
2008
|
return $self; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
7
|
|
|
|
|
26
|
my $tx = $self->ua->$http_method(@$req); |
88
|
7
|
100
|
100
|
|
|
12438
|
return $tx->res if !$tx->error or $self->return_on_error; |
89
|
2
|
|
|
|
|
39
|
Carp::croak(join ': ', grep {defined} $tx->error->{message}, $tx->res->body); |
|
4
|
|
|
|
|
262
|
|
90
|
|
|
|
|
|
|
} |
91
|
20
|
|
|
|
|
106
|
}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _init_package { |
95
|
4
|
|
|
4
|
|
7
|
my ($package, $base) = @_; |
96
|
4
|
50
|
|
4
|
|
22
|
eval <<"HERE" or die "package $package: $@"; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
22
|
|
|
4
|
|
|
|
|
391
|
|
97
|
|
|
|
|
|
|
package $package; |
98
|
|
|
|
|
|
|
use Mojo::Base '$base'; |
99
|
|
|
|
|
|
|
has return_on_error => 0; |
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
HERE |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _invalid_input_res { |
105
|
1
|
|
|
1
|
|
27
|
my $res = Mojo::Message::Response->new; |
106
|
1
|
|
|
|
|
9
|
$res->headers->content_type('application/json'); |
107
|
1
|
|
|
|
|
74
|
$res->body(Mojo::JSON::encode_json({errors => $_[0]})); |
108
|
1
|
|
|
|
|
176
|
$res->code(400)->message($res->default_message); |
109
|
1
|
|
|
|
|
25
|
$res->error({message => 'Invalid input', code => 400}); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _swagger_url { |
113
|
4
|
100
|
|
4
|
|
24
|
if (UNIVERSAL::isa($_[0], 'Swagger2')) { |
114
|
1
|
|
|
|
|
2
|
my $swagger = shift->load->expand; |
115
|
1
|
|
|
|
|
7
|
return ($swagger, $swagger->url); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
else { |
118
|
3
|
|
|
|
|
5
|
my $url = shift; |
119
|
3
|
|
|
|
|
14
|
return (Swagger2->new->load($url)->expand, $url); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _validate_request { |
124
|
15
|
|
|
15
|
|
31
|
my ($self, $args, $op_spec, $req) = @_; |
125
|
15
|
|
|
|
|
41
|
my $query = $req->[0]->query; |
126
|
15
|
|
|
|
|
92
|
my (%data, $body, @e); |
127
|
|
|
|
|
|
|
|
128
|
15
|
50
|
|
|
|
13
|
for my $p (@{$op_spec->{parameters} || []}) { |
|
15
|
|
|
|
|
53
|
|
129
|
15
|
|
|
|
|
49
|
my ($in, $name, $type) = @$p{qw(in name type)}; |
130
|
15
|
100
|
|
|
|
42
|
my $value = exists $args->{$name} ? $args->{$name} : $p->{default}; |
131
|
|
|
|
|
|
|
|
132
|
15
|
100
|
100
|
|
|
50
|
if (defined $value or Swagger2::_is_true($p->{required})) { |
133
|
11
|
|
100
|
|
|
36
|
$type ||= 'object'; |
134
|
|
|
|
|
|
|
|
135
|
11
|
100
|
|
|
|
22
|
if (defined $value) { |
136
|
7
|
100
|
100
|
|
|
53
|
$value += 0 if $type =~ /^(?:integer|number)/ and $value =~ /^\d/; |
137
|
7
|
0
|
0
|
|
|
17
|
$value = ($value eq 'false' or !$value) ? Mojo::JSON->false : Mojo::JSON->true |
|
|
50
|
|
|
|
|
|
138
|
|
|
|
|
|
|
if $type eq 'boolean'; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
11
|
100
|
66
|
|
|
38
|
if ($in eq 'body') { |
|
|
100
|
|
|
|
|
|
142
|
3
|
|
|
|
|
2
|
warn "[Swagger2::Client] Validate $in\n" if DEBUG; |
143
|
|
|
|
|
|
|
push @e, |
144
|
2
|
50
|
|
|
|
157
|
map { $_->{path} = $_->{path} eq "/" ? "/$name" : "/$name$_->{path}"; $_; } |
|
2
|
|
|
|
|
6
|
|
145
|
3
|
|
|
|
|
12
|
$self->_validator->validate($value, $p->{schema}); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
elsif ($in eq 'formData' and $type eq 'file') { |
148
|
2
|
|
|
|
|
3
|
warn "[Swagger2::Client] Validate $in file=$name (@{[defined $value ? 1 : 0]})\n" if DEBUG; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
else { |
151
|
6
|
|
|
|
|
7
|
warn "[Swagger2::Client] Validate $in $name=$value\n" if DEBUG; |
152
|
6
|
|
|
|
|
23
|
push @e, $self->_validator->validate({$name => $value}, {properties => {$name => $p}}); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
15
|
100
|
|
|
|
959
|
if (not defined $value) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
157
|
8
|
|
|
|
|
13
|
next; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
elsif ($in eq 'query') { |
160
|
2
|
|
|
|
|
6
|
$query->param($name => $value); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
elsif ($in eq 'header') { |
163
|
0
|
|
|
|
|
0
|
$req->[1]{$name} = $value; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
elsif ($in eq 'body') { |
166
|
1
|
|
|
|
|
3
|
$data{json} = $value; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
elsif ($in eq 'formData') { |
169
|
1
|
|
|
|
|
3
|
$data{form}{$name} = $value; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
15
|
|
|
|
|
83
|
push @$req, map { ($_ => $data{$_}) } keys %data; |
|
2
|
|
|
|
|
5
|
|
174
|
15
|
50
|
|
|
|
27
|
push @$req, $body if defined $body; |
175
|
|
|
|
|
|
|
|
176
|
15
|
|
|
|
|
31
|
return @e; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=encoding utf8 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 NAME |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Swagger2::Client - A client for talking to a Swagger powered server |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 DEPRECATION WARNING |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
See L. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 DESCRIPTION |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L is a base class for autogenerated classes that can |
194
|
|
|
|
|
|
|
talk to a server using a swagger specification. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Note that this is a DRAFT, so there will probably be bugs and changes. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 SYNOPSIS |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head2 Swagger specification |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
The input L given to L need to point to a valid |
203
|
|
|
|
|
|
|
L |
204
|
|
|
|
|
|
|
document. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
--- |
207
|
|
|
|
|
|
|
swagger: 2.0 |
208
|
|
|
|
|
|
|
basePath: /api |
209
|
|
|
|
|
|
|
paths: |
210
|
|
|
|
|
|
|
/foo: |
211
|
|
|
|
|
|
|
get: |
212
|
|
|
|
|
|
|
operationId: listPets |
213
|
|
|
|
|
|
|
parameters: |
214
|
|
|
|
|
|
|
- name: limit |
215
|
|
|
|
|
|
|
in: query |
216
|
|
|
|
|
|
|
type: integer |
217
|
|
|
|
|
|
|
responses: |
218
|
|
|
|
|
|
|
200: { ... } |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head2 Client |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
The swagger specification will the be turned into a sub class of |
223
|
|
|
|
|
|
|
L, where the "parameters" rules are used to do input |
224
|
|
|
|
|
|
|
validation. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
use Swagger2::Client; |
227
|
|
|
|
|
|
|
$ua = Swagger2::Client->generate("file:///path/to/api.json"); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# blocking (will croak() on error) |
230
|
|
|
|
|
|
|
$pets = $ua->listPets; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# blocking (will not croak() on error) |
233
|
|
|
|
|
|
|
$ua->return_on_error(1); |
234
|
|
|
|
|
|
|
$pets = $ua->listPets; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# non-blocking |
237
|
|
|
|
|
|
|
$ua = $ua->listPets(sub { my ($ua, $err, $pets) = @_; }); |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# with arguments, where the key map to the "parameters" name |
240
|
|
|
|
|
|
|
$pets = $ua->listPets({limit => 10}); |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
The method name added will both be the original C, but a "snake |
243
|
|
|
|
|
|
|
case" version will also be added. Example: |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
"operationId": "listPets" |
246
|
|
|
|
|
|
|
=> $client->listPets() |
247
|
|
|
|
|
|
|
=> $client->list_pets() |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head2 Customization |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
If you want to request a different server than what is specified in |
252
|
|
|
|
|
|
|
the swagger document: |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
$ua->base_url->host("other.server.com"); |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 base_url |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
$base_url = $self->base_url; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
Returns a L object with the base URL to the API. |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head2 ua |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
$ua = $self->ua; |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Returns a L object which is used to execute requests. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 METHODS |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head2 generate |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
$client = Swagger2::Client->generate(Swagger2->new($specification_url)); |
275
|
|
|
|
|
|
|
$client = Swagger2::Client->generate($specification_url); |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Returns an object of a generated class, with the rules from the |
278
|
|
|
|
|
|
|
C<$specification_url>. |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
Note that the class is cached by perl, so loading a new specification from the |
281
|
|
|
|
|
|
|
same URL will not generate a new class. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Copyright (C) 2014-2015, Jan Henning Thorsen |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
288
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head1 AUTHOR |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=cut |