quake_timesheets_client 0.1.0

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/GENERATING_README.md +17 -0
  3. data/Gemfile +9 -0
  4. data/README.md +161 -0
  5. data/Rakefile +10 -0
  6. data/build.sh +10 -0
  7. data/commands +13 -0
  8. data/docs/Approval.md +34 -0
  9. data/docs/ApprovalType.md +24 -0
  10. data/docs/ApprovalTypesApi.md +153 -0
  11. data/docs/ApprovalsApi.md +153 -0
  12. data/docs/Dataset.md +24 -0
  13. data/docs/DatasetsApi.md +214 -0
  14. data/docs/EntriesApi.md +251 -0
  15. data/docs/Entry.md +36 -0
  16. data/docs/PeopleApi.md +151 -0
  17. data/docs/Person.md +22 -0
  18. data/git_push.sh +58 -0
  19. data/lib/quake_timesheets_client.rb +58 -0
  20. data/lib/quake_timesheets_client/api/approval_types_api.rb +158 -0
  21. data/lib/quake_timesheets_client/api/approvals_api.rb +174 -0
  22. data/lib/quake_timesheets_client/api/datasets_api.rb +202 -0
  23. data/lib/quake_timesheets_client/api/entries_api.rb +253 -0
  24. data/lib/quake_timesheets_client/api/people_api.rb +151 -0
  25. data/lib/quake_timesheets_client/api_client.rb +396 -0
  26. data/lib/quake_timesheets_client/api_error.rb +57 -0
  27. data/lib/quake_timesheets_client/configuration.rb +302 -0
  28. data/lib/quake_timesheets_client/models/approval.rb +378 -0
  29. data/lib/quake_timesheets_client/models/approval_type.rb +269 -0
  30. data/lib/quake_timesheets_client/models/dataset.rb +268 -0
  31. data/lib/quake_timesheets_client/models/entry.rb +361 -0
  32. data/lib/quake_timesheets_client/models/person.rb +254 -0
  33. data/lib/quake_timesheets_client/version.rb +24 -0
  34. data/quake_timesheets_client.gemspec +38 -0
  35. data/ruby-templates/README.mustache +187 -0
  36. data/ruby-templates/api_client.mustache +263 -0
  37. data/ruby-templates/api_client_faraday_partial.mustache +140 -0
  38. data/ruby-templates/configuration.mustache +379 -0
  39. data/ruby-templates/gem.mustache +59 -0
  40. data/ruby-templates/version.mustache +16 -0
  41. data/ruby.config.yaml +10 -0
  42. data/spec/api/approval_types_api_spec.rb +59 -0
  43. data/spec/api/approvals_api_spec.rb +60 -0
  44. data/spec/api/datasets_api_spec.rb +67 -0
  45. data/spec/api/entries_api_spec.rb +82 -0
  46. data/spec/api/people_api_spec.rb +58 -0
  47. data/spec/api_client_spec.rb +188 -0
  48. data/spec/configuration_spec.rb +42 -0
  49. data/spec/models/approval_spec.rb +86 -0
  50. data/spec/models/approval_type_spec.rb +52 -0
  51. data/spec/models/dataset_spec.rb +52 -0
  52. data/spec/models/entry_spec.rb +92 -0
  53. data/spec/models/person_spec.rb +46 -0
  54. data/spec/spec_helper.rb +111 -0
  55. metadata +149 -0
@@ -0,0 +1,140 @@
1
+ # Call an API with given options.
2
+ #
3
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
4
+ # the data deserialized from response body (could be nil), response status code and response headers.
5
+ def call_api(http_method, path, opts = {})
6
+ ssl_options = {
7
+ :ca_file => @config.ssl_ca_file,
8
+ :verify => @config.ssl_verify,
9
+ :verify_mode => @config.ssl_verify_mode,
10
+ :client_cert => @config.ssl_client_cert,
11
+ :client_key => @config.ssl_client_key
12
+ }
13
+
14
+ connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|
15
+ if opts[:header_params]["Content-Type"] == "multipart/form-data"
16
+ conn.request :multipart
17
+ conn.request :url_encoded
18
+ end
19
+
20
+ config.faraday_middlewares.each { |middleware| conn.use middleware }
21
+
22
+ conn.adapter(Faraday.default_adapter)
23
+ end
24
+
25
+ begin
26
+ response = connection.public_send(http_method.to_sym.downcase) do |req|
27
+ build_request(http_method, path, req, opts)
28
+ end
29
+
30
+ if @config.debugging
31
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
32
+ end
33
+
34
+ unless response.success?
35
+ if response.status == 0
36
+ # Errors from libcurl will be made visible here
37
+ fail ApiError.new(:code => 0,
38
+ :message => response.return_message)
39
+ else
40
+ fail ApiError.new(:code => response.status,
41
+ :response_headers => response.headers,
42
+ :response_body => response.body),
43
+ response.reason_phrase
44
+ end
45
+ end
46
+ rescue Faraday::TimeoutError
47
+ fail ApiError.new('Connection timed out')
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.status, response.headers
56
+ end
57
+
58
+ # Builds the HTTP request
59
+ #
60
+ # @param [String] http_method HTTP method/verb (e.g. POST)
61
+ # @param [String] path URL path (e.g. /account/new)
62
+ # @option opts [Hash] :header_params Header parameters
63
+ # @option opts [Hash] :query_params Query parameters
64
+ # @option opts [Hash] :form_params Query parameters
65
+ # @option opts [Object] :body HTTP body (JSON/XML)
66
+ # @return [Typhoeus::Request] A Typhoeus Request
67
+ def build_request(http_method, path, request, opts = {})
68
+ url = build_request_url(path, opts)
69
+ http_method = http_method.to_sym.downcase
70
+
71
+ header_params = @default_headers.merge(opts[:header_params] || {})
72
+ query_params = opts[:query_params] || {}
73
+ form_params = opts[:form_params] || {}
74
+
75
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
76
+
77
+ req_opts = {
78
+ :method => http_method,
79
+ :headers => header_params,
80
+ :params => query_params,
81
+ :params_encoding => @config.params_encoding,
82
+ :timeout => @config.timeout,
83
+ :verbose => @config.debugging
84
+ }
85
+
86
+ if [:post, :patch, :put, :delete].include?(http_method)
87
+ req_body = build_request_body(header_params, form_params, opts[:body])
88
+ req_opts.update :body => req_body
89
+ if @config.debugging
90
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
91
+ end
92
+ end
93
+ request.headers = header_params
94
+ request.body = req_body
95
+ request.url url
96
+ request.params = query_params
97
+ download_file(request) if opts[:return_type] == 'File'
98
+ request
99
+ end
100
+
101
+ # Builds the HTTP request body
102
+ #
103
+ # @param [Hash] header_params Header parameters
104
+ # @param [Hash] form_params Query parameters
105
+ # @param [Object] body HTTP body (JSON/XML)
106
+ # @return [String] HTTP body data in the form of string
107
+ def build_request_body(header_params, form_params, body)
108
+ # http form
109
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
110
+ data = URI.encode_www_form(form_params)
111
+ elsif header_params['Content-Type'] == 'multipart/form-data'
112
+ data = {}
113
+ form_params.each do |key, value|
114
+ case value
115
+ when ::File, ::Tempfile
116
+ # TODO hardcode to application/octet-stream, need better way to detect content type
117
+ data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
118
+ when ::Array, nil
119
+ # let Faraday handle Array and nil parameters
120
+ data[key] = value
121
+ else
122
+ data[key] = value.to_s
123
+ end
124
+ end
125
+ elsif body
126
+ data = body.is_a?(String) ? body : body.to_json
127
+ else
128
+ data = nil
129
+ end
130
+ data
131
+ end
132
+
133
+ def download_file(request)
134
+ @stream = []
135
+
136
+ # handle streaming Responses
137
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
138
+ @stream << chunk
139
+ end
140
+ end
@@ -0,0 +1,379 @@
1
+ =begin
2
+ {{> api_info}}
3
+ =end
4
+
5
+ module {{moduleName}}
6
+ class Configuration
7
+ # Defines url scheme
8
+ attr_accessor :scheme
9
+
10
+ # Defines url host
11
+ attr_accessor :host
12
+
13
+ # Defines url base path
14
+ attr_accessor :base_path
15
+
16
+ # Define server configuration index
17
+ attr_accessor :server_index
18
+
19
+ # Define server operation configuration index
20
+ attr_accessor :server_operation_index
21
+
22
+ # Default server variables
23
+ attr_accessor :server_variables
24
+
25
+ # Default server operation variables
26
+ attr_accessor :server_operation_variables
27
+
28
+ # Defines API keys used with API Key authentications.
29
+ #
30
+ # @return [Hash] key: parameter name, value: parameter value (API key)
31
+ #
32
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
33
+ # config.api_key['api_key'] = 'xxx'
34
+ attr_accessor :api_key
35
+
36
+ # Defines API key prefixes used with API Key authentications.
37
+ #
38
+ # @return [Hash] key: parameter name, value: API key prefix
39
+ #
40
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
41
+ # config.api_key_prefix['api_key'] = 'Token'
42
+ attr_accessor :api_key_prefix
43
+
44
+ # Defines the username used with HTTP basic authentication.
45
+ #
46
+ # @return [String]
47
+ attr_accessor :username
48
+
49
+ # Defines the password used with HTTP basic authentication.
50
+ #
51
+ # @return [String]
52
+ attr_accessor :password
53
+
54
+ # Defines the access token (Bearer) used with OAuth2.
55
+ attr_accessor :access_token
56
+
57
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
58
+ # details will be logged with `logger.debug` (see the `logger` attribute).
59
+ # Default to false.
60
+ #
61
+ # @return [true, false]
62
+ attr_accessor :debugging
63
+
64
+ # Defines the logger used for debugging.
65
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
66
+ #
67
+ # @return [#debug]
68
+ attr_accessor :logger
69
+
70
+ # Defines the temporary folder to store downloaded files
71
+ # (for API endpoints that have file response).
72
+ # Default to use `Tempfile`.
73
+ #
74
+ # @return [String]
75
+ attr_accessor :temp_folder_path
76
+
77
+ # The time limit for HTTP request in seconds.
78
+ # Default to 0 (never times out).
79
+ attr_accessor :timeout
80
+
81
+ # Set this to false to skip client side validation in the operation.
82
+ # Default to true.
83
+ # @return [true, false]
84
+ attr_accessor :client_side_validation
85
+
86
+ {{^isFaraday}}
87
+ {{> configuration_tls_typhoeus_partial}}
88
+ {{/isFaraday}}
89
+ {{#isFaraday}}
90
+ attr_accessor :faraday_middlewares
91
+ {{> configuration_tls_faraday_partial}}
92
+ {{/isFaraday}}
93
+ # Set this to customize parameters encoding of array parameter with multi collectionFormat.
94
+ # Default to nil.
95
+ #
96
+ # @see The params_encoding option of Ethon. Related source code:
97
+ # https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
98
+ attr_accessor :params_encoding
99
+
100
+ attr_accessor :inject_format
101
+
102
+ attr_accessor :force_ending_format
103
+
104
+ attr_accessor :server_settings
105
+
106
+ attr_accessor :user_agent
107
+
108
+ def initialize
109
+ @scheme = '{{scheme}}'
110
+ @host = '{{host}}{{#port}}:{{{.}}}{{/port}}'
111
+ @base_path = '{{contextPath}}'
112
+ @server_index = 0
113
+ @server_operation_index = {}
114
+ @server_variables = {}
115
+ @server_operation_variables = {}
116
+ @api_key = {}
117
+ @api_key_prefix = {}
118
+ @timeout = 0
119
+ @client_side_validation = true
120
+ {{#isFaraday}}
121
+ @ssl_verify = true
122
+ @ssl_verify_mode = nil
123
+ @ssl_ca_file = nil
124
+ @ssl_client_cert = nil
125
+ @ssl_client_key = nil
126
+ @faraday_middlewares = []
127
+ {{/isFaraday}}
128
+ {{^isFaraday}}
129
+ @verify_ssl = true
130
+ @verify_ssl_host = true
131
+ @params_encoding = nil
132
+ @cert_file = nil
133
+ @key_file = nil
134
+ {{/isFaraday}}
135
+ @debugging = false
136
+ @inject_format = false
137
+ @force_ending_format = false
138
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
139
+ @server_settings = default_server_settings
140
+ @user_agent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/#{VERSION}/ruby{{/httpUserAgent}}"
141
+
142
+ timesheets_initialize
143
+
144
+ yield(self) if block_given?
145
+ end
146
+
147
+ ###################################
148
+ # This is newly added / overrides #
149
+ # to support specific for our app #
150
+ # #
151
+ # This will need to be handled #
152
+ # differently when we change auth #
153
+ # schemes #
154
+ ###################################
155
+ def timesheets_initialize
156
+ @api_key_prefix['authToken'] = 'Bearer'
157
+ @user_agent = "quake/timesheets-client/ruby/#{VERSION}"
158
+ end
159
+
160
+ def endpoint=(value)
161
+ @server_settings = [{ url: value }]
162
+ end
163
+ ###################################
164
+ # end of overrides #
165
+ ###################################
166
+
167
+ # The default Configuration object.
168
+ def self.default
169
+ @@default ||= Configuration.new
170
+ end
171
+
172
+ def configure
173
+ yield(self) if block_given?
174
+ end
175
+
176
+ def scheme=(scheme)
177
+ # remove :// from scheme
178
+ @scheme = scheme.sub(/:\/\//, '')
179
+ end
180
+
181
+ def host=(host)
182
+ # remove http(s):// and anything after a slash
183
+ @host = host.sub(/https?:\/\//, '').split('/').first
184
+ end
185
+
186
+ def base_path=(base_path)
187
+ # Add leading and trailing slashes to base_path
188
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
189
+ @base_path = '' if @base_path == '/'
190
+ end
191
+
192
+ # Returns base URL for specified operation based on server settings
193
+ def base_url(operation = nil)
194
+ index = server_operation_index.fetch(operation, server_index)
195
+ return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
196
+
197
+ server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
198
+ end
199
+
200
+ # Gets API key (with prefix if set).
201
+ # @param [String] param_name the parameter name of API key auth
202
+ def api_key_with_prefix(param_name, param_alias = nil)
203
+ key = @api_key[param_name]
204
+ key = @api_key.fetch(param_alias, key) unless param_alias.nil?
205
+ if @api_key_prefix[param_name]
206
+ "#{@api_key_prefix[param_name]} #{key}"
207
+ else
208
+ key
209
+ end
210
+ end
211
+
212
+ # Gets Basic Auth token string
213
+ def basic_auth_token
214
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
215
+ end
216
+
217
+ # Returns Auth Settings hash for api client.
218
+ def auth_settings
219
+ {
220
+ {{#authMethods}}
221
+ {{#isApiKey}}
222
+ '{{name}}' =>
223
+ {
224
+ type: 'api_key',
225
+ in: {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
226
+ key: '{{keyParamName}}',
227
+ value: api_key_with_prefix('{{name}}'{{#vendorExtensions.x-auth-id-alias}}, '{{.}}'{{/vendorExtensions.x-auth-id-alias}})
228
+ },
229
+ {{/isApiKey}}
230
+ {{#isBasic}}
231
+ {{#isBasicBasic}}
232
+ '{{name}}' =>
233
+ {
234
+ type: 'basic',
235
+ in: 'header',
236
+ key: 'Authorization',
237
+ value: basic_auth_token
238
+ },
239
+ {{/isBasicBasic}}
240
+ {{#isBasicBearer}}
241
+ '{{name}}' =>
242
+ {
243
+ type: 'bearer',
244
+ in: 'header',
245
+ {{#bearerFormat}}
246
+ format: '{{{.}}}',
247
+ {{/bearerFormat}}
248
+ key: 'Authorization',
249
+ value: "Bearer #{access_token}"
250
+ },
251
+ {{/isBasicBearer}}
252
+ {{/isBasic}}
253
+ {{#isOAuth}}
254
+ '{{name}}' =>
255
+ {
256
+ type: 'oauth2',
257
+ in: 'header',
258
+ key: 'Authorization',
259
+ value: "Bearer #{access_token}"
260
+ },
261
+ {{/isOAuth}}
262
+ {{/authMethods}}
263
+ }
264
+ end
265
+
266
+ # Returns an array of Server setting
267
+ def default_server_settings
268
+ [
269
+ {{#servers}}
270
+ {
271
+ url: "{{{url}}}",
272
+ description: "{{{description}}}{{^description}}No description provided{{/description}}",
273
+ {{#variables}}
274
+ {{#-first}}
275
+ variables: {
276
+ {{/-first}}
277
+ {{{name}}}: {
278
+ description: "{{{description}}}{{^description}}No description provided{{/description}}",
279
+ default_value: "{{{defaultValue}}}",
280
+ {{#enumValues}}
281
+ {{#-first}}
282
+ enum_values: [
283
+ {{/-first}}
284
+ "{{{.}}}"{{^-last}},{{/-last}}
285
+ {{#-last}}
286
+ ]
287
+ {{/-last}}
288
+ {{/enumValues}}
289
+ }{{^-last}},{{/-last}}
290
+ {{#-last}}
291
+ }
292
+ {{/-last}}
293
+ {{/variables}}
294
+ }{{^-last}},{{/-last}}
295
+ {{/servers}}
296
+ ]
297
+ end
298
+
299
+ def operation_server_settings
300
+ {
301
+ {{#apiInfo}}
302
+ {{#apis}}
303
+ {{#operations}}
304
+ {{#operation}}
305
+ {{#servers}}
306
+ {{#-first}}
307
+ "{{{classname}}}.{{{nickname}}}": [
308
+ {{/-first}}
309
+ {
310
+ url: "{{{url}}}",
311
+ description: "{{{description}}}{{^description}}No description provided{{/description}}",
312
+ {{#variables}}
313
+ {{#-first}}
314
+ variables: {
315
+ {{/-first}}
316
+ {{{name}}}: {
317
+ description: "{{{description}}}{{^description}}No description provided{{/description}}",
318
+ default_value: "{{{defaultValue}}}",
319
+ {{#enumValues}}
320
+ {{#-first}}
321
+ enum_values: [
322
+ {{/-first}}
323
+ "{{{.}}}"{{^-last}},{{/-last}}
324
+ {{#-last}}
325
+ ]
326
+ {{/-last}}
327
+ {{/enumValues}}
328
+ }{{^-last}},{{/-last}}
329
+ {{#-last}}
330
+ }
331
+ {{/-last}}
332
+ {{/variables}}
333
+ }{{^-last}},{{/-last}}
334
+ {{#-last}}
335
+ ],
336
+ {{/-last}}
337
+ {{/servers}}
338
+ {{/operation}}
339
+ {{/operations}}
340
+ {{/apis}}
341
+ {{/apiInfo}}
342
+ }
343
+ end
344
+
345
+ # Returns URL based on server settings
346
+ #
347
+ # @param index array index of the server settings
348
+ # @param variables hash of variable and the corresponding value
349
+ def server_url(index, variables = {}, servers = nil)
350
+ servers = server_settings if servers == nil
351
+
352
+ # check array index out of bound
353
+ if (index < 0 || index >= servers.size)
354
+ fail ArgumentError, "Invalid index #{index} when selecting the server. Must be less than #{servers.size}"
355
+ end
356
+
357
+ server = servers[index]
358
+ url = server[:url]
359
+
360
+ return url unless server.key? :variables
361
+
362
+ # go through variable and assign a value
363
+ server[:variables].each do |name, variable|
364
+ if variables.key?(name)
365
+ if (!server[:variables][name].key?(:enum_values) || server[:variables][name][:enum_values].include?(variables[name]))
366
+ url.gsub! "{" + name.to_s + "}", variables[name]
367
+ else
368
+ fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}."
369
+ end
370
+ else
371
+ # use default value
372
+ url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value]
373
+ end
374
+ end
375
+
376
+ url
377
+ end
378
+ end
379
+ end