[crystal][client] Support authorization (#9488)

* [crystal][client] support authorization header

* [crystal][client] update samples

* [crystal][client] update tests

* [crystal][client] add unit test for api_client.cr
This commit is contained in:
Yusuke Hosonuma 2021-05-17 22:24:30 +09:00 committed by GitHub
parent 08467928de
commit 477926f008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 112 additions and 43 deletions

View File

@ -128,9 +128,9 @@ module {{moduleName}}
local_var_path = "{{{path}}}"{{#pathParams}}.sub("{" + "{{baseName}}" + "}", URI.encode({{paramName}}.to_s){{^strictSpecBehavior}}.gsub("%2F", "/"){{/strictSpecBehavior}}){{/pathParams}} local_var_path = "{{{path}}}"{{#pathParams}}.sub("{" + "{{baseName}}" + "}", URI.encode({{paramName}}.to_s){{^strictSpecBehavior}}.gsub("%2F", "/"){{/strictSpecBehavior}}){{/pathParams}}
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
{{#queryParams}} {{#queryParams}}
query_params[:"{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}} query_params["{{{baseName}}}"] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
{{/queryParams}} {{/queryParams}}
# header parameters # header parameters

View File

@ -163,7 +163,7 @@ module {{moduleName}}
# @param [Hash] query_params Query parameters # @param [Hash] query_params Query parameters
# @param [String] auth_names Authentication scheme name # @param [String] auth_names Authentication scheme name
def update_params_for_auth!(header_params, query_params, auth_names) def update_params_for_auth!(header_params, query_params, auth_names)
Array{auth_names}.each do |auth_name| auth_names.each do |auth_name|
auth_setting = @config.auth_settings[auth_name] auth_setting = @config.auth_settings[auth_name]
next unless auth_setting next unless auth_setting
case auth_setting[:in] case auth_setting[:in]
@ -256,7 +256,7 @@ module {{moduleName}}
# #
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
# the data deserialized from response body (could be nil), response status code and response headers. # the data deserialized from response body (could be nil), response status code and response headers.
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of Symbol => String, form_params = {} of Symbol => String) def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of String => String, form_params = {} of Symbol => String)
#ssl_options = { #ssl_options = {
# :ca_file => @config.ssl_ca_file, # :ca_file => @config.ssl_ca_file,
# :verify => @config.ssl_verify, # :verify => @config.ssl_verify,
@ -274,6 +274,8 @@ module {{moduleName}}
# conn.adapter(Faraday.default_adapter) # conn.adapter(Faraday.default_adapter)
#end #end
update_params_for_auth! header_params, query_params, auth_names
if !post_body.nil? && !post_body.empty? if !post_body.nil? && !post_body.empty?
# use JSON string in the payload # use JSON string in the payload
form_or_body = post_body form_or_body = post_body

View File

@ -30,7 +30,7 @@ module {{moduleName}}
# @return [Hash] key: parameter name, value: parameter value (API key) # @return [Hash] key: parameter name, value: parameter value (API key)
# #
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string) # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
# config.api_key[:"api_key"] = "xxx" # config.api_key[:api_key] = "xxx"
property api_key : Hash(Symbol, String) property api_key : Hash(Symbol, String)
# Defines API key prefixes used with API Key authentications. # Defines API key prefixes used with API Key authentications.
@ -38,7 +38,7 @@ module {{moduleName}}
# @return [Hash] key: parameter name, value: API key prefix # @return [Hash] key: parameter name, value: API key prefix
# #
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers) # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
# config.api_key_prefix[:"api_key"] = "Token" # config.api_key_prefix[:api_key] = "Token"
property api_key_prefix : Hash(Symbol, String) property api_key_prefix : Hash(Symbol, String)
# Defines the username used with HTTP basic authentication. # Defines the username used with HTTP basic authentication.
@ -183,10 +183,10 @@ module {{moduleName}}
# Gets API key (with prefix if set). # Gets API key (with prefix if set).
# @param [String] param_name the parameter name of API key auth # @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)
if @api_key_prefix[param_name] if prefix = @api_key_prefix[param_name]?
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}" "#{prefix} #{@api_key[param_name]}"
else else
@api_key[param_name] @api_key[param_name]? || ""
end end
end end
@ -204,7 +204,7 @@ module {{moduleName}}
type: "api_key", type: "api_key",
in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}, in: {{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}},
key: "{{keyParamName}}", key: "{{keyParamName}}",
value: api_key_with_prefix("{{keyParamName}}") value: api_key_with_prefix(:{{keyParamName}})
}, },
{{/isApiKey}} {{/isApiKey}}
{{#isBasic}} {{#isBasic}}

View File

@ -79,7 +79,16 @@ describe "PetApi" do
describe "get_pet_by_id test" do describe "get_pet_by_id test" do
it "should work" do it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html # assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
api_instance = Petstore::PetApi.new
config = Petstore::Configuration.new
config.access_token = "yyy"
config.api_key[:api_key] = "xxx"
config.api_key_prefix[:api_key] = "Token"
api_client = Petstore::ApiClient.new(config)
api_instance = Petstore::PetApi.new(api_client)
# create a pet to start with # create a pet to start with
pet_id = Int64.new(91829) pet_id = Int64.new(91829)
pet = Petstore::Pet.new(id: pet_id, category: Petstore::Category.new(id: pet_id + 10, name: "crystal category"), name: "crystal", photo_urls: ["https://crystal-lang.org"], tags: [Petstore::Tag.new(id: pet_id + 100, name: "crystal tag")], status: "available") pet = Petstore::Pet.new(id: pet_id, category: Petstore::Category.new(id: pet_id + 10, name: "crystal category"), name: "crystal", photo_urls: ["https://crystal-lang.org"], tags: [Petstore::Tag.new(id: pet_id + 100, name: "crystal tag")], status: "available")

View File

@ -0,0 +1,56 @@
require "./spec_helper"
describe Petstore::ApiClient do
describe "#update_params_for_auth!" do
describe "oauth2" do
it "should add 'Authorization' to header" do
config = Petstore::Configuration.new
config.access_token = "xxx"
header_params = {} of String => String
query_params = {} of String => String
api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["petstore_auth"])
header_params["Authorization"].should eq "Bearer xxx"
query_params.size.should eq 0
end
end
describe "api_key" do
context "without api_key_prefix" do
it "should add 'api_key' to header" do
config = Petstore::Configuration.new
config.api_key[:api_key] = "xxx"
header_params = {} of String => String
query_params = {} of String => String
api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
header_params["api_key"].should eq "xxx"
query_params.empty?.should be_true
end
end
context "with api_key_prefix" do
it "should add 'api_key' to header" do
config = Petstore::Configuration.new
config.api_key[:api_key] = "xxx"
config.api_key_prefix[:api_key] = "Token"
header_params = {} of String => String
query_params = {} of String => String
api_client = Petstore::ApiClient.new(config)
api_client.update_params_for_auth!(header_params, query_params, ["api_key"])
header_params["api_key"].should eq "Token xxx"
query_params.empty?.should be_true
end
end
end
end
end

View File

@ -40,7 +40,7 @@ module Petstore
local_var_path = "/pet" local_var_path = "/pet"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -99,7 +99,7 @@ module Petstore
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -157,8 +157,8 @@ module Petstore
local_var_path = "/pet/findByStatus" local_var_path = "/pet/findByStatus"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
query_params[:"status"] = @api_client.build_collection_param(status, :csv) query_params["status"] = @api_client.build_collection_param(status, :csv)
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -217,8 +217,8 @@ module Petstore
local_var_path = "/pet/findByTags" local_var_path = "/pet/findByTags"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
query_params[:"tags"] = @api_client.build_collection_param(tags, :csv) query_params["tags"] = @api_client.build_collection_param(tags, :csv)
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -277,7 +277,7 @@ module Petstore
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -334,7 +334,7 @@ module Petstore
local_var_path = "/pet" local_var_path = "/pet"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -393,7 +393,7 @@ module Petstore
local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) local_var_path = "/pet/{petId}".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -452,7 +452,7 @@ module Petstore
local_var_path = "/pet/{petId}/uploadImage".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/")) local_var_path = "/pet/{petId}/uploadImage".sub("{" + "petId" + "}", URI.encode(pet_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new

View File

@ -42,7 +42,7 @@ module Petstore
local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/")) local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -93,7 +93,7 @@ module Petstore
local_var_path = "/store/inventory" local_var_path = "/store/inventory"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -160,7 +160,7 @@ module Petstore
local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/")) local_var_path = "/store/order/{orderId}".sub("{" + "orderId" + "}", URI.encode(order_id.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -217,7 +217,7 @@ module Petstore
local_var_path = "/store/order" local_var_path = "/store/order"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new

View File

@ -42,7 +42,7 @@ module Petstore
local_var_path = "/user" local_var_path = "/user"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -99,7 +99,7 @@ module Petstore
local_var_path = "/user/createWithArray" local_var_path = "/user/createWithArray"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -156,7 +156,7 @@ module Petstore
local_var_path = "/user/createWithList" local_var_path = "/user/createWithList"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -215,7 +215,7 @@ module Petstore
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -270,7 +270,7 @@ module Petstore
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -338,9 +338,9 @@ module Petstore
local_var_path = "/user/login" local_var_path = "/user/login"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
query_params[:"username"] = username query_params["username"] = username
query_params[:"password"] = password query_params["password"] = password
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -391,7 +391,7 @@ module Petstore
local_var_path = "/user/logout" local_var_path = "/user/logout"
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new
@ -454,7 +454,7 @@ module Petstore
local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/")) local_var_path = "/user/{username}".sub("{" + "username" + "}", URI.encode(username.to_s).gsub("%2F", "/"))
# query parameters # query parameters
query_params = Hash(Symbol, String).new query_params = Hash(String, String).new
# header parameters # header parameters
header_params = Hash(String, String).new header_params = Hash(String, String).new

View File

@ -171,7 +171,7 @@ module Petstore
# @param [Hash] query_params Query parameters # @param [Hash] query_params Query parameters
# @param [String] auth_names Authentication scheme name # @param [String] auth_names Authentication scheme name
def update_params_for_auth!(header_params, query_params, auth_names) def update_params_for_auth!(header_params, query_params, auth_names)
Array{auth_names}.each do |auth_name| auth_names.each do |auth_name|
auth_setting = @config.auth_settings[auth_name] auth_setting = @config.auth_settings[auth_name]
next unless auth_setting next unless auth_setting
case auth_setting[:in] case auth_setting[:in]
@ -264,7 +264,7 @@ module Petstore
# #
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
# the data deserialized from response body (could be nil), response status code and response headers. # the data deserialized from response body (could be nil), response status code and response headers.
def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of Symbol => String, form_params = {} of Symbol => String) def call_api(http_method : Symbol, path : String, operation : Symbol, return_type : String, post_body : String?, auth_names = [] of String, header_params = {} of String => String, query_params = {} of String => String, form_params = {} of Symbol => String)
#ssl_options = { #ssl_options = {
# :ca_file => @config.ssl_ca_file, # :ca_file => @config.ssl_ca_file,
# :verify => @config.ssl_verify, # :verify => @config.ssl_verify,
@ -282,6 +282,8 @@ module Petstore
# conn.adapter(Faraday.default_adapter) # conn.adapter(Faraday.default_adapter)
#end #end
update_params_for_auth! header_params, query_params, auth_names
if !post_body.nil? && !post_body.empty? if !post_body.nil? && !post_body.empty?
# use JSON string in the payload # use JSON string in the payload
form_or_body = post_body form_or_body = post_body

View File

@ -38,7 +38,7 @@ module Petstore
# @return [Hash] key: parameter name, value: parameter value (API key) # @return [Hash] key: parameter name, value: parameter value (API key)
# #
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string) # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
# config.api_key[:"api_key"] = "xxx" # config.api_key[:api_key] = "xxx"
property api_key : Hash(Symbol, String) property api_key : Hash(Symbol, String)
# Defines API key prefixes used with API Key authentications. # Defines API key prefixes used with API Key authentications.
@ -46,7 +46,7 @@ module Petstore
# @return [Hash] key: parameter name, value: API key prefix # @return [Hash] key: parameter name, value: API key prefix
# #
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers) # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
# config.api_key_prefix[:"api_key"] = "Token" # config.api_key_prefix[:api_key] = "Token"
property api_key_prefix : Hash(Symbol, String) property api_key_prefix : Hash(Symbol, String)
# Defines the username used with HTTP basic authentication. # Defines the username used with HTTP basic authentication.
@ -191,10 +191,10 @@ module Petstore
# Gets API key (with prefix if set). # Gets API key (with prefix if set).
# @param [String] param_name the parameter name of API key auth # @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)
if @api_key_prefix[param_name] if prefix = @api_key_prefix[param_name]?
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}" "#{prefix} #{@api_key[param_name]}"
else else
@api_key[param_name] @api_key[param_name]? || ""
end end
end end
@ -210,7 +210,7 @@ module Petstore
type: "api_key", type: "api_key",
in: "header", in: "header",
key: "api_key", key: "api_key",
value: api_key_with_prefix("api_key") value: api_key_with_prefix(:api_key)
}, },
"petstore_auth" => { "petstore_auth" => {
type: "oauth2", type: "oauth2",