kafka_rest_proxy_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +69 -0
- data/README.md +129 -0
- data/Rakefile +8 -0
- data/bin/console +9 -0
- data/docs/Body.md +12 -0
- data/docs/InlineResponse200.md +10 -0
- data/docs/InlineResponse200Offsets.md +11 -0
- data/docs/Offset.md +11 -0
- data/docs/ProducerApi.md +58 -0
- data/docs/ProducerRequest.md +12 -0
- data/docs/ProducerResponse.md +10 -0
- data/docs/Record.md +10 -0
- data/docs/TopicstopicNameRecords.md +10 -0
- data/git_push.sh +55 -0
- data/kafka_rest_proxy_client.gemspec +33 -0
- data/lib/kafka_rest_proxy_client.rb +48 -0
- data/lib/kafka_rest_proxy_client/api/producer_api.rb +69 -0
- data/lib/kafka_rest_proxy_client/api_client.rb +364 -0
- data/lib/kafka_rest_proxy_client/api_error.rb +26 -0
- data/lib/kafka_rest_proxy_client/configuration.rb +184 -0
- data/lib/kafka_rest_proxy_client/models/body.rb +214 -0
- data/lib/kafka_rest_proxy_client/models/inline_response_200.rb +196 -0
- data/lib/kafka_rest_proxy_client/models/inline_response_200_offsets.rb +203 -0
- data/lib/kafka_rest_proxy_client/models/offset.rb +203 -0
- data/lib/kafka_rest_proxy_client/models/producer_request.rb +214 -0
- data/lib/kafka_rest_proxy_client/models/producer_response.rb +196 -0
- data/lib/kafka_rest_proxy_client/models/record.rb +194 -0
- data/lib/kafka_rest_proxy_client/models/topicstopic_name_records.rb +194 -0
- data/lib/kafka_rest_proxy_client/version.rb +3 -0
- data/spec/api/producer_api_spec.rb +47 -0
- data/spec/api_client_spec.rb +225 -0
- data/spec/configuration_spec.rb +41 -0
- data/spec/models/body_spec.rb +65 -0
- data/spec/models/inline_response_200_offsets_spec.rb +59 -0
- data/spec/models/inline_response_200_spec.rb +53 -0
- data/spec/models/offset_spec.rb +59 -0
- data/spec/models/producer_request_spec.rb +65 -0
- data/spec/models/producer_response_spec.rb +53 -0
- data/spec/models/record_spec.rb +53 -0
- data/spec/models/topicstopic_name_records_spec.rb +53 -0
- data/spec/spec_helper.rb +110 -0
- data/swagger.yml +170 -0
- metadata +279 -0
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "kafka_rest_proxy_client/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "kafka_rest_proxy_client"
|
8
|
+
s.version = KafkaProxyRestClient::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["Swagger-Codegen"]
|
11
|
+
s.email = [""]
|
12
|
+
s.homepage = "https://github.com/swagger-api/swagger-codegen"
|
13
|
+
s.summary = "Kafka REST Proxy API Ruby Gem"
|
14
|
+
s.description = "A ruby client for the Kafka REST Proxy API"
|
15
|
+
s.license = "Apache 2.0"
|
16
|
+
s.required_ruby_version = ">= 1.9"
|
17
|
+
|
18
|
+
s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
|
19
|
+
s.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
|
22
|
+
s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.1'
|
23
|
+
s.add_development_dependency 'webmock', '~> 1.24', '>= 1.24.3'
|
24
|
+
s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
|
25
|
+
s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
|
26
|
+
s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
|
27
|
+
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.11'
|
28
|
+
|
29
|
+
s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
|
30
|
+
s.test_files = `find spec/*`.split("\n")
|
31
|
+
s.executables = []
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
=begin
|
2
|
+
#Kafka REST Proxy API
|
3
|
+
|
4
|
+
#An initial draft of the [Kafka REST Proxy API](https://github.com/confluentinc/kafka-rest). ## Example JSON request ``` POST /topics/test HTTP/1.1 Host: kafkaproxy.example.com Content-Type: application/vnd.kafka.json.v2+json Accept: application/vnd.kafka.v2+json, application/vnd.kafka+json, application/json { \"records\": [ { \"key\": \"somekey\", \"value\": {\"foo\": \"bar\"} }, { \"value\": [ \"foo\", \"bar\" ], \"partition\": 1 }, { \"value\": 53.5 } ] } ``` ## Example JSON response ``` HTTP/1.1 200 OK Content-Type: application/vnd.kafka.v2+json { \"key_schema_id\": null, \"value_schema_id\": null, \"offsets\": [ { \"partition\": 2, \"offset\": 100 }, { \"partition\": 1, \"offset\": 101 }, { \"partition\": 2, \"offset\": 102 } ] } ```
|
5
|
+
|
6
|
+
OpenAPI spec version: 0.1.0
|
7
|
+
|
8
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
9
|
+
|
10
|
+
=end
|
11
|
+
|
12
|
+
# Common files
|
13
|
+
require 'kafka_rest_proxy_client/api_client'
|
14
|
+
require 'kafka_rest_proxy_client/api_error'
|
15
|
+
require 'kafka_rest_proxy_client/version'
|
16
|
+
require 'kafka_rest_proxy_client/configuration'
|
17
|
+
|
18
|
+
# Models
|
19
|
+
require 'kafka_rest_proxy_client/models/body'
|
20
|
+
require 'kafka_rest_proxy_client/models/inline_response_200'
|
21
|
+
require 'kafka_rest_proxy_client/models/inline_response_200_offsets'
|
22
|
+
require 'kafka_rest_proxy_client/models/offset'
|
23
|
+
require 'kafka_rest_proxy_client/models/producer_request'
|
24
|
+
require 'kafka_rest_proxy_client/models/producer_response'
|
25
|
+
require 'kafka_rest_proxy_client/models/record'
|
26
|
+
require 'kafka_rest_proxy_client/models/topicstopic_name_records'
|
27
|
+
|
28
|
+
# APIs
|
29
|
+
require 'kafka_rest_proxy_client/api/producer_api'
|
30
|
+
|
31
|
+
module KafkaProxyRestClient
|
32
|
+
|
33
|
+
class << self
|
34
|
+
# Customize default settings for the SDK using block.
|
35
|
+
# KafkaProxyRestClient.configure do |config|
|
36
|
+
# config.username = "xxx"
|
37
|
+
# config.password = "xxx"
|
38
|
+
# end
|
39
|
+
# If no block given, return the default Configuration object.
|
40
|
+
def configure
|
41
|
+
if block_given?
|
42
|
+
yield(Configuration.default)
|
43
|
+
else
|
44
|
+
Configuration.default
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module KafkaProxyRestClient
|
4
|
+
class ProducerApi
|
5
|
+
attr_accessor :api_client
|
6
|
+
|
7
|
+
def initialize(api_client = ApiClient.default)
|
8
|
+
@api_client = api_client
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Produce messages to a topic, optionally specifying keys or partitions for the messages. If no partition is provided, one will be chosen based on the hash of the key. If no key is provided, the partition will be chosen for each message in a round-robin fashion. # For the ``avro`` embedded format, you must provide information about schemas and the REST proxy must be configured with the URL to access the schema registry (``schema.registry.connect``). Schemas may be provided as the full schema encoded as a string, or, after the initial request may be provided as the schema ID returned with the first response.
|
13
|
+
# @param topic_name Name of the topic to produce the messages to
|
14
|
+
# @param body Data to send to the kafka topic.
|
15
|
+
# @param [Hash] opts the optional parameters
|
16
|
+
# @return [InlineResponse200]
|
17
|
+
def topics_topic_name_post(topic_name, body, opts = {})
|
18
|
+
data, _status_code, _headers = topics_topic_name_post_with_http_info(topic_name, body, opts)
|
19
|
+
return data
|
20
|
+
end
|
21
|
+
alias_method :publish, :topics_topic_name_post
|
22
|
+
|
23
|
+
#
|
24
|
+
# Produce messages to a topic, optionally specifying keys or partitions for the messages. If no partition is provided, one will be chosen based on the hash of the key. If no key is provided, the partition will be chosen for each message in a round-robin fashion. # For the ``avro`` embedded format, you must provide information about schemas and the REST proxy must be configured with the URL to access the schema registry (``schema.registry.connect``). Schemas may be provided as the full schema encoded as a string, or, after the initial request may be provided as the schema ID returned with the first response.
|
25
|
+
# @param topic_name Name of the topic to produce the messages to
|
26
|
+
# @param body Data to send to the kafka topic.
|
27
|
+
# @param [Hash] opts the optional parameters
|
28
|
+
# @return [Array<(InlineResponse200, Fixnum, Hash)>] InlineResponse200 data, response status code and response headers
|
29
|
+
def topics_topic_name_post_with_http_info(topic_name, body, opts = {})
|
30
|
+
if @api_client.config.debugging
|
31
|
+
@api_client.config.logger.debug "Calling API: ProducerApi.topics_topic_name_post ..."
|
32
|
+
end
|
33
|
+
# verify the required parameter 'topic_name' is set
|
34
|
+
fail ArgumentError, "Missing the required parameter 'topic_name' when calling ProducerApi.topics_topic_name_post" if topic_name.nil?
|
35
|
+
# verify the required parameter 'body' is set
|
36
|
+
fail ArgumentError, "Missing the required parameter 'body' when calling ProducerApi.topics_topic_name_post" if body.nil?
|
37
|
+
# resource path
|
38
|
+
local_var_path = "/topics/{topic_name}".sub('{format}','json').sub('{' + 'topic_name' + '}', topic_name.to_s)
|
39
|
+
|
40
|
+
# query parameters
|
41
|
+
query_params = {}
|
42
|
+
|
43
|
+
# header parameters
|
44
|
+
header_params = {}
|
45
|
+
# HTTP header 'Accept' (if needed)
|
46
|
+
header_params['Accept'] = @api_client.select_header_accept(['application/vnd.kafka.v2+json', 'application/vnd.kafka+json', 'application/json'])
|
47
|
+
# HTTP header 'Content-Type'
|
48
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(['application/vnd.kafka.v2+json', 'application/vnd.kafka+json', 'application/json'])
|
49
|
+
|
50
|
+
# form parameters
|
51
|
+
form_params = {}
|
52
|
+
|
53
|
+
# http body (model)
|
54
|
+
post_body = @api_client.object_to_http_body(body)
|
55
|
+
auth_names = []
|
56
|
+
data, status_code, headers = @api_client.call_api(:POST, local_var_path,
|
57
|
+
:header_params => header_params,
|
58
|
+
:query_params => query_params,
|
59
|
+
:form_params => form_params,
|
60
|
+
:body => post_body,
|
61
|
+
:auth_names => auth_names,
|
62
|
+
:return_type => 'InlineResponse200')
|
63
|
+
if @api_client.config.debugging
|
64
|
+
@api_client.config.logger.debug "API called: ProducerApi#topics_topic_name_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
65
|
+
end
|
66
|
+
return data, status_code, headers
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,364 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'json'
|
3
|
+
require 'logger'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'typhoeus'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
module KafkaProxyRestClient
|
9
|
+
class ApiClient
|
10
|
+
# The Configuration object holding settings to be used in the API client.
|
11
|
+
attr_accessor :config
|
12
|
+
|
13
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
14
|
+
#
|
15
|
+
# @return [Hash]
|
16
|
+
attr_accessor :default_headers
|
17
|
+
|
18
|
+
# Initializes the ApiClient
|
19
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
20
|
+
def initialize(config = Configuration.default)
|
21
|
+
@config = config
|
22
|
+
@user_agent = "Swagger-Codegen/#{VERSION}/ruby"
|
23
|
+
@default_headers = {
|
24
|
+
'Content-Type' => "application/json",
|
25
|
+
'User-Agent' => @user_agent
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.default
|
30
|
+
@@default ||= ApiClient.new
|
31
|
+
end
|
32
|
+
|
33
|
+
# Call an API with given options.
|
34
|
+
#
|
35
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
36
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
37
|
+
def call_api(http_method, path, opts = {})
|
38
|
+
request = build_request(http_method, path, opts)
|
39
|
+
response = request.run
|
40
|
+
|
41
|
+
if @config.debugging
|
42
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
43
|
+
end
|
44
|
+
|
45
|
+
unless response.success?
|
46
|
+
if response.timed_out?
|
47
|
+
fail ApiError.new('Connection timed out')
|
48
|
+
elsif response.code == 0
|
49
|
+
# Errors from libcurl will be made visible here
|
50
|
+
fail ApiError.new(:code => 0,
|
51
|
+
:message => response.return_message)
|
52
|
+
else
|
53
|
+
fail ApiError.new(:code => response.code,
|
54
|
+
:response_headers => response.headers,
|
55
|
+
:response_body => response.body),
|
56
|
+
response.status_message
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if opts[:return_type]
|
61
|
+
data = deserialize(response, opts[:return_type])
|
62
|
+
else
|
63
|
+
data = nil
|
64
|
+
end
|
65
|
+
return data, response.code, response.headers
|
66
|
+
end
|
67
|
+
|
68
|
+
# Builds the HTTP request
|
69
|
+
#
|
70
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
71
|
+
# @param [String] path URL path (e.g. /account/new)
|
72
|
+
# @option opts [Hash] :header_params Header parameters
|
73
|
+
# @option opts [Hash] :query_params Query parameters
|
74
|
+
# @option opts [Hash] :form_params Query parameters
|
75
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
76
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
77
|
+
def build_request(http_method, path, opts = {})
|
78
|
+
url = build_request_url(path)
|
79
|
+
http_method = http_method.to_sym.downcase
|
80
|
+
|
81
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
82
|
+
query_params = opts[:query_params] || {}
|
83
|
+
form_params = opts[:form_params] || {}
|
84
|
+
|
85
|
+
|
86
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
87
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
88
|
+
|
89
|
+
req_opts = {
|
90
|
+
:method => http_method,
|
91
|
+
:headers => header_params,
|
92
|
+
:params => query_params,
|
93
|
+
:params_encoding => @config.params_encoding,
|
94
|
+
:timeout => @config.timeout,
|
95
|
+
:ssl_verifypeer => @config.verify_ssl,
|
96
|
+
:ssl_verifyhost => _verify_ssl_host,
|
97
|
+
:sslcert => @config.cert_file,
|
98
|
+
:sslkey => @config.key_file,
|
99
|
+
:verbose => @config.debugging
|
100
|
+
}
|
101
|
+
|
102
|
+
# set custom cert, if provided
|
103
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
104
|
+
|
105
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
106
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
107
|
+
req_opts.update :body => req_body
|
108
|
+
if @config.debugging
|
109
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
Typhoeus::Request.new(url, req_opts)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Check if the given MIME is a JSON MIME.
|
117
|
+
# JSON MIME examples:
|
118
|
+
# application/json
|
119
|
+
# application/json; charset=UTF8
|
120
|
+
# APPLICATION/JSON
|
121
|
+
# */*
|
122
|
+
# @param [String] mime MIME
|
123
|
+
# @return [Boolean] True if the MIME is application/json
|
124
|
+
def json_mime?(mime)
|
125
|
+
(mime == "*/*") || !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
|
126
|
+
end
|
127
|
+
|
128
|
+
# Deserialize the response to the given return type.
|
129
|
+
#
|
130
|
+
# @param [Response] response HTTP response
|
131
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
132
|
+
def deserialize(response, return_type)
|
133
|
+
body = response.body
|
134
|
+
return nil if body.nil? || body.empty?
|
135
|
+
|
136
|
+
# return response body directly for String return type
|
137
|
+
return body if return_type == 'String'
|
138
|
+
|
139
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
140
|
+
return download_file(response) if return_type == 'File'
|
141
|
+
|
142
|
+
# ensuring a default content type
|
143
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
144
|
+
|
145
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
146
|
+
|
147
|
+
begin
|
148
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
149
|
+
rescue JSON::ParserError => e
|
150
|
+
if %w(String Date DateTime).include?(return_type)
|
151
|
+
data = body
|
152
|
+
else
|
153
|
+
raise e
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
convert_to_type data, return_type
|
158
|
+
end
|
159
|
+
|
160
|
+
# Convert data to the given return type.
|
161
|
+
# @param [Object] data Data to be converted
|
162
|
+
# @param [String] return_type Return type
|
163
|
+
# @return [Mixed] Data in a particular type
|
164
|
+
def convert_to_type(data, return_type)
|
165
|
+
return nil if data.nil?
|
166
|
+
case return_type
|
167
|
+
when 'String'
|
168
|
+
data.to_s
|
169
|
+
when 'Integer'
|
170
|
+
data.to_i
|
171
|
+
when 'Float'
|
172
|
+
data.to_f
|
173
|
+
when 'BOOLEAN'
|
174
|
+
data == true
|
175
|
+
when 'DateTime'
|
176
|
+
# parse date time (expecting ISO 8601 format)
|
177
|
+
DateTime.parse data
|
178
|
+
when 'Date'
|
179
|
+
# parse date time (expecting ISO 8601 format)
|
180
|
+
Date.parse data
|
181
|
+
when 'Object'
|
182
|
+
# generic object (usually a Hash), return directly
|
183
|
+
data
|
184
|
+
when /\AArray<(.+)>\z/
|
185
|
+
# e.g. Array<Pet>
|
186
|
+
sub_type = $1
|
187
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
188
|
+
when /\AHash\<String, (.+)\>\z/
|
189
|
+
# e.g. Hash<String, Integer>
|
190
|
+
sub_type = $1
|
191
|
+
{}.tap do |hash|
|
192
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
193
|
+
end
|
194
|
+
else
|
195
|
+
# models, e.g. Pet
|
196
|
+
KafkaProxyRestClient.const_get(return_type).new.tap do |model|
|
197
|
+
model.build_from_hash data
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
203
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
204
|
+
#
|
205
|
+
# @see Configuration#temp_folder_path
|
206
|
+
# @return [Tempfile] the file downloaded
|
207
|
+
def download_file(response)
|
208
|
+
content_disposition = response.headers['Content-Disposition']
|
209
|
+
if content_disposition and content_disposition =~ /filename=/i
|
210
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
211
|
+
prefix = sanitize_filename(filename)
|
212
|
+
else
|
213
|
+
prefix = 'download-'
|
214
|
+
end
|
215
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
216
|
+
|
217
|
+
tempfile = nil
|
218
|
+
encoding = response.body.encoding
|
219
|
+
Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
|
220
|
+
file.write(response.body)
|
221
|
+
tempfile = file
|
222
|
+
end
|
223
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
224
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
225
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
226
|
+
"explicitly with `tempfile.delete`"
|
227
|
+
tempfile
|
228
|
+
end
|
229
|
+
|
230
|
+
# Sanitize filename by removing path.
|
231
|
+
# e.g. ../../sun.gif becomes sun.gif
|
232
|
+
#
|
233
|
+
# @param [String] filename the filename to be sanitized
|
234
|
+
# @return [String] the sanitized filename
|
235
|
+
def sanitize_filename(filename)
|
236
|
+
filename.gsub(/.*[\/\\]/, '')
|
237
|
+
end
|
238
|
+
|
239
|
+
def build_request_url(path)
|
240
|
+
# Add leading and trailing slashes to path
|
241
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
242
|
+
URI.encode(@config.base_url + path)
|
243
|
+
end
|
244
|
+
|
245
|
+
# Builds the HTTP request body
|
246
|
+
#
|
247
|
+
# @param [Hash] header_params Header parameters
|
248
|
+
# @param [Hash] form_params Query parameters
|
249
|
+
# @param [Object] body HTTP body (JSON/XML)
|
250
|
+
# @return [String] HTTP body data in the form of string
|
251
|
+
def build_request_body(header_params, form_params, body)
|
252
|
+
# http form
|
253
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
254
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
255
|
+
data = {}
|
256
|
+
form_params.each do |key, value|
|
257
|
+
case value
|
258
|
+
when File, Array, nil
|
259
|
+
# let typhoeus handle File, Array and nil parameters
|
260
|
+
data[key] = value
|
261
|
+
else
|
262
|
+
data[key] = value.to_s
|
263
|
+
end
|
264
|
+
end
|
265
|
+
elsif body
|
266
|
+
data = body.is_a?(String) ? body : body.to_json
|
267
|
+
else
|
268
|
+
data = nil
|
269
|
+
end
|
270
|
+
data
|
271
|
+
end
|
272
|
+
|
273
|
+
# Update hearder and query params based on authentication settings.
|
274
|
+
#
|
275
|
+
# @param [Hash] header_params Header parameters
|
276
|
+
# @param [Hash] query_params Query parameters
|
277
|
+
# @param [String] auth_names Authentication scheme name
|
278
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
279
|
+
Array(auth_names).each do |auth_name|
|
280
|
+
auth_setting = @config.auth_settings[auth_name]
|
281
|
+
next unless auth_setting
|
282
|
+
case auth_setting[:in]
|
283
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
284
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
285
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Sets user agent in HTTP header
|
291
|
+
#
|
292
|
+
# @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
|
293
|
+
def user_agent=(user_agent)
|
294
|
+
@user_agent = user_agent
|
295
|
+
@default_headers['User-Agent'] = @user_agent
|
296
|
+
end
|
297
|
+
|
298
|
+
# Return Accept header based on an array of accepts provided.
|
299
|
+
# @param [Array] accepts array for Accept
|
300
|
+
# @return [String] the Accept header (e.g. application/json)
|
301
|
+
def select_header_accept(accepts)
|
302
|
+
return nil if accepts.nil? || accepts.empty?
|
303
|
+
# use JSON when present, otherwise use all of the provided
|
304
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
305
|
+
return json_accept || accepts.join(',')
|
306
|
+
end
|
307
|
+
|
308
|
+
# Return Content-Type header based on an array of content types provided.
|
309
|
+
# @param [Array] content_types array for Content-Type
|
310
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
311
|
+
def select_header_content_type(content_types)
|
312
|
+
# use application/json by default
|
313
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
314
|
+
# use JSON when present, otherwise use the first one
|
315
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
316
|
+
return json_content_type || content_types.first
|
317
|
+
end
|
318
|
+
|
319
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
320
|
+
# @param [Object] model object to be converted into JSON string
|
321
|
+
# @return [String] JSON string representation of the object
|
322
|
+
def object_to_http_body(model)
|
323
|
+
return model if model.nil? || model.is_a?(String)
|
324
|
+
local_body = nil
|
325
|
+
if model.is_a?(Array)
|
326
|
+
local_body = model.map{|m| object_to_hash(m) }
|
327
|
+
else
|
328
|
+
local_body = object_to_hash(model)
|
329
|
+
end
|
330
|
+
local_body.to_json
|
331
|
+
end
|
332
|
+
|
333
|
+
# Convert object(non-array) to hash.
|
334
|
+
# @param [Object] obj object to be converted into JSON string
|
335
|
+
# @return [String] JSON string representation of the object
|
336
|
+
def object_to_hash(obj)
|
337
|
+
if obj.respond_to?(:to_hash)
|
338
|
+
obj.to_hash
|
339
|
+
else
|
340
|
+
obj
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
# Build parameter value according to the given collection format.
|
345
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
346
|
+
def build_collection_param(param, collection_format)
|
347
|
+
case collection_format
|
348
|
+
when :csv
|
349
|
+
param.join(',')
|
350
|
+
when :ssv
|
351
|
+
param.join(' ')
|
352
|
+
when :tsv
|
353
|
+
param.join("\t")
|
354
|
+
when :pipes
|
355
|
+
param.join('|')
|
356
|
+
when :multi
|
357
|
+
# return the array directly as typhoeus will handle it as expected
|
358
|
+
param
|
359
|
+
else
|
360
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|