add support for cli (perl)

This commit is contained in:
wing328 2015-06-15 15:44:23 +08:00
parent 6ad3a717fe
commit 10e07eaf74
11 changed files with 219 additions and 34 deletions

View File

@ -7,16 +7,16 @@ import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.codegen.CliOption;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "SwaggerClient";
protected String groupId = "io.swagger";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
protected String moduleName = "SwaggerClient";
protected String moduleVersion = "1.0.0";
public PerlClientCodegen() {
super();
@ -26,8 +26,6 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
apiTemplateFiles.put("api.mustache", ".pm");
templateDir = "perl";
typeMapping.clear();
languageSpecificPrimitives.clear();
reservedWords = new HashSet<String>(
Arrays.asList(
@ -44,11 +42,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
)
);
additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);
languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("int");
languageSpecificPrimitives.add("double");
languageSpecificPrimitives.add("string");
@ -58,6 +52,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
languageSpecificPrimitives.add("HASH");
languageSpecificPrimitives.add("object");
typeMapping.clear();
typeMapping.put("integer", "int");
typeMapping.put("long", "int");
typeMapping.put("float", "double");
@ -71,9 +66,31 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("map", "HASH");
typeMapping.put("object", "object");
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "ApiClient.pm"));
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "Configuration.pm"));
supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "Object/BaseObject.pm"));
cliOptions.clear();
cliOptions.add(new CliOption("moduleName", "perl module name, default: SwaggerClient"));
cliOptions.add(new CliOption("moduleVersion", "perl module version, default: 1.0.0"));
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey("moduleVersion")) {
moduleVersion = (String) additionalProperties.get("moduleVersion");
} else {
additionalProperties.put("moduleVersion", moduleVersion);
}
if (additionalProperties.containsKey("moduleName")) {
moduleName = (String) additionalProperties.get("moduleName");
} else {
additionalProperties.put("moduleName", moduleName);
}
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiClient.pm"));
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm"));
supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Object/BaseObject.pm"));
}
public CodegenType getTag() {
@ -95,11 +112,11 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override
public String apiFileFolder() {
return (outputFolder + "/lib/WWW/" + invokerPackage + apiPackage()).replace('/', File.separatorChar);
return (outputFolder + "/lib/WWW/" + moduleName + apiPackage()).replace('/', File.separatorChar);
}
public String modelFileFolder() {
return (outputFolder + "/lib/WWW/" + invokerPackage + modelPackage()).replace('/', File.separatorChar);
return (outputFolder + "/lib/WWW/" + moduleName + modelPackage()).replace('/', File.separatorChar);
}
@Override

View File

@ -1,4 +1,4 @@
package WWW::{{invokerPackage}}::ApiClient;
package WWW::{{moduleName}}::ApiClient;
use strict;
use warnings;
@ -18,7 +18,7 @@ use Log::Any qw($log);
use Carp;
use Module::Runtime qw(use_module);
use WWW::{{invokerPackage}}::Configuration;
use WWW::{{moduleName}}::Configuration;
sub new
{
@ -115,8 +115,8 @@ sub call_api {
else {
}
$self->{ua}->timeout($self->{http_timeout} || $WWW::{{invokerPackage}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{invokerPackage}}::Configuration::http_user_agent);
$self->{ua}->timeout($self->{http_timeout} || $WWW::{{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{moduleName}}::Configuration::http_user_agent);
my $_response = $self->{ua}->request($_request);
@ -237,7 +237,7 @@ sub deserialize
} elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) {
return $data;
} else { # model
my $_instance = use_module("WWW::SwaggerClient::Object::$class")->new;
my $_instance = use_module("WWW::{{moduleName}}::Object::$class")->new;
if (ref $data eq "HASH") {
return $_instance->from_hash($data);
} else { # string, need to json decode first
@ -287,10 +287,10 @@ sub select_header_content_type
sub get_api_key_with_prefix
{
my ($self, $api_key) = @_;
if ($WWW::{{invokerPackage}}::Configuration::api_key_prefix->{$api_key}) {
return $WWW::{{invokerPackage}}::Configuration::api_key_prefix->{$api_key}." ".$WWW::{{invokerPackage}}::Configuration::api_key->{$api_key};
if ($WWW::{{moduleName}}::Configuration::api_key_prefix->{$api_key}) {
return $WWW::{{moduleName}}::Configuration::api_key_prefix->{$api_key}." ".$WWW::{{moduleName}}::Configuration::api_key->{$api_key};
} else {
return $WWW::{{invokerPackage}}::Configuration::api_key->{$api_key};
return $WWW::{{moduleName}}::Configuration::api_key->{$api_key};
}
}
@ -310,7 +310,7 @@ sub update_params_for_auth {
if (!defined($auth)) {
}
{{#authMethods}}elsif ($auth eq '{{name}}') {
{{#isApiKey}}{{#isKeyInHeader}}$header_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$query_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$header_params->{'Authorization'} = 'Basic '.encode_base64($WWW::{{invokerPackage}}::Configuration::username.":".$WWW::{{invokerPackage}}::Configuration::password);{{/isBasic}}
{{#isApiKey}}{{#isKeyInHeader}}$header_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$query_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$header_params->{'Authorization'} = 'Basic '.encode_base64($WWW::{{moduleName}}::Configuration::username.":".$WWW::{{moduleName}}::Configuration::password);{{/isBasic}}
{{#isOAuth}}# TODO support oauth{{/isOAuth}}
}
{{/authMethods}}

View File

@ -1,4 +1,4 @@
package WWW::{{invokerPackage}}::Object::BaseObject;
package WWW::{{moduleName}}::Object::BaseObject;
require 5.6.0;
use strict;
@ -68,7 +68,7 @@ sub _deserialize {
} elsif ( grep( /^$type$/, ('int', 'double', 'string', 'boolean'))) {
return $data;
} else { # hash(model)
my $_instance = eval "WWW::{{invokerPackage}}::Object::$type->new()";
my $_instance = eval "WWW::{{moduleName}}::Object::$type->new()";
return $_instance->from_hash($data);
}
}

View File

@ -1,4 +1,4 @@
package WWW::{{invokerPackage}}::Configuration;
package WWW::{{moduleName}}::Configuration;
use strict;
use warnings;
@ -7,6 +7,8 @@ use utf8;
use Log::Any qw($log);
use Carp;
use constant VERSION => '{{moduleVersion}}';
# class/static variables
our $api_client;
our $http_timeout = 180;

View File

@ -17,7 +17,7 @@
# NOTE: This class is auto generated by the swagger code generator program.
# Do not edit the class manually.
#
package WWW::{{invokerPackage}}::{{classname}};
package WWW::{{moduleName}}::{{classname}};
require 5.6.0;
use strict;
@ -27,8 +27,8 @@ use Exporter;
use Carp qw( croak );
use Log::Any qw($log);
use WWW::{{invokerPackage}}::ApiClient;
use WWW::{{invokerPackage}}::Configuration;
use WWW::{{moduleName}}::ApiClient;
use WWW::{{moduleName}}::Configuration;
{{#operations}}
our @EXPORT_OK = qw(
@ -38,7 +38,7 @@ our @EXPORT_OK = qw(
sub new {
my $class = shift;
my $default_api_client = $WWW::{{invokerPackage}}::Configuration::api_client ? $WWW::{{invokerPackage}}::Configuration::api_client : WWW::{{invokerPackage}}::ApiClient->new;
my $default_api_client = $WWW::{{moduleName}}::Configuration::api_client ? $WWW::{{moduleName}}::Configuration::api_client : WWW::{{moduleName}}::ApiClient->new;
my (%self) = (
'api_client' => $default_api_client,
@_

View File

@ -1,6 +1,6 @@
{{#models}}
{{#model}}
package WWW::{{invokerPackage}}::Object::{{classname}};
package WWW::{{moduleName}}::Object::{{classname}};
require 5.6.0;
use strict;
@ -13,7 +13,7 @@ use Log::Any qw($log);
use Date::Parse;
use DateTime;
use base "WWW::{{invokerPackage}}::Object::BaseObject";
use base "WWW::{{moduleName}}::Object::BaseObject";
#
#{{description}}

View File

@ -0,0 +1,61 @@
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace IO.Swagger.Model {
/// <summary>
///
/// </summary>
[DataContract]
public class ApiResponse {
[DataMember(Name="code", EmitDefaultValue=false)]
public int? Code { get; set; }
[DataMember(Name="type", EmitDefaultValue=false)]
public string Type { get; set; }
[DataMember(Name="message", EmitDefaultValue=false)]
public string Message { get; set; }
/// <summary>
/// Get the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString() {
var sb = new StringBuilder();
sb.Append("class ApiResponse {\n");
sb.Append(" Code: ").Append(Code).Append("\n");
sb.Append(" Type: ").Append(Type).Append("\n");
sb.Append(" Message: ").Append(Message).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Get the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson() {
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
}
}

View File

@ -0,0 +1,64 @@
package io.swagger.client.model;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel(description = "")
public class ApiResponse {
private Integer code = null;
private String type = null;
private String message = null;
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("code")
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("message")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ApiResponse {\n");
sb.append(" code: ").append(code).append("\n");
sb.append(" type: ").append(type).append("\n");
sb.append(" message: ").append(message).append("\n");
sb.append("}\n");
return sb.toString();
}
}

View File

@ -0,0 +1,17 @@
#import <Foundation/Foundation.h>
#import "SWGObject.h"
@protocol SWGApiResponse
@end
@interface SWGApiResponse : SWGObject
@property(nonatomic) NSNumber* code;
@property(nonatomic) NSString* type;
@property(nonatomic) NSString* message;
@end

View File

@ -0,0 +1,22 @@
#import "SWGApiResponse.h"
@implementation SWGApiResponse
+ (JSONKeyMapper *)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{ @"code": @"code", @"type": @"type", @"message": @"message" }];
}
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
NSArray *optionalProperties = @[@"code", @"type", @"message"];
if ([optionalProperties containsObject:propertyName]) {
return YES;
}
else {
return NO;
}
}
@end

View File

@ -7,6 +7,8 @@ use utf8;
use Log::Any qw($log);
use Carp;
use constant VERSION => '1.0.0';
# class/static variables
our $api_client;
our $http_timeout = 180;