Class: Coinbase::Advanced::REST::RESTBase
- Includes:
- Coinbase::Advanced::REST::Resources::Accounts, Coinbase::Advanced::REST::Resources::Converts, Coinbase::Advanced::REST::Resources::DataAPI, Coinbase::Advanced::REST::Resources::Fees, Coinbase::Advanced::REST::Resources::Futures, Coinbase::Advanced::REST::Resources::Orders, Coinbase::Advanced::REST::Resources::PaymentMethods, Coinbase::Advanced::REST::Resources::Perpetuals, Coinbase::Advanced::REST::Resources::Portfolios, Coinbase::Advanced::REST::Resources::Products, Coinbase::Advanced::REST::Resources::Public
- Defined in:
- lib/coinbase/advanced/rest/rest_base.rb
Overview
RESTBase sets up, executes, and appropriately handles errors strictly within context of HTTP + REST communication
Constant Summary collapse
- AUTH_ERROR_MESSAGE =
<<~HEREDOC Unauthenticated request to private endpoint. If you wish to access private endpoints, you must \ provide your API key and secret when initializing the RESTClient. HEREDOC
Constants inherited from APIBase
APIBase::JSON_API_KEY, APIBase::JSON_API_SECRET
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#build_jwt(uri = nil) ⇒ Object
copied rb sample from https://docs.cdp.coinbase.com/advanced-trade/docs/ws-auth#code-samples See README if confused why this method is here.
- #delete(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
- #get(url_path, params = {}, auth_required: true) ⇒ Object
-
#initialize(config = Coinbase::Advanced.config) ⇒ RESTBase
constructor
A new instance of RESTBase.
- #post(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
- #prepare_and_send_request(http_method, url_path, params = {}, data = {}, auth_required: true) ⇒ Object
- #put(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
-
#send_request(http_method, url_path, data = {}) ⇒ Object
def send_request(http_method, url_path, params, headers, data = None) - py method signature, lot of redundant shit.
- #set_headers(method, path) ⇒ Object
Methods included from Coinbase::Advanced::REST::Resources::DataAPI
Methods included from Coinbase::Advanced::REST::Resources::PaymentMethods
#get_payment_method, #list_payment_methods
Methods included from Coinbase::Advanced::REST::Resources::Public
#get_public_market_trades, #get_public_product, #get_public_product_book, #get_public_product_candles, #get_server_time, #list_public_products
Methods included from Coinbase::Advanced::REST::Resources::Fees
Methods included from Coinbase::Advanced::REST::Resources::Converts
#commit_convert_trade, #create_convert_quote, #get_convert_trade
Methods included from Coinbase::Advanced::REST::Resources::Perpetuals
#allocate_portfolio, #get_perpetuals_portfolio_summary, #get_perpetuals_position, #get_portfolios_balances, #list_perpetuals_positions, #opt_in_or_out_of_multi_asset_collateral
Methods included from Coinbase::Advanced::REST::Resources::Futures
#cancel_pending_futures_sweep, #get_current_margin_window, #get_futures_balance_summary, #get_futures_position, #get_intraday_margin_setting, #list_futures_positions, #list_futures_sweeps, #schedule_futures_sweep, #set_intraday_margin_setting
Methods included from Coinbase::Advanced::REST::Resources::Portfolios
#create_portfolio, #delete_portfolio, #edit_portfolio, #get_portfolio_funds, #list_portfolios, #move_portfolio_funds
Methods included from Coinbase::Advanced::REST::Resources::Orders
#cancel_orders, #close_position, #create_order, #edit_order, #edit_order_preview, #get_order, #list_fills, #list_orders, #preview_order
Methods included from Coinbase::Advanced::REST::Resources::Products
#get_best_bid_ask, #get_market_trades, #get_product, #get_product_book, #get_product_candles, #list_products
Methods included from Coinbase::Advanced::REST::Resources::Accounts
Methods inherited from APIBase
#authenticate, #parse_key_file
Constructor Details
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
8 9 10 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 8 def session @session end |
Instance Method Details
#build_jwt(uri = nil) ⇒ Object
copied rb sample from https://docs.cdp.coinbase.com/advanced-trade/docs/ws-auth#code-samples See README if confused why this method is here
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 46 def build_jwt(uri = nil) header = { typ: "JWT", kid: @api_key, nonce: SecureRandom.hex(16) } claims = { sub: @api_key, iss: "cdp", aud: ["cdp_service"], nbf: Time.now.to_i, exp: Time.now.to_i + 120 # Expiration time: 2 minute from now. } claims[:uri] = uri if uri private_key = OpenSSL::PKey.read(@api_secret) JWT.encode(claims, private_key, "ES256", header) end |
#delete(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
111 112 113 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 111 def delete(url_path, params = {}, data = {}, auth_required: true) prepare_and_send_request(:delete, url_path, params, data, auth_required: auth_required) end |
#get(url_path, params = {}, auth_required: true) ⇒ Object
99 100 101 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 99 def get(url_path, params = {}, auth_required: true) prepare_and_send_request(:get, url_path, params, {}, auth_required: auth_required) end |
#post(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
103 104 105 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 103 def post(url_path, params = {}, data = {}, auth_required: true) prepare_and_send_request(:post, url_path, params, data, auth_required: auth_required) end |
#prepare_and_send_request(http_method, url_path, params = {}, data = {}, auth_required: true) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 67 def prepare_and_send_request(http_method, url_path, params = {}, data = {}, auth_required: true) raise AuthenticationError, AUTH_ERROR_MESSAGE if auth_required && !@authenticated url_path = "#{@api_prefix}/#{url_path.dup}" set_headers(http_method, url_path) @session.params = params unless params.empty? url_path.insert(0, "/") if url_path[0] != "/" send_request(http_method, url_path, data) end |
#put(url_path, params = {}, data = {}, auth_required: true) ⇒ Object
107 108 109 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 107 def put(url_path, params = {}, data = {}, auth_required: true) prepare_and_send_request(:put, url_path, params, data, auth_required: auth_required) end |
#send_request(http_method, url_path, data = {}) ⇒ Object
def send_request(http_method, url_path, params, headers, data = None) - py method signature, lot of redundant shit
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 80 def send_request(http_method, url_path, data = {}) url = URI::HTTPS.build(host: @base_url, path: url_path).to_s @config.log("Sending #{http_method.to_s.upcase}:#{url} with body #{data.to_json}") if @verbose response = case http_method when :get then @session.get(url) when :post then @session.post(url, data.to_json) when :put then @session.put(url, data.to_json) when :delete then @session.delete(url, data.to_json) else raise URI::BadURIError, "Unknown HTTP method / verb: '#{http_method}'" end @config.log("Response object:\n\n#{response.pretty_inspect}") if @verbose BaseResponse.new(JSON.parse(response.body)) end |
#set_headers(method, path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/coinbase/advanced/rest/rest_base.rb', line 32 def set_headers(method, path) @session.headers = { "User-Agent" => Coinbase::Advanced::USER_AGENT, "Content-Type" => "application/json" } return unless @authenticated uri = "#{method} #{@base_url}#{path}" @session.headers["Authorization"] = "Bearer #{build_jwt(uri)}" end |