osdn-client 0.0.20160304
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/osdn-client.rb +45 -0
- data/lib/osdn-client/api/default_api.rb +148 -0
- data/lib/osdn-client/api/project_api.rb +1752 -0
- data/lib/osdn-client/api/project_frs_api.rb +1033 -0
- data/lib/osdn-client/api/project_news_api.rb +673 -0
- data/lib/osdn-client/api/user_api.rb +76 -0
- data/lib/osdn-client/api_client.rb +318 -0
- data/lib/osdn-client/api_error.rb +24 -0
- data/lib/osdn-client/configuration.rb +177 -0
- data/lib/osdn-client/models/group.rb +298 -0
- data/lib/osdn-client/models/group_tool_flags.rb +236 -0
- data/lib/osdn-client/models/news.rb +206 -0
- data/lib/osdn-client/models/package.rb +198 -0
- data/lib/osdn-client/models/pong.rb +176 -0
- data/lib/osdn-client/models/rel_file.rb +256 -0
- data/lib/osdn-client/models/release.rb +228 -0
- data/lib/osdn-client/models/simple_chamber.rb +176 -0
- data/lib/osdn-client/models/simple_group.rb +176 -0
- data/lib/osdn-client/models/simple_user.rb +166 -0
- data/lib/osdn-client/models/skill.rb +176 -0
- data/lib/osdn-client/models/token.rb +186 -0
- data/lib/osdn-client/models/user.rb +272 -0
- data/lib/osdn-client/version.rb +3 -0
- data/osdn-client.gemspec +32 -0
- data/spec/api/DefaultApi_spec.rb +58 -0
- data/spec/api/ProjectApi_spec.rb +472 -0
- data/spec/api/ProjectFrsApi_spec.rb +285 -0
- data/spec/api/ProjectNewsApi_spec.rb +194 -0
- data/spec/api/UserApi_spec.rb +39 -0
- data/spec/models/GroupToolFlags_spec.rb +124 -0
- data/spec/models/Group_spec.rb +184 -0
- data/spec/models/News_spec.rb +94 -0
- data/spec/models/Package_spec.rb +84 -0
- data/spec/models/Pong_spec.rb +64 -0
- data/spec/models/RelFile_spec.rb +144 -0
- data/spec/models/Release_spec.rb +114 -0
- data/spec/models/SimpleChamber_spec.rb +64 -0
- data/spec/models/SimpleGroup_spec.rb +64 -0
- data/spec/models/SimpleUser_spec.rb +54 -0
- data/spec/models/Skill_spec.rb +64 -0
- data/spec/models/Token_spec.rb +74 -0
- data/spec/models/User_spec.rb +154 -0
- metadata +284 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module OSDNClient
|
4
|
+
class UserApi
|
5
|
+
attr_accessor :api_client
|
6
|
+
|
7
|
+
def initialize(api_client = ApiClient.default)
|
8
|
+
@api_client = api_client
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Get user profile.
|
13
|
+
# @param id_or_name numeric user id or user name
|
14
|
+
# @param [Hash] opts the optional parameters
|
15
|
+
# @return [User]
|
16
|
+
def get_user(id_or_name, opts = {})
|
17
|
+
data, status_code, headers = get_user_with_http_info(id_or_name, opts)
|
18
|
+
return data
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Get user profile.
|
23
|
+
# @param id_or_name numeric user id or user name
|
24
|
+
# @param [Hash] opts the optional parameters
|
25
|
+
# @return [Array<(User, Fixnum, Hash)>] User data, response status code and response headers
|
26
|
+
def get_user_with_http_info(id_or_name, opts = {})
|
27
|
+
if @api_client.config.debugging
|
28
|
+
@api_client.config.logger.debug "Calling API: UserApi#get_user ..."
|
29
|
+
end
|
30
|
+
|
31
|
+
# verify the required parameter 'id_or_name' is set
|
32
|
+
fail "Missing the required parameter 'id_or_name' when calling get_user" if id_or_name.nil?
|
33
|
+
|
34
|
+
# resource path
|
35
|
+
path = "/user/{id_or_name}".sub('{format}','json').sub('{' + 'id_or_name' + '}', id_or_name.to_s)
|
36
|
+
|
37
|
+
# query parameters
|
38
|
+
query_params = {}
|
39
|
+
|
40
|
+
# header parameters
|
41
|
+
header_params = {}
|
42
|
+
|
43
|
+
# HTTP header 'Accept' (if needed)
|
44
|
+
_header_accept = []
|
45
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
46
|
+
|
47
|
+
# HTTP header 'Content-Type'
|
48
|
+
_header_content_type = ['application/x-www-form-urlencoded']
|
49
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
50
|
+
|
51
|
+
# form parameters
|
52
|
+
form_params = {}
|
53
|
+
|
54
|
+
# http body (model)
|
55
|
+
post_body = nil
|
56
|
+
|
57
|
+
|
58
|
+
auth_names = ['oauth2-implicit']
|
59
|
+
data, status_code, headers = @api_client.call_api(:GET, path,
|
60
|
+
:header_params => header_params,
|
61
|
+
:query_params => query_params,
|
62
|
+
:form_params => form_params,
|
63
|
+
:body => post_body,
|
64
|
+
:auth_names => auth_names,
|
65
|
+
:return_type => 'User')
|
66
|
+
if @api_client.config.debugging
|
67
|
+
@api_client.config.logger.debug "API called: UserApi#get_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
68
|
+
end
|
69
|
+
return data, status_code, headers
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
@@ -0,0 +1,318 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'json'
|
3
|
+
require 'logger'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'typhoeus'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
module OSDNClient
|
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
|
+
def initialize(config = Configuration.default)
|
19
|
+
@config = config
|
20
|
+
@user_agent = "ruby-swagger-#{VERSION}"
|
21
|
+
@default_headers = {
|
22
|
+
'Content-Type' => "application/json",
|
23
|
+
'User-Agent' => @user_agent
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.default
|
28
|
+
@@default ||= ApiClient.new
|
29
|
+
end
|
30
|
+
|
31
|
+
# Call an API with given options.
|
32
|
+
#
|
33
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
34
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
35
|
+
def call_api(http_method, path, opts = {})
|
36
|
+
request = build_request(http_method, path, opts)
|
37
|
+
response = request.run
|
38
|
+
|
39
|
+
if @config.debugging
|
40
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
unless response.success?
|
44
|
+
fail ApiError.new(:code => response.code,
|
45
|
+
:response_headers => response.headers,
|
46
|
+
:response_body => response.body),
|
47
|
+
response.status_message
|
48
|
+
end
|
49
|
+
|
50
|
+
if opts[:return_type]
|
51
|
+
data = deserialize(response, opts[:return_type])
|
52
|
+
else
|
53
|
+
data = nil
|
54
|
+
end
|
55
|
+
return data, response.code, response.headers
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_request(http_method, path, opts = {})
|
59
|
+
url = build_request_url(path)
|
60
|
+
http_method = http_method.to_sym.downcase
|
61
|
+
|
62
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
63
|
+
query_params = opts[:query_params] || {}
|
64
|
+
form_params = opts[:form_params] || {}
|
65
|
+
|
66
|
+
|
67
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
68
|
+
|
69
|
+
|
70
|
+
req_opts = {
|
71
|
+
:method => http_method,
|
72
|
+
:headers => header_params,
|
73
|
+
:params => query_params,
|
74
|
+
:timeout => @config.timeout,
|
75
|
+
:ssl_verifypeer => @config.verify_ssl,
|
76
|
+
:sslcert => @config.cert_file,
|
77
|
+
:sslkey => @config.key_file,
|
78
|
+
:cainfo => @config.ssl_ca_cert,
|
79
|
+
:verbose => @config.debugging
|
80
|
+
}
|
81
|
+
|
82
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
83
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
84
|
+
req_opts.update :body => req_body
|
85
|
+
if @config.debugging
|
86
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
Typhoeus::Request.new(url, req_opts)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Check if the given MIME is a JSON MIME.
|
94
|
+
# JSON MIME examples:
|
95
|
+
# application/json
|
96
|
+
# application/json; charset=UTF8
|
97
|
+
# APPLICATION/JSON
|
98
|
+
def json_mime?(mime)
|
99
|
+
!!(mime =~ /\Aapplication\/json(;.*)?\z/i)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Deserialize the response to the given return type.
|
103
|
+
#
|
104
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
105
|
+
def deserialize(response, return_type)
|
106
|
+
body = response.body
|
107
|
+
return nil if body.nil? || body.empty?
|
108
|
+
|
109
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
110
|
+
return download_file(response) if return_type == 'File'
|
111
|
+
|
112
|
+
# ensuring a default content type
|
113
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
114
|
+
|
115
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
116
|
+
|
117
|
+
begin
|
118
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
119
|
+
rescue JSON::ParserError => e
|
120
|
+
if %w(String Date DateTime).include?(return_type)
|
121
|
+
data = body
|
122
|
+
else
|
123
|
+
raise e
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
convert_to_type data, return_type
|
128
|
+
end
|
129
|
+
|
130
|
+
# Convert data to the given return type.
|
131
|
+
def convert_to_type(data, return_type)
|
132
|
+
return nil if data.nil?
|
133
|
+
case return_type
|
134
|
+
when 'String'
|
135
|
+
data.to_s
|
136
|
+
when 'Integer'
|
137
|
+
data.to_i
|
138
|
+
when 'Float'
|
139
|
+
data.to_f
|
140
|
+
when 'BOOLEAN'
|
141
|
+
data == true
|
142
|
+
when 'DateTime'
|
143
|
+
# parse date time (expecting ISO 8601 format)
|
144
|
+
DateTime.parse data
|
145
|
+
when 'Date'
|
146
|
+
# parse date time (expecting ISO 8601 format)
|
147
|
+
Date.parse data
|
148
|
+
when 'Object'
|
149
|
+
# generic object, return directly
|
150
|
+
data
|
151
|
+
when /\AArray<(.+)>\z/
|
152
|
+
# e.g. Array<Pet>
|
153
|
+
sub_type = $1
|
154
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
155
|
+
when /\AHash\<String, (.+)\>\z/
|
156
|
+
# e.g. Hash<String, Integer>
|
157
|
+
sub_type = $1
|
158
|
+
{}.tap do |hash|
|
159
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
160
|
+
end
|
161
|
+
else
|
162
|
+
# models, e.g. Pet
|
163
|
+
OSDNClient.const_get(return_type).new.tap do |model|
|
164
|
+
model.build_from_hash data
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
170
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
171
|
+
#
|
172
|
+
# @see Configuration#temp_folder_path
|
173
|
+
# @return [Tempfile] the file downloaded
|
174
|
+
def download_file(response)
|
175
|
+
content_disposition = response.headers['Content-Disposition']
|
176
|
+
if content_disposition
|
177
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
178
|
+
prefix = sanitize_filename(filename)
|
179
|
+
else
|
180
|
+
prefix = 'download-'
|
181
|
+
end
|
182
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
183
|
+
|
184
|
+
tempfile = nil
|
185
|
+
encoding = response.body.encoding
|
186
|
+
Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
|
187
|
+
file.write(response.body)
|
188
|
+
tempfile = file
|
189
|
+
end
|
190
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
191
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
192
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
193
|
+
"explicitly with `tempfile.delete`"
|
194
|
+
tempfile
|
195
|
+
end
|
196
|
+
|
197
|
+
# Sanitize filename by removing path.
|
198
|
+
# e.g. ../../sun.gif becomes sun.gif
|
199
|
+
#
|
200
|
+
# @param [String] filename the filename to be sanitized
|
201
|
+
# @return [String] the sanitized filename
|
202
|
+
def sanitize_filename(filename)
|
203
|
+
filename.gsub /.*[\/\\]/, ''
|
204
|
+
end
|
205
|
+
|
206
|
+
def build_request_url(path)
|
207
|
+
# Add leading and trailing slashes to path
|
208
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
209
|
+
URI.encode(@config.base_url + path)
|
210
|
+
end
|
211
|
+
|
212
|
+
def build_request_body(header_params, form_params, body)
|
213
|
+
# http form
|
214
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
215
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
216
|
+
data = {}
|
217
|
+
form_params.each do |key, value|
|
218
|
+
case value
|
219
|
+
when File, Array, nil
|
220
|
+
# let typhoeus handle File, Array and nil parameters
|
221
|
+
data[key] = value
|
222
|
+
else
|
223
|
+
data[key] = value.to_s
|
224
|
+
end
|
225
|
+
end
|
226
|
+
elsif body
|
227
|
+
data = body.is_a?(String) ? body : body.to_json
|
228
|
+
else
|
229
|
+
data = nil
|
230
|
+
end
|
231
|
+
data
|
232
|
+
end
|
233
|
+
|
234
|
+
# Update hearder and query params based on authentication settings.
|
235
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
236
|
+
Array(auth_names).each do |auth_name|
|
237
|
+
auth_setting = @config.auth_settings[auth_name]
|
238
|
+
next unless auth_setting
|
239
|
+
case auth_setting[:in]
|
240
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
241
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
242
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def user_agent=(user_agent)
|
248
|
+
@user_agent = user_agent
|
249
|
+
@default_headers['User-Agent'] = @user_agent
|
250
|
+
end
|
251
|
+
|
252
|
+
# Return Accept header based on an array of accepts provided.
|
253
|
+
# @param [Array] accepts array for Accept
|
254
|
+
# @return [String] the Accept header (e.g. application/json)
|
255
|
+
def select_header_accept(accepts)
|
256
|
+
return nil if accepts.nil? || accepts.empty?
|
257
|
+
# use JSON when present, otherwise use all of the provided
|
258
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
259
|
+
return json_accept || accepts.join(',')
|
260
|
+
end
|
261
|
+
|
262
|
+
# Return Content-Type header based on an array of content types provided.
|
263
|
+
# @param [Array] content_types array for Content-Type
|
264
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
265
|
+
def select_header_content_type(content_types)
|
266
|
+
# use application/json by default
|
267
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
268
|
+
# use JSON when present, otherwise use the first one
|
269
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
270
|
+
return json_content_type || content_types.first
|
271
|
+
end
|
272
|
+
|
273
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
274
|
+
# @param [Object] model object to be converted into JSON string
|
275
|
+
# @return [String] JSON string representation of the object
|
276
|
+
def object_to_http_body(model)
|
277
|
+
return if model.nil?
|
278
|
+
_body = nil
|
279
|
+
if model.is_a?(Array)
|
280
|
+
_body = model.map{|m| object_to_hash(m) }
|
281
|
+
else
|
282
|
+
_body = object_to_hash(model)
|
283
|
+
end
|
284
|
+
_body.to_json
|
285
|
+
end
|
286
|
+
|
287
|
+
# Convert object(non-array) to hash.
|
288
|
+
# @param [Object] obj object to be converted into JSON string
|
289
|
+
# @return [String] JSON string representation of the object
|
290
|
+
def object_to_hash(obj)
|
291
|
+
if obj.respond_to?(:to_hash)
|
292
|
+
obj.to_hash
|
293
|
+
else
|
294
|
+
obj
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Build parameter value according to the given collection format.
|
299
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
300
|
+
def build_collection_param(param, collection_format)
|
301
|
+
case collection_format
|
302
|
+
when :csv
|
303
|
+
param.join(',')
|
304
|
+
when :ssv
|
305
|
+
param.join(' ')
|
306
|
+
when :tsv
|
307
|
+
param.join("\t")
|
308
|
+
when :pipes
|
309
|
+
param.join('|')
|
310
|
+
when :multi
|
311
|
+
# return the array directly as typhoeus will handle it as expected
|
312
|
+
param
|
313
|
+
else
|
314
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module OSDNClient
|
2
|
+
class ApiError < StandardError
|
3
|
+
attr_reader :code, :response_headers, :response_body
|
4
|
+
|
5
|
+
# Usage examples:
|
6
|
+
# ApiError.new
|
7
|
+
# ApiError.new("message")
|
8
|
+
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
|
9
|
+
# ApiError.new(:code => 404, :message => "Not Found")
|
10
|
+
def initialize(arg = nil)
|
11
|
+
if arg.is_a? Hash
|
12
|
+
arg.each do |k, v|
|
13
|
+
if k.to_s == 'message'
|
14
|
+
super v
|
15
|
+
else
|
16
|
+
instance_variable_set "@#{k}", v
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
super arg
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module OSDNClient
|
4
|
+
class Configuration
|
5
|
+
# Defines url scheme
|
6
|
+
attr_accessor :scheme
|
7
|
+
|
8
|
+
# Defines url host
|
9
|
+
attr_accessor :host
|
10
|
+
|
11
|
+
# Defines url base path
|
12
|
+
attr_accessor :base_path
|
13
|
+
|
14
|
+
# Defines API keys used with API Key authentications.
|
15
|
+
#
|
16
|
+
# @return [Hash] key: parameter name, value: parameter value (API key)
|
17
|
+
#
|
18
|
+
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
19
|
+
# config.api_key['api_key'] = 'xxx'
|
20
|
+
attr_accessor :api_key
|
21
|
+
|
22
|
+
# Defines API key prefixes used with API Key authentications.
|
23
|
+
#
|
24
|
+
# @return [Hash] key: parameter name, value: API key prefix
|
25
|
+
#
|
26
|
+
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
27
|
+
# config.api_key_prefix['api_key'] = 'Token'
|
28
|
+
attr_accessor :api_key_prefix
|
29
|
+
|
30
|
+
# Defines the username used with HTTP basic authentication.
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
attr_accessor :username
|
34
|
+
|
35
|
+
# Defines the password used with HTTP basic authentication.
|
36
|
+
#
|
37
|
+
# @return [String]
|
38
|
+
attr_accessor :password
|
39
|
+
|
40
|
+
# Defines the access token (Bearer) used with OAuth2.
|
41
|
+
attr_accessor :access_token
|
42
|
+
|
43
|
+
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
44
|
+
# details will be logged with `logger.debug` (see the `logger` attribute).
|
45
|
+
# Default to false.
|
46
|
+
#
|
47
|
+
# @return [true, false]
|
48
|
+
attr_accessor :debugging
|
49
|
+
|
50
|
+
# Defines the logger used for debugging.
|
51
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
52
|
+
#
|
53
|
+
# @return [#debug]
|
54
|
+
attr_accessor :logger
|
55
|
+
|
56
|
+
# Defines the temporary folder to store downloaded files
|
57
|
+
# (for API endpoints that have file response).
|
58
|
+
# Default to use `Tempfile`.
|
59
|
+
#
|
60
|
+
# @return [String]
|
61
|
+
attr_accessor :temp_folder_path
|
62
|
+
|
63
|
+
# The time limit for HTTP request in seconds.
|
64
|
+
# Default to 0 (never times out).
|
65
|
+
attr_accessor :timeout
|
66
|
+
|
67
|
+
### TLS/SSL
|
68
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
69
|
+
# Default to true.
|
70
|
+
#
|
71
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
72
|
+
#
|
73
|
+
# @return [true, false]
|
74
|
+
attr_accessor :verify_ssl
|
75
|
+
|
76
|
+
# Set this to customize the certificate file to verify the peer.
|
77
|
+
#
|
78
|
+
# @return [String] the path to the certificate file
|
79
|
+
#
|
80
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
81
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
82
|
+
attr_accessor :ssl_ca_cert
|
83
|
+
|
84
|
+
# Client certificate file (for client certificate)
|
85
|
+
attr_accessor :cert_file
|
86
|
+
|
87
|
+
# Client private key file (for client certificate)
|
88
|
+
attr_accessor :key_file
|
89
|
+
|
90
|
+
attr_accessor :inject_format
|
91
|
+
|
92
|
+
attr_accessor :force_ending_format
|
93
|
+
|
94
|
+
def initialize
|
95
|
+
@scheme = 'https'
|
96
|
+
@host = 'osdn.jp'
|
97
|
+
@base_path = '/api/v0'
|
98
|
+
@api_key = {}
|
99
|
+
@api_key_prefix = {}
|
100
|
+
@timeout = 0
|
101
|
+
@verify_ssl = true
|
102
|
+
@cert_file = nil
|
103
|
+
@key_file = nil
|
104
|
+
@debugging = false
|
105
|
+
@inject_format = false
|
106
|
+
@force_ending_format = false
|
107
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
108
|
+
|
109
|
+
yield(self) if block_given?
|
110
|
+
end
|
111
|
+
|
112
|
+
# The default Configuration object.
|
113
|
+
def self.default
|
114
|
+
@@default ||= Configuration.new
|
115
|
+
end
|
116
|
+
|
117
|
+
def configure
|
118
|
+
yield(self) if block_given?
|
119
|
+
end
|
120
|
+
|
121
|
+
def scheme=(scheme)
|
122
|
+
# remove :// from scheme
|
123
|
+
@scheme = scheme.sub(/:\/\//, '')
|
124
|
+
end
|
125
|
+
|
126
|
+
def host=(host)
|
127
|
+
# remove http(s):// and anything after a slash
|
128
|
+
@host = host.sub(/https?:\/\//, '').split('/').first
|
129
|
+
end
|
130
|
+
|
131
|
+
def base_path=(base_path)
|
132
|
+
# Add leading and trailing slashes to base_path
|
133
|
+
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
134
|
+
@base_path = "" if @base_path == "/"
|
135
|
+
end
|
136
|
+
|
137
|
+
def base_url
|
138
|
+
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
|
139
|
+
URI.encode(url)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Gets API key (with prefix if set).
|
143
|
+
# @param [String] param_name the parameter name of API key auth
|
144
|
+
def api_key_with_prefix(param_name)
|
145
|
+
if @api_key_prefix[param_name]
|
146
|
+
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
147
|
+
else
|
148
|
+
@api_key[param_name]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Gets Basic Auth token string
|
153
|
+
def basic_auth_token
|
154
|
+
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns Auth Settings hash for api client.
|
158
|
+
def auth_settings
|
159
|
+
{
|
160
|
+
'oauth2-code' =>
|
161
|
+
{
|
162
|
+
type: 'oauth2',
|
163
|
+
in: 'header',
|
164
|
+
key: 'Authorization',
|
165
|
+
value: "Bearer #{access_token}"
|
166
|
+
},
|
167
|
+
'oauth2-implicit' =>
|
168
|
+
{
|
169
|
+
type: 'oauth2',
|
170
|
+
in: 'header',
|
171
|
+
key: 'Authorization',
|
172
|
+
value: "Bearer #{access_token}"
|
173
|
+
},
|
174
|
+
}
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|