coinbase-sdk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/coinbase/address.rb +88 -49
  3. data/lib/coinbase/asset.rb +2 -1
  4. data/lib/coinbase/authenticator.rb +52 -0
  5. data/lib/coinbase/balance_map.rb +1 -1
  6. data/lib/coinbase/client/api/addresses_api.rb +385 -0
  7. data/lib/coinbase/client/api/transfers_api.rb +256 -0
  8. data/lib/coinbase/client/api/users_api.rb +79 -0
  9. data/lib/coinbase/client/api/wallets_api.rb +348 -0
  10. data/lib/coinbase/client/api_client.rb +431 -0
  11. data/lib/coinbase/client/api_error.rb +58 -0
  12. data/lib/coinbase/client/configuration.rb +375 -0
  13. data/lib/coinbase/client/models/address.rb +273 -0
  14. data/lib/coinbase/client/models/address_balance_list.rb +275 -0
  15. data/lib/coinbase/client/models/address_list.rb +275 -0
  16. data/lib/coinbase/client/models/asset.rb +260 -0
  17. data/lib/coinbase/client/models/balance.rb +239 -0
  18. data/lib/coinbase/client/models/create_address_request.rb +239 -0
  19. data/lib/coinbase/client/models/create_transfer_request.rb +273 -0
  20. data/lib/coinbase/client/models/create_wallet_request.rb +221 -0
  21. data/lib/coinbase/client/models/error.rb +278 -0
  22. data/lib/coinbase/client/models/transfer.rb +393 -0
  23. data/lib/coinbase/client/models/transfer_list.rb +275 -0
  24. data/lib/coinbase/client/models/user.rb +231 -0
  25. data/lib/coinbase/client/models/wallet.rb +241 -0
  26. data/lib/coinbase/client/models/wallet_list.rb +275 -0
  27. data/lib/coinbase/client/version.rb +15 -0
  28. data/lib/coinbase/client.rb +57 -0
  29. data/lib/coinbase/constants.rb +5 -1
  30. data/lib/coinbase/middleware.rb +21 -0
  31. data/lib/coinbase/network.rb +2 -2
  32. data/lib/coinbase/transfer.rb +69 -54
  33. data/lib/coinbase/user.rb +64 -0
  34. data/lib/coinbase/wallet.rb +146 -54
  35. data/lib/coinbase.rb +73 -9
  36. metadata +74 -6
@@ -0,0 +1,431 @@
1
+ =begin
2
+ #Coinbase Platform API
3
+
4
+ #This is the OpenAPI 3.0 specification for the Coinbase Platform APIs, used in conjunction with the Coinbase Platform SDKs.
5
+
6
+ The version of the OpenAPI document: 0.0.1-alpha
7
+ Contact: yuga.cohler@coinbase.com
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.5.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'time'
18
+ require 'faraday'
19
+ require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
20
+ require 'marcel'
21
+
22
+
23
+ module Coinbase::Client
24
+ class ApiClient
25
+ # The Configuration object holding settings to be used in the API client.
26
+ attr_accessor :config
27
+
28
+ # Defines the headers to be used in HTTP requests of all API calls by default.
29
+ #
30
+ # @return [Hash]
31
+ attr_accessor :default_headers
32
+
33
+ # Initializes the ApiClient
34
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
35
+ def initialize(config = Configuration.default)
36
+ @config = config
37
+ @user_agent = "OpenAPI-Generator/#{VERSION}/ruby"
38
+ @default_headers = {
39
+ 'Content-Type' => 'application/json',
40
+ 'User-Agent' => @user_agent
41
+ }
42
+ end
43
+
44
+ def self.default
45
+ @@default ||= ApiClient.new
46
+ end
47
+
48
+ # Call an API with given options.
49
+ #
50
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
51
+ # the data deserialized from response body (could be nil), response status code and response headers.
52
+ def call_api(http_method, path, opts = {})
53
+ stream = nil
54
+ begin
55
+ response = connection(opts).public_send(http_method.to_sym.downcase) do |req|
56
+ request = build_request(http_method, path, req, opts)
57
+ stream = download_file(request) if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
58
+ end
59
+
60
+ if config.debugging
61
+ config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
62
+ end
63
+
64
+ unless response.success?
65
+ if response.status == 0 && response.respond_to?(:return_message)
66
+ # Errors from libcurl will be made visible here
67
+ fail ApiError.new(code: 0,
68
+ message: response.return_message)
69
+ else
70
+ fail ApiError.new(code: response.status,
71
+ response_headers: response.headers,
72
+ response_body: response.body),
73
+ response.reason_phrase
74
+ end
75
+ end
76
+ rescue Faraday::TimeoutError
77
+ fail ApiError.new('Connection timed out')
78
+ rescue Faraday::ConnectionFailed
79
+ fail ApiError.new('Connection failed')
80
+ end
81
+
82
+ if opts[:return_type] == 'File' || opts[:return_type] == 'Binary'
83
+ data = deserialize_file(response, stream)
84
+ elsif opts[:return_type]
85
+ data = deserialize(response, opts[:return_type])
86
+ else
87
+ data = nil
88
+ end
89
+ return data, response.status, response.headers
90
+ end
91
+
92
+ # Builds the HTTP request
93
+ #
94
+ # @param [String] http_method HTTP method/verb (e.g. POST)
95
+ # @param [String] path URL path (e.g. /account/new)
96
+ # @option opts [Hash] :header_params Header parameters
97
+ # @option opts [Hash] :query_params Query parameters
98
+ # @option opts [Hash] :form_params Query parameters
99
+ # @option opts [Object] :body HTTP body (JSON/XML)
100
+ # @return [Faraday::Request] A Faraday Request
101
+ def build_request(http_method, path, request, opts = {})
102
+ url = build_request_url(path, opts)
103
+ http_method = http_method.to_sym.downcase
104
+
105
+ header_params = @default_headers.merge(opts[:header_params] || {})
106
+ query_params = opts[:query_params] || {}
107
+ form_params = opts[:form_params] || {}
108
+
109
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
110
+
111
+ if [:post, :patch, :put, :delete].include?(http_method)
112
+ req_body = build_request_body(header_params, form_params, opts[:body])
113
+ if config.debugging
114
+ config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
115
+ end
116
+ end
117
+ request.headers = header_params
118
+ request.body = req_body
119
+
120
+ # Overload default options only if provided
121
+ request.options.params_encoder = config.params_encoder if config.params_encoder
122
+ request.options.timeout = config.timeout if config.timeout
123
+
124
+ request.url url
125
+ request.params = query_params
126
+ request
127
+ end
128
+
129
+ # Builds the HTTP request body
130
+ #
131
+ # @param [Hash] header_params Header parameters
132
+ # @param [Hash] form_params Query parameters
133
+ # @param [Object] body HTTP body (JSON/XML)
134
+ # @return [String] HTTP body data in the form of string
135
+ def build_request_body(header_params, form_params, body)
136
+ # http form
137
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
138
+ data = URI.encode_www_form(form_params)
139
+ elsif header_params['Content-Type'] == 'multipart/form-data'
140
+ data = {}
141
+ form_params.each do |key, value|
142
+ case value
143
+ when ::File, ::Tempfile
144
+ data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path)))
145
+ when ::Array, nil
146
+ # let Faraday handle Array and nil parameters
147
+ data[key] = value
148
+ else
149
+ data[key] = value.to_s
150
+ end
151
+ end
152
+ elsif body
153
+ data = body.is_a?(String) ? body : body.to_json
154
+ else
155
+ data = nil
156
+ end
157
+ data
158
+ end
159
+
160
+ def download_file(request)
161
+ stream = []
162
+
163
+ # handle streaming Responses
164
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
165
+ stream << chunk
166
+ end
167
+ stream
168
+ end
169
+
170
+ def deserialize_file(response, stream)
171
+ body = response.body
172
+ if @config.return_binary_data == true
173
+ # return byte stream
174
+ encoding = body.encoding
175
+ stream.join.force_encoding(encoding)
176
+ else
177
+ # return file instead of binary data
178
+ content_disposition = response.headers['Content-Disposition']
179
+ if content_disposition && content_disposition =~ /filename=/i
180
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
181
+ prefix = sanitize_filename(filename)
182
+ else
183
+ prefix = 'download-'
184
+ end
185
+ prefix = prefix + '-' unless prefix.end_with?('-')
186
+ encoding = body.encoding
187
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
188
+ tempfile.write(stream.join.force_encoding(encoding))
189
+ tempfile.close
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
+ end
197
+
198
+ def connection(opts)
199
+ opts[:header_params]['Content-Type'] == 'multipart/form-data' ? connection_multipart : connection_regular
200
+ end
201
+
202
+ def connection_multipart
203
+ @connection_multipart ||= build_connection do |conn|
204
+ conn.request :multipart
205
+ conn.request :url_encoded
206
+ end
207
+ end
208
+
209
+ def connection_regular
210
+ @connection_regular ||= build_connection
211
+ end
212
+
213
+ def build_connection
214
+ Faraday.new(url: config.base_url, ssl: ssl_options, proxy: config.proxy) do |conn|
215
+ basic_auth(conn)
216
+ config.configure_middleware(conn)
217
+ yield(conn) if block_given?
218
+ conn.adapter(Faraday.default_adapter)
219
+ config.configure_connection(conn)
220
+ end
221
+ end
222
+
223
+ def ssl_options
224
+ {
225
+ ca_file: config.ssl_ca_file,
226
+ verify: config.ssl_verify,
227
+ verify_mode: config.ssl_verify_mode,
228
+ client_cert: config.ssl_client_cert,
229
+ client_key: config.ssl_client_key
230
+ }
231
+ end
232
+
233
+ def basic_auth(conn)
234
+ if config.username && config.password
235
+ if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
236
+ conn.request(:authorization, :basic, config.username, config.password)
237
+ else
238
+ conn.request(:basic_auth, config.username, config.password)
239
+ end
240
+ end
241
+ end
242
+
243
+ # Check if the given MIME is a JSON MIME.
244
+ # JSON MIME examples:
245
+ # application/json
246
+ # application/json; charset=UTF8
247
+ # APPLICATION/JSON
248
+ # */*
249
+ # @param [String] mime MIME
250
+ # @return [Boolean] True if the MIME is application/json
251
+ def json_mime?(mime)
252
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
253
+ end
254
+
255
+ # Deserialize the response to the given return type.
256
+ #
257
+ # @param [Response] response HTTP response
258
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
259
+ def deserialize(response, return_type)
260
+ body = response.body
261
+ return nil if body.nil? || body.empty?
262
+
263
+ # return response body directly for String return type
264
+ return body.to_s if return_type == 'String'
265
+
266
+ # ensuring a default content type
267
+ content_type = response.headers['Content-Type'] || 'application/json'
268
+
269
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
270
+
271
+ begin
272
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
273
+ rescue JSON::ParserError => e
274
+ if %w(String Date Time).include?(return_type)
275
+ data = body
276
+ else
277
+ raise e
278
+ end
279
+ end
280
+
281
+ convert_to_type data, return_type
282
+ end
283
+
284
+ # Convert data to the given return type.
285
+ # @param [Object] data Data to be converted
286
+ # @param [String] return_type Return type
287
+ # @return [Mixed] Data in a particular type
288
+ def convert_to_type(data, return_type)
289
+ return nil if data.nil?
290
+ case return_type
291
+ when 'String'
292
+ data.to_s
293
+ when 'Integer'
294
+ data.to_i
295
+ when 'Float'
296
+ data.to_f
297
+ when 'Boolean'
298
+ data == true
299
+ when 'Time'
300
+ # parse date time (expecting ISO 8601 format)
301
+ Time.parse data
302
+ when 'Date'
303
+ # parse date time (expecting ISO 8601 format)
304
+ Date.parse data
305
+ when 'Object'
306
+ # generic object (usually a Hash), return directly
307
+ data
308
+ when /\AArray<(.+)>\z/
309
+ # e.g. Array<Pet>
310
+ sub_type = $1
311
+ data.map { |item| convert_to_type(item, sub_type) }
312
+ when /\AHash\<String, (.+)\>\z/
313
+ # e.g. Hash<String, Integer>
314
+ sub_type = $1
315
+ {}.tap do |hash|
316
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
317
+ end
318
+ else
319
+ # models (e.g. Pet) or oneOf
320
+ klass = Coinbase::Client.const_get(return_type)
321
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
322
+ end
323
+ end
324
+
325
+ # Sanitize filename by removing path.
326
+ # e.g. ../../sun.gif becomes sun.gif
327
+ #
328
+ # @param [String] filename the filename to be sanitized
329
+ # @return [String] the sanitized filename
330
+ def sanitize_filename(filename)
331
+ filename.gsub(/.*[\/\\]/, '')
332
+ end
333
+
334
+ def build_request_url(path, opts = {})
335
+ # Add leading and trailing slashes to path
336
+ path = "/#{path}".gsub(/\/+/, '/')
337
+ @config.base_url(opts[:operation]) + path
338
+ end
339
+
340
+ # Update header and query params based on authentication settings.
341
+ #
342
+ # @param [Hash] header_params Header parameters
343
+ # @param [Hash] query_params Query parameters
344
+ # @param [String] auth_names Authentication scheme name
345
+ def update_params_for_auth!(header_params, query_params, auth_names)
346
+ Array(auth_names).each do |auth_name|
347
+ auth_setting = @config.auth_settings[auth_name]
348
+ next unless auth_setting
349
+ case auth_setting[:in]
350
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
351
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
352
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
353
+ end
354
+ end
355
+ end
356
+
357
+ # Sets user agent in HTTP header
358
+ #
359
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
360
+ def user_agent=(user_agent)
361
+ @user_agent = user_agent
362
+ @default_headers['User-Agent'] = @user_agent
363
+ end
364
+
365
+ # Return Accept header based on an array of accepts provided.
366
+ # @param [Array] accepts array for Accept
367
+ # @return [String] the Accept header (e.g. application/json)
368
+ def select_header_accept(accepts)
369
+ return nil if accepts.nil? || accepts.empty?
370
+ # use JSON when present, otherwise use all of the provided
371
+ json_accept = accepts.find { |s| json_mime?(s) }
372
+ json_accept || accepts.join(',')
373
+ end
374
+
375
+ # Return Content-Type header based on an array of content types provided.
376
+ # @param [Array] content_types array for Content-Type
377
+ # @return [String] the Content-Type header (e.g. application/json)
378
+ def select_header_content_type(content_types)
379
+ # return nil by default
380
+ return if content_types.nil? || content_types.empty?
381
+ # use JSON when present, otherwise use the first one
382
+ json_content_type = content_types.find { |s| json_mime?(s) }
383
+ json_content_type || content_types.first
384
+ end
385
+
386
+ # Convert object (array, hash, object, etc) to JSON string.
387
+ # @param [Object] model object to be converted into JSON string
388
+ # @return [String] JSON string representation of the object
389
+ def object_to_http_body(model)
390
+ return model if model.nil? || model.is_a?(String)
391
+ local_body = nil
392
+ if model.is_a?(Array)
393
+ local_body = model.map { |m| object_to_hash(m) }
394
+ else
395
+ local_body = object_to_hash(model)
396
+ end
397
+ local_body.to_json
398
+ end
399
+
400
+ # Convert object(non-array) to hash.
401
+ # @param [Object] obj object to be converted into JSON string
402
+ # @return [String] JSON string representation of the object
403
+ def object_to_hash(obj)
404
+ if obj.respond_to?(:to_hash)
405
+ obj.to_hash
406
+ else
407
+ obj
408
+ end
409
+ end
410
+
411
+ # Build parameter value according to the given collection format.
412
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
413
+ def build_collection_param(param, collection_format)
414
+ case collection_format
415
+ when :csv
416
+ param.join(',')
417
+ when :ssv
418
+ param.join(' ')
419
+ when :tsv
420
+ param.join("\t")
421
+ when :pipes
422
+ param.join('|')
423
+ when :multi
424
+ # return the array directly as typhoeus will handle it as expected
425
+ param
426
+ else
427
+ fail "unknown collection format: #{collection_format.inspect}"
428
+ end
429
+ end
430
+ end
431
+ end
@@ -0,0 +1,58 @@
1
+ =begin
2
+ #Coinbase Platform API
3
+
4
+ #This is the OpenAPI 3.0 specification for the Coinbase Platform APIs, used in conjunction with the Coinbase Platform SDKs.
5
+
6
+ The version of the OpenAPI document: 0.0.1-alpha
7
+ Contact: yuga.cohler@coinbase.com
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.5.0
10
+
11
+ =end
12
+
13
+ module Coinbase::Client
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
+ @message = arg
36
+ end
37
+ end
38
+
39
+ # Override to_s to display a friendly error message
40
+ def to_s
41
+ message
42
+ end
43
+
44
+ def message
45
+ if @message.nil?
46
+ msg = "Error message: the server returns an error"
47
+ else
48
+ msg = @message
49
+ end
50
+
51
+ msg += "\nHTTP status code: #{code}" if code
52
+ msg += "\nResponse headers: #{response_headers}" if response_headers
53
+ msg += "\nResponse body: #{response_body}" if response_body
54
+
55
+ msg
56
+ end
57
+ end
58
+ end