forked from loafle/openapi-generator-original
update error message, update test script
This commit is contained in:
@@ -7,7 +7,7 @@ use utf8;
|
||||
use LWP::UserAgent;
|
||||
use HTTP::Headers;
|
||||
use HTTP::Response;
|
||||
use HTTP::Request::Common;
|
||||
use HTTP::Request::Common qw(DELETE POST GET HEAD PUT);
|
||||
use HTTP::Status;
|
||||
use URI::Query;
|
||||
use JSON;
|
||||
@@ -75,7 +75,8 @@ sub call_api {
|
||||
}
|
||||
|
||||
# body data
|
||||
my $_body_data = %$post_params ? $post_params : to_json($body_data->to_hash);
|
||||
$body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
|
||||
my $_body_data = %$post_params ? $post_params : $body_data;
|
||||
|
||||
# Make the HTTP request
|
||||
my $_request;
|
||||
@@ -84,6 +85,7 @@ sub call_api {
|
||||
# multipart
|
||||
my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
|
||||
'form-data' : $header_params->{'Content-Type'};
|
||||
|
||||
$_request = POST($_url, Accept => $header_params->{Accept},
|
||||
Content_Type => $_content_type, Content => $_body_data);
|
||||
}
|
||||
@@ -98,9 +100,17 @@ sub call_api {
|
||||
$_request = GET($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'});
|
||||
}
|
||||
case 'PUT' {
|
||||
|
||||
case 'HEAD' {
|
||||
$_request = HEAD($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'});
|
||||
}
|
||||
case 'DELETE' { #TODO support form data
|
||||
$_request = DELETE($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'}, Content => $_body_data);
|
||||
}
|
||||
case 'PATCH' { #TODO
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$ua->timeout($http_timeout);
|
||||
@@ -109,8 +119,7 @@ sub call_api {
|
||||
my $_response = $ua->request($_request);
|
||||
|
||||
unless ($_response->is_success) {
|
||||
croak("Can't connect to the server");
|
||||
#croak("Can't connect to the api ($_response{code}): $_response{message}");
|
||||
croak("API Exception(".$_response->code."): ".$_response->message);
|
||||
}
|
||||
|
||||
return $_response->content;
|
||||
|
||||
@@ -112,7 +112,11 @@ sub new {
|
||||
}{{/pathParams}}
|
||||
{{#formParams}} # form params
|
||||
if ( exists $args->{'{{paramName}}'} ) {
|
||||
$form_params->{'{{baseName}}'} = {{#isFile}}'@' . {{/isFile}}WWW::{{invokerPackage}}::APIClient::to_form_value($args->{'{{paramName}}'});
|
||||
{{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'};
|
||||
push $form_params->{'{{baseName}}'}, $args->{'{{paramName}}'};
|
||||
{{/isFile}}
|
||||
{{^isFile}}$form_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_form_value($args->{'{{paramName}}'});
|
||||
{{/isFile}}
|
||||
}{{/formParams}}
|
||||
my $_body_data;
|
||||
{{#bodyParams}} # body params
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
use LWP::UserAgent;
|
||||
|
||||
my $ua = LWP::UserAgent->new;
|
||||
|
||||
my $server_endpoint = "http://petstore.swagger.io/v2/pet/10002";
|
||||
|
||||
# set custom HTTP request header fields
|
||||
my $req = HTTP::Request->new(GET => $server_endpoint);
|
||||
$req->header('content-type' => 'application/json');
|
||||
|
||||
use Data::Dumper;
|
||||
print Dumper($req);
|
||||
|
||||
my $resp = $ua->request($req);
|
||||
if ($resp->is_success) {
|
||||
my $message = $resp->decoded_content;
|
||||
print "Received reply: $message\n";
|
||||
}
|
||||
else {
|
||||
print "HTTP GET error code: ", $resp->code, "\n";
|
||||
print "HTTP GET error message: ", $resp->message, "\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ use utf8;
|
||||
use LWP::UserAgent;
|
||||
use HTTP::Headers;
|
||||
use HTTP::Response;
|
||||
use HTTP::Request::Common;
|
||||
use HTTP::Request::Common qw(DELETE POST GET HEAD PUT);
|
||||
use HTTP::Status;
|
||||
use URI::Query;
|
||||
use JSON;
|
||||
@@ -75,7 +75,8 @@ sub call_api {
|
||||
}
|
||||
|
||||
# body data
|
||||
my $_body_data = %$post_params ? $post_params : to_json($body_data->to_hash);
|
||||
$body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
|
||||
my $_body_data = %$post_params ? $post_params : $body_data;
|
||||
|
||||
# Make the HTTP request
|
||||
my $_request;
|
||||
@@ -84,6 +85,7 @@ sub call_api {
|
||||
# multipart
|
||||
my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
|
||||
'form-data' : $header_params->{'Content-Type'};
|
||||
|
||||
$_request = POST($_url, Accept => $header_params->{Accept},
|
||||
Content_Type => $_content_type, Content => $_body_data);
|
||||
}
|
||||
@@ -98,9 +100,17 @@ sub call_api {
|
||||
$_request = GET($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'});
|
||||
}
|
||||
case 'PUT' {
|
||||
|
||||
case 'HEAD' {
|
||||
$_request = HEAD($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'});
|
||||
}
|
||||
case 'DELETE' { #TODO support form data
|
||||
$_request = DELETE($_url, Accept => $header_params->{'Accept'},
|
||||
Content_Type => $header_params->{'Content-Type'}, Content => $_body_data);
|
||||
}
|
||||
case 'PATCH' { #TODO
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$ua->timeout($http_timeout);
|
||||
@@ -109,8 +119,7 @@ sub call_api {
|
||||
my $_response = $ua->request($_request);
|
||||
|
||||
unless ($_response->is_success) {
|
||||
croak("Can't connect to the server");
|
||||
#croak("Can't connect to the api ($_response{code}): $_response{message}");
|
||||
croak("API Exception(".$_response->code."): ".$_response->message);
|
||||
}
|
||||
|
||||
return $_response->content;
|
||||
|
||||
@@ -385,10 +385,14 @@ sub new {
|
||||
}
|
||||
# form params
|
||||
if ( exists $args->{'name'} ) {
|
||||
|
||||
$form_params->{'name'} = WWW::SwaggerClient::APIClient::to_form_value($args->{'name'});
|
||||
|
||||
} # form params
|
||||
if ( exists $args->{'status'} ) {
|
||||
|
||||
$form_params->{'status'} = WWW::SwaggerClient::APIClient::to_form_value($args->{'status'});
|
||||
|
||||
}
|
||||
my $_body_data;
|
||||
|
||||
@@ -513,10 +517,15 @@ sub new {
|
||||
}
|
||||
# form params
|
||||
if ( exists $args->{'additional_metadata'} ) {
|
||||
|
||||
$form_params->{'additionalMetadata'} = WWW::SwaggerClient::APIClient::to_form_value($args->{'additional_metadata'});
|
||||
|
||||
} # form params
|
||||
if ( exists $args->{'file'} ) {
|
||||
$form_params->{'file'} = '@' . WWW::SwaggerClient::APIClient::to_form_value($args->{'file'});
|
||||
$form_params->{'file'} = [] unless defined $form_params->{'file'};
|
||||
push $form_params->{'file'}, $args->{'file'};
|
||||
|
||||
|
||||
}
|
||||
my $_body_data;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
#
|
||||
|
||||
use lib 'lib';
|
||||
use strict;
|
||||
use warnings;
|
||||
use WWW::SwaggerClient::PetApi;
|
||||
@@ -15,46 +15,26 @@ use DateTime;
|
||||
|
||||
my $api = WWW::SwaggerClient::PetApi->new();
|
||||
|
||||
#print WWW::SwaggerClient::APIClient::to_form_value('testing 123');
|
||||
|
||||
my $pet_id = 88;
|
||||
my $dt = DateTime->new(
|
||||
year => 1972,
|
||||
month => 12,
|
||||
day => 31,
|
||||
hour => 23,
|
||||
minute => 59,
|
||||
second => 30,
|
||||
time_zone => 'UTC'
|
||||
);
|
||||
|
||||
print "dt = $dt\n";
|
||||
print "dt = ".$dt->datetime()."\n";
|
||||
|
||||
|
||||
my $category = WWW::SwaggerClient::Object::Category->new({'id' => '2', 'name' => 'pending'});
|
||||
my $tag = WWW::SwaggerClient::Object::Tag->new({'id' => '1', 'name' => 'just kidding',
|
||||
"photoUrls" => ['123', 'oop']});
|
||||
my $category = WWW::SwaggerClient::Object::Category->new({'id' => '2', 'name' => 'perl'});
|
||||
my $tag = WWW::SwaggerClient::Object::Tag->new({'id' => '1', 'name' => 'just kidding'});
|
||||
my $pet = WWW::SwaggerClient::Object::Pet->new({'id' => $pet_id, 'name' => 'perl test',
|
||||
"photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'status' => 'pending'});
|
||||
"photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'status' => 'pending', 'category' => $category});
|
||||
|
||||
print Dumper $pet;
|
||||
print "\npet(object)=".Dumper $pet;
|
||||
my $json = JSON->new->convert_blessed;
|
||||
|
||||
#print $json->convert_blessed->encode($pet);
|
||||
#print $json->get_convert_blessed;
|
||||
print Dumper($pet->to_hash);
|
||||
#my $pet2 = WWW::SwaggerClient::Model::Pet->from_json($pet->to_hash);
|
||||
my $pet2 = WWW::SwaggerClient::Object::Pet->new();
|
||||
$pet2 = $pet2->from_hash($pet->to_hash);
|
||||
#$pet2->from_json($pet->to_hash);
|
||||
print Dumper($pet2->to_hash);
|
||||
#exit;
|
||||
print "============================\n";
|
||||
print Dumper $api->add_pet({body => $pet});
|
||||
#print Dumper $api->get_pet_by_id({pet_id => $pet_id});
|
||||
#print Dumper $api->update_pet_with_form({pet_id => $pet_id, name => 'test_name', status => 'test status'});
|
||||
#print Dumper $api->delete_pet({pet_id => $pet_id});
|
||||
my $new_pet = WWW::SwaggerClient::Object::Pet->new();
|
||||
$new_pet = $new_pet->from_hash($pet->to_hash);
|
||||
print "new_pet(hash):".Dumper($new_pet->to_hash);
|
||||
|
||||
print "\nTest Petstore endpoints\n";
|
||||
#print "\nupload_file:".Dumper $api->upload_file({pet_id => $pet_id, additional_metadata => 'testabc', file => '/var/tmp/f5.jpg'});
|
||||
print "\nadd_pet:".Dumper $api->add_pet({body => $pet});
|
||||
print "\nget_pet_by_id:".Dumper $api->get_pet_by_id({pet_id => $pet_id});
|
||||
print "\nupdate_pet_with_form:".Dumper $api->update_pet_with_form({pet_id => $pet_id, name => 'test_name', status => 'test status'});
|
||||
print "\ndelete_pet:".Dumper $api->delete_pet({pet_id => $pet_id});
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user