Merge branch 'develop_2.0' of github.com:swagger-api/swagger-codegen into develop_2.0

This commit is contained in:
Tony Tam
2015-03-16 20:54:14 -07:00
26 changed files with 1702 additions and 126 deletions

View File

@@ -0,0 +1,120 @@
package com.wordnik.swagger.codegen.languages;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.models.properties.*;
import java.util.*;
import java.io.File;
public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "com.wordnik.client";
protected String groupId = "com.wordnik";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
public CodegenType getTag() {
return CodegenType.CLIENT;
}
public String getName() {
return "ruby";
}
public String getHelp() {
return "Generates a Ruby client library.";
}
public RubyClientCodegen() {
super();
modelPackage = "models";
apiPackage = "lib";
outputFolder = "generated-code/ruby";
modelTemplateFiles.put("model.mustache", ".rb");
apiTemplateFiles.put("api.mustache", ".rb");
templateDir = "ruby";
typeMapping.clear();
languageSpecificPrimitives.clear();
reservedWords = new HashSet<String> (
Arrays.asList(
"int")
);
additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);
languageSpecificPrimitives.add("int");
languageSpecificPrimitives.add("array");
languageSpecificPrimitives.add("map");
languageSpecificPrimitives.add("string");
languageSpecificPrimitives.add("DateTime");
typeMapping.put("long", "int");
typeMapping.put("integer", "int");
typeMapping.put("Array", "array");
typeMapping.put("String", "string");
typeMapping.put("List", "array");
typeMapping.put("map", "map");
supportingFiles.add(new SupportingFile("swagger.mustache", "", "lib/swagger.rb"));
supportingFiles.add(new SupportingFile("monkey.mustache", "", "lib/monkey.rb"));
supportingFiles.add(new SupportingFile("swagger/request.mustache", "", "lib/swagger/request.rb"));
supportingFiles.add(new SupportingFile("swagger/response.mustache", "", "lib/swagger/response.rb"));
supportingFiles.add(new SupportingFile("swagger/version.mustache", "", "lib/swagger/version.rb"));
supportingFiles.add(new SupportingFile("swagger/configuration.mustache", "", "lib/swagger/configuration.rb"));
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
}
@Override
public String apiFileFolder() {
return outputFolder + "/" + apiPackage().replace('.', File.separatorChar);
}
public String modelFileFolder() {
return outputFolder + "/" + modelPackage().replace('.', File.separatorChar);
}
@Override
public String getTypeDeclaration(Property p) {
if(p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getSwaggerType(p) + "[" + getTypeDeclaration(inner) + "]";
}
else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return getSwaggerType(p) + "[string," + getTypeDeclaration(inner) + "]";
}
return super.getTypeDeclaration(p);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type = null;
if(typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if(languageSpecificPrimitives.contains(type)) {
return type;
}
}
else
type = swaggerType;
if(type == null)
return null;
return type;
}
public String toDefaultValue(Property p) {
return "null";
}
}

View File

@@ -11,4 +11,5 @@ com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
com.wordnik.swagger.codegen.languages.SwaggerGenerator
com.wordnik.swagger.codegen.languages.TizenClientCodegen
com.wordnik.swagger.codegen.languages.PhpClientCodegen
com.wordnik.swagger.codegen.languages.RubyClientCodegen
com.wordnik.swagger.codegen.languages.PythonClientCodegen

View File

@@ -48,6 +48,18 @@ class APIClient {
$this->headerValue = $headerValue;
}
/**
* Set the user agent of the API client
*
* @param string $user_agent The user agent of the API client
*/
public function setUserAgent($user_agent) {
if (!is_string($user_agent)) {
throw new Exception('User-agent must be a string.');
}
$this->user_agent= $user_agent;
}
/**
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
*/
@@ -58,7 +70,6 @@ class APIClient {
$this->curl_timout = $seconds;
}
/**
* @param string $resourcePath path to method endpoint
* @param string $method method to call
@@ -121,6 +132,13 @@ class APIClient {
}
curl_setopt($curl, CURLOPT_URL, $url);
// Set user agent
if ($this->user_agent) {
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
} else { // use PHP-Swagger as the default user agent
curl_setopt($curl, CURLOPT_USERAGENT, 'PHP-Swagger');
}
// Make the request
$response = curl_exec($curl);
$response_info = curl_getinfo($curl);
@@ -129,11 +147,14 @@ class APIClient {
if ($response_info['http_code'] == 0) {
throw new APIClientException("TIMEOUT: api call to " . $url .
" took more than 5s to return", 0, $response_info, $response);
} else if ($response_info['http_code'] == 200) {
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response);
if (json_last_error() > 0) { // if response is a string
$data = $response;
}
} else if ($response_info['http_code'] == 401) {
throw new APIClientException("Unauthorized API request to " . $url .
": " . json_decode($response)->message, 0, $response_info, $response);
": " . serialize($response), 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) {
$data = null;
} else {
@@ -178,7 +199,7 @@ class APIClient {
* @return string the serialized object
*/
public static function toPathValue($value) {
return rawurlencode($value);
return rawurlencode(toString($value));
}
/**
@@ -193,20 +214,48 @@ class APIClient {
if (is_array($object)) {
return implode(',', $object);
} else {
return $object;
return toString($object);
}
}
/**
* Just pass through the header value for now. Placeholder in case we
* find out we need to do something with header values.
* Take value and turn it into a string suitable for inclusion in
* the header. If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
* @param string $value a string which will be part of the header
* @return string the header string
*/
public static function toHeaderValue($value) {
return $value;
return toString($value);
}
/**
* Take value and turn it into a string suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
* @param string $value the value of the form parameter
* @return string the form string
*/
public static function toFormValue($value) {
return toString($value);
}
/**
* Take value and turn it into a string suitable for inclusion in
* the parameter. If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
* @param string $value the value of the parameter
* @return string the header string
*/
public static function toString($value) {
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ISO8601);
}
else {
return $value;
}
}
/**
* Deserialize a JSON string into an object
*

View File

@@ -63,7 +63,7 @@ class {{classname}} {
}{{/pathParams}}
{{#formParams}}// form params
if (${{paramName}} !== null) {
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}${{paramName}};
$formParams['{{baseName}}'] = {{#isFile}}'@' . {{/isFile}}$this->apiClient->toFormValue(${{paramName}});
}{{/formParams}}
{{#bodyParams}}// body params
$body = null;

View File

@@ -7,9 +7,8 @@ class {{classname}}
def self.attribute_map
{
{{#vars}}
:{{{name}}} => :{{{baseName}}}{{#hasMore}},
{{/hasMore}}
{{/vars}}{{newline}}
:{{{name}}} => :{{{baseName}}}{{#hasMore}},{{/hasMore}}
{{/vars}}
}
end
@@ -19,14 +18,14 @@ class {{classname}}
{{#vars}}
if self.class.attribute_map[:"{{{name}}}"]
{{#isContainer}}
if (value = attributes["{{{baseName}}}"]).is_a?(Array)
@{{{name}}} = value{{#complexType}}.map{ |v| {{complexType}}.new(v) }{{/complexType}}{{newline}}
end
if (value = attributes["{{{baseName}}}"]).is_a?(Array)
@{{{name}}} = value{{#complexType}}.map{ |v| {{complexType}}.new(v) }{{/complexType}}{{newline}}
end
{{/isContainer}}{{^isContainer}}
@{{{name}}} = attributes["{{{baseName}}}"]
{{/isContainer}}
@{{{name}}} = attributes["{{{baseName}}}"]
{{/isContainer}}
end
{{/vars}}{{newline}}
{{/vars}}
end
def to_body