devcycle-server-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +9 -0
  3. data/README.md +175 -0
  4. data/Rakefile +10 -0
  5. data/devcycle-server-sdk.gemspec +38 -0
  6. data/docs/DevcycleApi.md +289 -0
  7. data/docs/ErrorResponse.md +20 -0
  8. data/docs/Event.md +26 -0
  9. data/docs/Feature.md +26 -0
  10. data/docs/UserData.md +48 -0
  11. data/docs/Variable.md +24 -0
  12. data/examples/sinatra/Gemfile +6 -0
  13. data/examples/sinatra/Gemfile.lock +51 -0
  14. data/examples/sinatra/README.md +14 -0
  15. data/examples/sinatra/app.rb +47 -0
  16. data/git_push.sh +57 -0
  17. data/lib/devcycle-server-sdk/api/devcycle_api.rb +347 -0
  18. data/lib/devcycle-server-sdk/api_client.rb +390 -0
  19. data/lib/devcycle-server-sdk/api_error.rb +57 -0
  20. data/lib/devcycle-server-sdk/configuration.rb +278 -0
  21. data/lib/devcycle-server-sdk/models/error_response.rb +234 -0
  22. data/lib/devcycle-server-sdk/models/event.rb +264 -0
  23. data/lib/devcycle-server-sdk/models/feature.rb +313 -0
  24. data/lib/devcycle-server-sdk/models/inline_response201.rb +218 -0
  25. data/lib/devcycle-server-sdk/models/user_data.rb +447 -0
  26. data/lib/devcycle-server-sdk/models/user_data_and_events_body.rb +229 -0
  27. data/lib/devcycle-server-sdk/models/variable.rb +303 -0
  28. data/lib/devcycle-server-sdk/version.rb +15 -0
  29. data/lib/devcycle-server-sdk.rb +47 -0
  30. data/spec/api/devcycle_api_spec.rb +80 -0
  31. data/spec/api_client_spec.rb +226 -0
  32. data/spec/configuration_spec.rb +42 -0
  33. data/spec/models/error_response_spec.rb +40 -0
  34. data/spec/models/event_spec.rb +58 -0
  35. data/spec/models/feature_spec.rb +62 -0
  36. data/spec/models/inline_response201_spec.rb +34 -0
  37. data/spec/models/user_data_and_events_body_spec.rb +40 -0
  38. data/spec/models/user_data_spec.rb +128 -0
  39. data/spec/models/variable_spec.rb +56 -0
  40. data/spec/spec_helper.rb +111 -0
  41. metadata +134 -0
@@ -0,0 +1,390 @@
1
+ =begin
2
+ #DevCycle Bucketing API
3
+
4
+ #Documents the DevCycle Bucketing API which provides and API interface to User Bucketing and for generated SDKs.
5
+
6
+ The version of the OpenAPI document: 1.0.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 5.3.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'time'
18
+ require 'typhoeus'
19
+
20
+ module DevCycle
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "OpenAPI-Generator/#{VERSION}/ruby"
35
+ @default_headers = {
36
+ 'Content-Type' => 'application/json',
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ end
40
+
41
+ def self.default
42
+ @@default ||= ApiClient.new
43
+ end
44
+
45
+ # Call an API with given options.
46
+ #
47
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
+ # the data deserialized from response body (could be nil), response status code and response headers.
49
+ def call_api(http_method, path, opts = {})
50
+ request = build_request(http_method, path, opts)
51
+ response = request.run
52
+
53
+ if @config.debugging
54
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
+ end
56
+
57
+ unless response.success?
58
+ if response.timed_out?
59
+ fail ApiError.new('Connection timed out')
60
+ elsif response.code == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(:code => 0,
63
+ :message => response.return_message)
64
+ else
65
+ fail ApiError.new(:code => response.code,
66
+ :response_headers => response.headers,
67
+ :response_body => response.body),
68
+ response.status_message
69
+ end
70
+ end
71
+
72
+ if opts[:return_type]
73
+ data = deserialize(response, opts[:return_type])
74
+ else
75
+ data = nil
76
+ end
77
+ return data, response.code, response.headers
78
+ end
79
+
80
+ # Builds the HTTP request
81
+ #
82
+ # @param [String] http_method HTTP method/verb (e.g. POST)
83
+ # @param [String] path URL path (e.g. /account/new)
84
+ # @option opts [Hash] :header_params Header parameters
85
+ # @option opts [Hash] :query_params Query parameters
86
+ # @option opts [Hash] :form_params Query parameters
87
+ # @option opts [Object] :body HTTP body (JSON/XML)
88
+ # @return [Typhoeus::Request] A Typhoeus Request
89
+ def build_request(http_method, path, opts = {})
90
+ url = build_request_url(path, opts)
91
+ http_method = http_method.to_sym.downcase
92
+
93
+ header_params = @default_headers.merge(opts[:header_params] || {})
94
+ query_params = opts[:query_params] || {}
95
+ form_params = opts[:form_params] || {}
96
+
97
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
98
+
99
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
100
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
101
+
102
+ req_opts = {
103
+ :method => http_method,
104
+ :headers => header_params,
105
+ :params => query_params,
106
+ :params_encoding => @config.params_encoding,
107
+ :timeout => @config.timeout,
108
+ :ssl_verifypeer => @config.verify_ssl,
109
+ :ssl_verifyhost => _verify_ssl_host,
110
+ :sslcert => @config.cert_file,
111
+ :sslkey => @config.key_file,
112
+ :verbose => @config.debugging
113
+ }
114
+
115
+ # set custom cert, if provided
116
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
117
+
118
+ if [:post, :patch, :put, :delete].include?(http_method)
119
+ req_body = build_request_body(header_params, form_params, opts[:body])
120
+ req_opts.update :body => req_body
121
+ if @config.debugging
122
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
123
+ end
124
+ end
125
+
126
+ request = Typhoeus::Request.new(url, req_opts)
127
+ download_file(request) if opts[:return_type] == 'File'
128
+ request
129
+ end
130
+
131
+ # Builds the HTTP request body
132
+ #
133
+ # @param [Hash] header_params Header parameters
134
+ # @param [Hash] form_params Query parameters
135
+ # @param [Object] body HTTP body (JSON/XML)
136
+ # @return [String] HTTP body data in the form of string
137
+ def build_request_body(header_params, form_params, body)
138
+ # http form
139
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
140
+ header_params['Content-Type'] == 'multipart/form-data'
141
+ data = {}
142
+ form_params.each do |key, value|
143
+ case value
144
+ when ::File, ::Array, nil
145
+ # let typhoeus handle File, Array and nil parameters
146
+ data[key] = value
147
+ else
148
+ data[key] = value.to_s
149
+ end
150
+ end
151
+ elsif body
152
+ data = body.is_a?(String) ? body : body.to_json
153
+ else
154
+ data = nil
155
+ end
156
+ data
157
+ end
158
+
159
+ # Save response body into a file in (the defined) temporary folder, using the filename
160
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
161
+ # The response body is written to the file in chunks in order to handle files which
162
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
163
+ # process can use.
164
+ #
165
+ # @see Configuration#temp_folder_path
166
+ def download_file(request)
167
+ tempfile = nil
168
+ encoding = nil
169
+ request.on_headers do |response|
170
+ content_disposition = response.headers['Content-Disposition']
171
+ if content_disposition && content_disposition =~ /filename=/i
172
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
173
+ prefix = sanitize_filename(filename)
174
+ else
175
+ prefix = 'download-'
176
+ end
177
+ prefix = prefix + '-' unless prefix.end_with?('-')
178
+ encoding = response.body.encoding
179
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
180
+ @tempfile = tempfile
181
+ end
182
+ request.on_body do |chunk|
183
+ chunk.force_encoding(encoding)
184
+ tempfile.write(chunk)
185
+ end
186
+ request.on_complete do |response|
187
+ if tempfile
188
+ tempfile.close
189
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
190
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
191
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
192
+ "explicitly with `tempfile.delete`"
193
+ end
194
+ end
195
+ end
196
+
197
+ # Check if the given MIME is a JSON MIME.
198
+ # JSON MIME examples:
199
+ # application/json
200
+ # application/json; charset=UTF8
201
+ # APPLICATION/JSON
202
+ # */*
203
+ # @param [String] mime MIME
204
+ # @return [Boolean] True if the MIME is application/json
205
+ def json_mime?(mime)
206
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
207
+ end
208
+
209
+ # Deserialize the response to the given return type.
210
+ #
211
+ # @param [Response] response HTTP response
212
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
213
+ def deserialize(response, return_type)
214
+ body = response.body
215
+
216
+ # handle file downloading - return the File instance processed in request callbacks
217
+ # note that response body is empty when the file is written in chunks in request on_body callback
218
+ return @tempfile if return_type == 'File'
219
+
220
+ return nil if body.nil? || body.empty?
221
+
222
+ # return response body directly for String return type
223
+ return body if return_type == 'String'
224
+
225
+ # ensuring a default content type
226
+ content_type = response.headers['Content-Type'] || 'application/json'
227
+
228
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
229
+
230
+ begin
231
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
232
+ rescue JSON::ParserError => e
233
+ if %w(String Date Time).include?(return_type)
234
+ data = body
235
+ else
236
+ raise e
237
+ end
238
+ end
239
+
240
+ convert_to_type data, return_type
241
+ end
242
+
243
+ # Convert data to the given return type.
244
+ # @param [Object] data Data to be converted
245
+ # @param [String] return_type Return type
246
+ # @return [Mixed] Data in a particular type
247
+ def convert_to_type(data, return_type)
248
+ return nil if data.nil?
249
+ case return_type
250
+ when 'String'
251
+ data.to_s
252
+ when 'Integer'
253
+ data.to_i
254
+ when 'Float'
255
+ data.to_f
256
+ when 'Boolean'
257
+ data == true
258
+ when 'Time'
259
+ # parse date time (expecting ISO 8601 format)
260
+ Time.parse data
261
+ when 'Date'
262
+ # parse date time (expecting ISO 8601 format)
263
+ Date.parse data
264
+ when 'Object'
265
+ # generic object (usually a Hash), return directly
266
+ data
267
+ when /\AArray<(.+)>\z/
268
+ # e.g. Array<Pet>
269
+ sub_type = $1
270
+ data.map { |item| convert_to_type(item, sub_type) }
271
+ when /\AHash\<String, (.+)\>\z/
272
+ # e.g. Hash<String, Integer>
273
+ sub_type = $1
274
+ {}.tap do |hash|
275
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
276
+ end
277
+ else
278
+ # models (e.g. Pet) or oneOf
279
+ klass = DevCycle.const_get(return_type)
280
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
281
+ end
282
+ end
283
+
284
+ # Sanitize filename by removing path.
285
+ # e.g. ../../sun.gif becomes sun.gif
286
+ #
287
+ # @param [String] filename the filename to be sanitized
288
+ # @return [String] the sanitized filename
289
+ def sanitize_filename(filename)
290
+ filename.gsub(/.*[\/\\]/, '')
291
+ end
292
+
293
+ def build_request_url(path, opts = {})
294
+ # Add leading and trailing slashes to path
295
+ path = "/#{path}".gsub(/\/+/, '/')
296
+ @config.base_url(opts[:operation]) + path
297
+ end
298
+
299
+ # Update header and query params based on authentication settings.
300
+ #
301
+ # @param [Hash] header_params Header parameters
302
+ # @param [Hash] query_params Query parameters
303
+ # @param [String] auth_names Authentication scheme name
304
+ def update_params_for_auth!(header_params, query_params, auth_names)
305
+ Array(auth_names).each do |auth_name|
306
+ auth_setting = @config.auth_settings[auth_name]
307
+ next unless auth_setting
308
+ case auth_setting[:in]
309
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
310
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
311
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
312
+ end
313
+ end
314
+ end
315
+
316
+ # Sets user agent in HTTP header
317
+ #
318
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
319
+ def user_agent=(user_agent)
320
+ @user_agent = user_agent
321
+ @default_headers['User-Agent'] = @user_agent
322
+ end
323
+
324
+ # Return Accept header based on an array of accepts provided.
325
+ # @param [Array] accepts array for Accept
326
+ # @return [String] the Accept header (e.g. application/json)
327
+ def select_header_accept(accepts)
328
+ return nil if accepts.nil? || accepts.empty?
329
+ # use JSON when present, otherwise use all of the provided
330
+ json_accept = accepts.find { |s| json_mime?(s) }
331
+ json_accept || accepts.join(',')
332
+ end
333
+
334
+ # Return Content-Type header based on an array of content types provided.
335
+ # @param [Array] content_types array for Content-Type
336
+ # @return [String] the Content-Type header (e.g. application/json)
337
+ def select_header_content_type(content_types)
338
+ # return nil by default
339
+ return if content_types.nil? || content_types.empty?
340
+ # use JSON when present, otherwise use the first one
341
+ json_content_type = content_types.find { |s| json_mime?(s) }
342
+ json_content_type || content_types.first
343
+ end
344
+
345
+ # Convert object (array, hash, object, etc) to JSON string.
346
+ # @param [Object] model object to be converted into JSON string
347
+ # @return [String] JSON string representation of the object
348
+ def object_to_http_body(model)
349
+ return model if model.nil? || model.is_a?(String)
350
+ local_body = nil
351
+ if model.is_a?(Array)
352
+ local_body = model.map { |m| object_to_hash(m) }
353
+ else
354
+ local_body = object_to_hash(model)
355
+ end
356
+ local_body.to_json
357
+ end
358
+
359
+ # Convert object(non-array) to hash.
360
+ # @param [Object] obj object to be converted into JSON string
361
+ # @return [String] JSON string representation of the object
362
+ def object_to_hash(obj)
363
+ if obj.respond_to?(:to_hash)
364
+ obj.to_hash
365
+ else
366
+ obj
367
+ end
368
+ end
369
+
370
+ # Build parameter value according to the given collection format.
371
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
372
+ def build_collection_param(param, collection_format)
373
+ case collection_format
374
+ when :csv
375
+ param.join(',')
376
+ when :ssv
377
+ param.join(' ')
378
+ when :tsv
379
+ param.join("\t")
380
+ when :pipes
381
+ param.join('|')
382
+ when :multi
383
+ # return the array directly as typhoeus will handle it as expected
384
+ param
385
+ else
386
+ fail "unknown collection format: #{collection_format.inspect}"
387
+ end
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,57 @@
1
+ =begin
2
+ #DevCycle Bucketing API
3
+
4
+ #Documents the DevCycle Bucketing API which provides and API interface to User Bucketing and for generated SDKs.
5
+
6
+ The version of the OpenAPI document: 1.0.0
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ OpenAPI Generator version: 5.3.0
10
+
11
+ =end
12
+
13
+ module DevCycle
14
+ class ApiError < StandardError
15
+ attr_reader :code, :response_headers, :response_body
16
+
17
+ # Usage examples:
18
+ # ApiError.new
19
+ # ApiError.new("message")
20
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
21
+ # ApiError.new(:code => 404, :message => "Not Found")
22
+ def initialize(arg = nil)
23
+ if arg.is_a? Hash
24
+ if arg.key?(:message) || arg.key?('message')
25
+ super(arg[:message] || arg['message'])
26
+ else
27
+ super arg
28
+ end
29
+
30
+ arg.each do |k, v|
31
+ instance_variable_set "@#{k}", v
32
+ end
33
+ else
34
+ super arg
35
+ end
36
+ end
37
+
38
+ # Override to_s to display a friendly error message
39
+ def to_s
40
+ message
41
+ end
42
+
43
+ def message
44
+ if @message.nil?
45
+ msg = "Error message: the server returns an error"
46
+ else
47
+ msg = @message
48
+ end
49
+
50
+ msg += "\nHTTP status code: #{code}" if code
51
+ msg += "\nResponse headers: #{response_headers}" if response_headers
52
+ msg += "\nResponse body: #{response_body}" if response_body
53
+
54
+ msg
55
+ end
56
+ end
57
+ end