update ruby client to support form parameters, add PATCH support, update

ruby petstore client
This commit is contained in:
William Cheng
2015-03-17 05:24:43 +08:00
parent c96853d5aa
commit fc9d632522
6 changed files with 215 additions and 73 deletions

View File

@@ -37,14 +37,15 @@ class {{classname}}
queryopts = options.select do |key,value|
query_param_keys.include? key
end
{{#headerParams}}headers = {
{{{paramName}}}: {{{paramName}}},
}
{{/headerParams}}
{{^headerParams}}headers = nil
{{/headerParams}}
# http body (model)
post_body = nil
{{#bodyParam}}
if body != nil
@@ -69,17 +70,22 @@ class {{classname}}
end
{{/bodyParam}}
# form parameters
form_parameter_hash = {}
{{#formParams}}{{#optional}}form_parameter_hash["{{baseName}}"] = options[:'{{paramName}}'] if options[:'{{paramName}}']{{/optional}}
{{^optional}}form_parameter_hash["{{baseName}}"] = {{paramName}}{{/optional}}{{/formParams}}
{{#returnType}}
response = Swagger::Request.new(:{{httpMethod}}, path, {:params=>queryopts,:headers=>headers, :body=>post_body }).make.body
response = Swagger::Request.new(:{{httpMethod}}, path, {:params=>queryopts,:headers=>headers, :body=>post_body, :form_params => form_parameter_hash }).make.body
{{#returnContainer}}
response.map {|response|{{/returnContainer}} {{returnBaseType}}.new(response){{#returnContainer}} }{{/returnContainer}}
{{/returnType}}
{{^returnType}}
Swagger::Request.new(:{{httpMethod}}, path, {:params=>queryopts,:headers=>headers, :body=>post_body}).make
Swagger::Request.new(:{{httpMethod}}, path, {:params=>queryopts,:headers=>headers, :body=>post_body, :form_params => form_parameter_hash }).make
{{/returnType}}
{{newline}}
end
{{/operation}}
end
{{/operations}}
{{/operations}}

View File

@@ -6,7 +6,7 @@ module Swagger
require 'typhoeus'
require "swagger/version"
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
# All requests must have an HTTP method and a path
@@ -115,9 +115,18 @@ module Swagger
end
# If body is an object, JSONify it before making the actual request.
#
# For form parameters, remove empty value
def outgoing_body
body.is_a?(String) ? body : body.to_json
# http form
if @body.nil? && @form_params && !@form_params.empty?
data = form_params.dup
data.each do |key, value|
data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
end
data
else # http body is JSON
@body.is_a?(String) ? @body : @body.to_json
end
end
# Construct a query string from the query-string-type params
@@ -163,6 +172,13 @@ module Swagger
:headers => self.headers.stringify_keys,
)
when :patch,:PATCH
Typhoeus::Request.patch(
self.url,
:body => self.outgoing_body,
:headers => self.headers.stringify_keys,
)
when :put,:PUT
Typhoeus::Request.put(
self.url,
@@ -196,4 +212,4 @@ module Swagger
end
end
end
end