Skip to content

Commit

Permalink
Generate ruby client using openapi-generator-cli 5.3.0
Browse files Browse the repository at this point in the history
closes: pulp#55
  • Loading branch information
dkliban committed Dec 6, 2021
1 parent 48235a6 commit c7fbadb
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 18 deletions.
2 changes: 1 addition & 1 deletion generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fi
if [ $2 = 'ruby' ]
then
python3 remove-cookie-auth.py
$container_exec run -u $(id -u) --rm -v ${PWD}:$volume_name openapitools/openapi-generator-cli:v4.3.1 generate \
$container_exec run -u $(id -u) --rm -v ${PWD}:$volume_name openapitools/openapi-generator-cli:v5.3.0 generate \
-i /local/api.json \
-g ruby \
-o /local/$1-client \
Expand Down
19 changes: 13 additions & 6 deletions templates/ruby/api_client_faraday_partial.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
:params_encoder => @config.params_encoder
}
connection = Faraday.new(:url => config.base_url, :ssl => ssl_options, :request => request_options) do |conn|
conn.basic_auth(config.username, config.password)
conn.request(:basic_auth, config.username, config.password)
@config.configure_middleware(conn)
if opts[:header_params]["Content-Type"] == "multipart/form-data"
conn.request :multipart
conn.request :url_encoded
Expand Down Expand Up @@ -65,7 +66,7 @@
# @option opts [Object] :body HTTP body (JSON/XML)
# @return [Typhoeus::Request] A Typhoeus Request
def build_request(http_method, path, request, opts = {})
url = build_request_url(path)
url = build_request_url(path, opts)
http_method = http_method.to_sym.downcase

header_params = @default_headers.merge(opts[:header_params] || {})
Expand All @@ -75,23 +76,20 @@
update_params_for_auth! header_params, query_params, opts[:auth_names]

req_opts = {
:method => http_method,
:headers => header_params,
:params => query_params,
:params_encoding => @config.params_encoding,
:timeout => @config.timeout,
:verbose => @config.debugging
}

if [:post, :patch, :put, :delete].include?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update :body => req_body
if @config.debugging
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
end
request.headers = header_params
request.body = req_body
request.options = OpenStruct.new(req_opts)
request.url url
request.params = query_params
download_file(request) if opts[:return_type] == 'File'
Expand Down Expand Up @@ -129,3 +127,12 @@
end
data
end

def download_file(request)
@stream = []

# handle streaming Responses
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
@stream << chunk
end
end
128 changes: 117 additions & 11 deletions templates/ruby/configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ module {{moduleName}}
# Defines url base path
attr_accessor :base_path

# Define server configuration index
attr_accessor :server_index

# Define server operation configuration index
attr_accessor :server_operation_index

# Default server variables
attr_accessor :server_variables

# Default server operation variables
attr_accessor :server_operation_variables

# Defines API keys used with API Key authentications.
#
# @return [Hash] key: parameter name, value: parameter value (API key)
Expand Down Expand Up @@ -90,11 +102,14 @@ module {{moduleName}}

def initialize
@scheme = '{{scheme}}'
@host = '{{host}}'
@host = '{{host}}{{#port}}:{{{.}}}{{/port}}'
@base_path = '{{contextPath}}'
@server_index = 0
@server_operation_index = {}
@server_variables = {}
@server_operation_variables = {}
@api_key = {}
@api_key_prefix = {}
@timeout = 0
@client_side_validation = true
{{#isFaraday}}
@ssl_verify = true
Expand All @@ -103,13 +118,18 @@ module {{moduleName}}
@ssl_client_cert = nil
@ssl_client_key = nil
@params_encoder = nil
@middlewares = []
@request_middlewares = []
@response_middlewares = []
@timeout = 60
{{/isFaraday}}
{{^isFaraday}}
@verify_ssl = true
@verify_ssl_host = true
@params_encoding = nil
@cert_file = nil
@key_file = nil
@timeout = 0
{{/isFaraday}}
@debugging = false
@inject_format = false
Expand Down Expand Up @@ -144,17 +164,23 @@ module {{moduleName}}
@base_path = '' if @base_path == '/'
end

def base_url
"#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
index = server_operation_index.fetch(operation, server_index)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil

server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
end

# Gets API key (with prefix if set).
# @param [String] param_name the parameter name of API key auth
def api_key_with_prefix(param_name)
def api_key_with_prefix(param_name, param_alias = nil)
key = @api_key[param_name]
key = @api_key.fetch(param_alias, key) unless param_alias.nil?
if @api_key_prefix[param_name]
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
"#{@api_key_prefix[param_name]} #{key}"
else
@api_key[param_name]
key
end
end

Expand All @@ -173,7 +199,7 @@ module {{moduleName}}
type: 'api_key',
in: {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
key: '{{keyParamName}}',
value: api_key_with_prefix('{{keyParamName}}')
value: api_key_with_prefix('{{name}}'{{#vendorExtensions.x-auth-id-alias}}, '{{.}}'{{/vendorExtensions.x-auth-id-alias}})
},
{{/isApiKey}}
{{#isBasic}}
Expand Down Expand Up @@ -245,12 +271,58 @@ module {{moduleName}}
]
end

def operation_server_settings
{
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
{{#servers}}
{{#-first}}
"{{{classname}}}.{{{nickname}}}": [
{{/-first}}
{
url: "{{{url}}}",
description: "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
variables: {
{{/-first}}
{{{name}}}: {
description: "{{{description}}}{{^description}}No description provided{{/description}}",
default_value: "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
enum_values: [
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
]
{{/-last}}
{{/enumValues}}
}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}
{{/variables}}
}{{^-last}},{{/-last}}
{{#-last}}
],
{{/-last}}
{{/servers}}
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
}
end

# Returns URL based on server settings
#
# @param index array index of the server settings
# @param variables hash of variable and the corresponding value
def server_url(index, variables = {})
servers = server_settings
def server_url(index, variables = {}, servers = nil)
servers = server_settings if servers == nil

# check array index out of bound
if (index < 0 || index >= servers.size)
Expand All @@ -260,10 +332,12 @@ module {{moduleName}}
server = servers[index]
url = server[:url]

return url unless server.key? :variables

# go through variable and assign a value
server[:variables].each do |name, variable|
if variables.key?(name)
if (server[:variables][name][:enum_values].include? variables[name])
if (!server[:variables][name].key?(:enum_values) || server[:variables][name][:enum_values].include?(variables[name]))
url.gsub! "{" + name.to_s + "}", variables[name]
else
fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}."
Expand All @@ -276,5 +350,37 @@ module {{moduleName}}

url
end

{{#isFaraday}}
# Adds middleware to the stack
def use(*middleware)
@middlewares << middleware
end

# Adds request middleware to the stack
def request(*middleware)
@request_middlewares << middleware
end

# Adds response middleware to the stack
def response(*middleware)
@response_middlewares << middleware
end

# Set up middleware on the connection
def configure_middleware(connection)
@middlewares.each do |middleware|
connection.use(*middleware)
end

@request_middlewares.each do |middleware|
connection.request(*middleware)
end

@response_middlewares.each do |middleware|
connection.response(*middleware)
end
end
{{/isFaraday}}
end
end

0 comments on commit c7fbadb

Please sign in to comment.