make api client pluggable

This commit is contained in:
wing328
2015-05-27 17:56:39 +08:00
parent 095771d345
commit 866b546cfd
10 changed files with 166 additions and 267 deletions

View File

@@ -15,20 +15,16 @@ use URI::Escape;
use Scalar::Util;
use Log::Any qw($log);
use Carp;
use Switch;
use Module::Runtime qw(use_module);
# class variables
my $ua = LWP::UserAgent->new;
my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent
my $http_timeout; #timeout
my $base_url = "http://petstore.swagger.io/v2";
sub new
{
my $class = shift;
my %args = @_;
my (%args) = (
'ua' => LWP::UserAgent->new,
'base_url' => 'http://petstore.swagger.io/v2',
@_
);
return bless \%args, $class;
}
@@ -38,8 +34,8 @@ sub new
# @param string $user_agent The user agent of the API client
#
sub set_user_agent {
my $user_agent = shift;
$http_user_agent= $user_agent;
my ($self, $user_agent) = @_;
$self->{http_user_agent}= $user_agent;
}
# Set timeout
@@ -47,11 +43,11 @@ sub set_user_agent {
# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
#
sub set_timeout {
my $seconds = shift;
my ($self, $seconds) = @_;
if (!looks_like_number($seconds)) {
croak('Timeout variable must be numeric.');
}
$http_timeout = $seconds;
$self->{http_timeout} = $seconds;
}
# make the HTTP request
@@ -67,7 +63,7 @@ sub call_api {
my $headers = HTTP::Headers->new(%$header_params);
my $_url = $base_url . $resource_path;
my $_url = $self->{base_url} . $resource_path;
# build query
if (%$query_params) {
@@ -80,43 +76,42 @@ sub call_api {
# Make the HTTP request
my $_request;
switch ($method) {
case 'POST' {
if ($method eq 'POST') {
# 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);
}
case 'PUT' {
}
elsif ($method eq 'PUT') {
# multipart
my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
'form-data' : $header_params->{'Content-Type'};
$_request = PUT($_url, Accept => $header_params->{Accept},
Content_Type => $_content_type, Content => $_body_data);
}
case 'GET' {
}
elsif ($method eq 'GET') {
$_request = GET($_url, Accept => $header_params->{'Accept'},
Content_Type => $header_params->{'Content-Type'});
}
case 'HEAD' {
}
elsif ($method eq 'HEAD') {
$_request = HEAD($_url, Accept => $header_params->{'Accept'},
Content_Type => $header_params->{'Content-Type'});
}
case 'DELETE' { #TODO support form data
}
elsif ($method eq 'DELETE') { #TODO support form data
$_request = DELETE($_url, Accept => $header_params->{'Accept'},
Content_Type => $header_params->{'Content-Type'}, Content => $_body_data);
}
case 'PATCH' { #TODO
}
}
elsif ($method eq 'PATCH') { #TODO
}
else {
}
$ua->timeout($http_timeout);
$ua->agent($http_user_agent);
$self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent);
my $_response = $ua->request($_request);
my $_response = $self->{ua}->request($_request);
unless ($_response->is_success) {
croak("API Exception(".$_response->code."): ".$_response->message);
@@ -126,41 +121,13 @@ sub call_api {
}
# Build a JSON POST object
sub sanitize_for_serialization
{
# my $data = shift;
# if (is_scalar($data) || null === $data) {
# $sanitized = $data;
# } else if ($data instanceof \DateTime) {
# $sanitized = $data->format(\DateTime::ISO8601);
# } else if (is_array($data)) {
# foreach ($data as $property => $value) {
# $data[$property] = $this->sanitizeForSerialization($value);
# }
# $sanitized = $data;
# } else if (is_object($data)) {
# $values = array();
# foreach (array_keys($data::$swaggerTypes) as $property) {
# $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
# }
# $sanitized = $values;
# } else {
# $sanitized = (string)$data;
# }
#
# return $sanitized;
}
# Take value and turn it into a string suitable for inclusion in
# the path, by url-encoding.
# @param string $value a string which will be part of the path
# @return string the serialized object
sub to_path_value {
my $value = shift;
return uri_escape(to_string($value));
my ($self, $value) = @_;
return uri_escape($self->to_string($value));
}
@@ -171,11 +138,11 @@ sub to_path_value {
# @param object $object an object to be serialized to a string
# @return string the serialized object
sub to_query_value {
my $object = shift;
my ($self, $object) = @_;
if (is_array($object)) {
return implode(',', $object);
} else {
return toString($object);
return $self->to_string($object);
}
}
@@ -186,8 +153,8 @@ sub to_query_value {
# @param string $value a string which will be part of the header
# @return string the header string
sub to_header_value {
my $value = shift;
return to_string($value);
my ($self, $value) = @_;
return $self->to_string($value);
}
# Take value and turn it into a string suitable for inclusion in
@@ -196,8 +163,8 @@ sub to_header_value {
# @param string $value the value of the form parameter
# @return string the form string
sub to_form_value {
my $value = shift;
return to_string($value);
my ($self, $value) = @_;
return $self->to_string($value);
}
# Take value and turn it into a string suitable for inclusion in
@@ -206,7 +173,7 @@ sub to_form_value {
# @param string $value the value of the parameter
# @return string the header string
sub to_string {
my $value = shift;
my ($self, $value) = @_;
if (ref($value) eq "DateTime") { # datetime in ISO8601 format
return $value->datetime();
}

View File

@@ -0,0 +1,16 @@
package WWW::SwaggerClient::Configuration;
use strict;
use warnings;
use utf8;
use Log::Any qw($log);
use Carp;
# class/static variables
my $api_client = WWW::SwaggerClient::APIClient->new;
my $http_timeout = 180;
my $http_user_agent = 'Perl-Swagger';
1;

View File

@@ -28,9 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log);
#use WWW::Swagger::Model::Category;
#use WWW::Swagger::Model::Pet;
use WWW::SwaggerClient::APIClient;
@@ -91,21 +88,18 @@ sub new {
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', );
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml');
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -142,21 +136,18 @@ sub new {
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', );
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml');
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -195,9 +186,9 @@ sub new {
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
# query params
if ( exists $args{'status'}) {
$query_params->{'status'} = WWW::::APIClient::to_query_value($args{'status'});
$query_params->{'status'} = $self->{api_client}->to_query_value($args{'status'});
}
@@ -205,9 +196,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -249,9 +237,9 @@ sub new {
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
# query params
if ( exists $args{'tags'}) {
$query_params->{'tags'} = WWW::::APIClient::to_query_value($args{'tags'});
$query_params->{'tags'} = $self->{api_client}->to_query_value($args{'tags'});
}
@@ -259,9 +247,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -310,19 +295,16 @@ sub new {
# path params
# path params
if ( exists $args{'pet_id'}) {
my $_base_variable = "{" . "petId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -369,33 +351,30 @@ sub new {
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded', );
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded');
# path params
# path params
if ( exists $args{'pet_id'}) {
my $_base_variable = "{" . "petId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
# form params
# form params
if ( exists $args{'name'} ) {
$form_params->{'name'} = WWW::SwaggerClient::APIClient::to_form_value($args{'name'});
$form_params->{'name'} = $self->{api_client}->to_form_value($args{'name'});
} # form params
}# form params
if ( exists $args{'status'} ) {
$form_params->{'status'} = WWW::SwaggerClient::APIClient::to_form_value($args{'status'});
$form_params->{'status'} = $self->{api_client}->to_form_value($args{'status'});
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -441,23 +420,20 @@ sub new {
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# header params
# header params
if ( exists $args{'api_key'}) {
$header_params->{'api_key'} = WWW::SwaggerClient::APIClient::to_header_value($args{'api_key'});
$header_params->{'api_key'} = $self->{api_client}->to_header_value($args{'api_key'});
}
# path params
# path params
if ( exists $args{'pet_id'}) {
my $_base_variable = "{" . "petId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -501,22 +477,22 @@ sub new {
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data', );
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data');
# path params
# path params
if ( exists $args{'pet_id'}) {
my $_base_variable = "{" . "petId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
# form params
# form params
if ( exists $args{'additional_metadata'} ) {
$form_params->{'additionalMetadata'} = WWW::SwaggerClient::APIClient::to_form_value($args{'additional_metadata'});
$form_params->{'additionalMetadata'} = $self->{api_client}->to_form_value($args{'additional_metadata'});
} # form params
}# form params
if ( exists $args{'file'} ) {
$form_params->{'file'} = [] unless defined $form_params->{'file'};
push $form_params->{'file'}, $args{'file'};
@@ -526,9 +502,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,

View File

@@ -28,9 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log);
#use WWW::Swagger::Model::Category;
#use WWW::Swagger::Model::Pet;
use WWW::SwaggerClient::APIClient;
@@ -95,9 +92,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -144,14 +138,11 @@ sub new {
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -200,19 +191,16 @@ sub new {
# path params
# path params
if ( exists $args{'order_id'}) {
my $_base_variable = "{" . "orderId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'order_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'order_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -261,19 +249,16 @@ sub new {
# path params
# path params
if ( exists $args{'order_id'}) {
my $_base_variable = "{" . "orderId" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'order_id'});
my $_base_value = $self->{api_client}->to_path_value($args{'order_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,

View File

@@ -28,9 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log);
#use WWW::Swagger::Model::Category;
#use WWW::Swagger::Model::Pet;
use WWW::SwaggerClient::APIClient;
@@ -98,14 +95,11 @@ sub new {
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -149,14 +143,11 @@ sub new {
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -200,14 +191,11 @@ sub new {
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -247,12 +235,12 @@ sub new {
}
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
# query params
if ( exists $args{'username'}) {
$query_params->{'username'} = WWW::::APIClient::to_query_value($args{'username'});
} # query params
$query_params->{'username'} = $self->{api_client}->to_query_value($args{'username'});
}# query params
if ( exists $args{'password'}) {
$query_params->{'password'} = WWW::::APIClient::to_query_value($args{'password'});
$query_params->{'password'} = $self->{api_client}->to_query_value($args{'password'});
}
@@ -260,9 +248,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -310,9 +295,6 @@ sub new {
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -358,19 +340,16 @@ sub new {
# path params
# path params
if ( exists $args{'username'}) {
my $_base_variable = "{" . "username" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'});
my $_base_value = $self->{api_client}->to_path_value($args{'username'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
$query_params, $form_params,
@@ -420,22 +399,19 @@ sub new {
# path params
# path params
if ( exists $args{'username'}) {
my $_base_variable = "{" . "username" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'});
my $_base_value = $self->{api_client}->to_path_value($args{'username'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# body params
# body params
if ( exists $args{'body'}) {
$_body_data = $args{'body'};
}
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,
@@ -481,19 +457,16 @@ sub new {
# path params
# path params
if ( exists $args{'username'}) {
my $_base_variable = "{" . "username" . "}";
my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'});
my $_base_value = $self->{api_client}->to_path_value($args{'username'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}
my $_body_data;
# for HTTP post (form)
#$_body_data = $_body ? undef : $form_params;
# make the API Call
$self->{api_client}->call_api($_resource_path, $_method,