Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/bitgo_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,25 @@ def initialize(access_token)
@access_token = access_token
end

def request(url, payload = nil, method: :get, logger: nil)
def request(url, payload = nil, method: :get, logger: nil, proxy: nil)
body = payload.to_json if payload

log logger, "Request url: #{url}, method: #{method}, body:"
log logger, payload

request = Typhoeus::Request.new(
url,
options = {
method: method,
headers: {
"Authorization" => "Bearer #{access_token}",
"Content-Type" => "application/json"
},
body: body
)
}

unless String(proxy).empty?
options[:proxy] = proxy
end

request = Typhoeus::Request.new(url, options)
request.run

response = request.response
Expand Down
79 changes: 56 additions & 23 deletions lib/bitgo_client/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,92 @@ def client
BitgoClient::Client.new(access_token)
end

def create_address(wallet_id, coin_code: :tbtc, logger: nil)
client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/address", method: :post, logger: logger)
end

def address(wallet_id, address, coin_code: :tbtc, logger: nil)
client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/address/#{address}", logger: logger)
def create_address(wallet_id, coin_code: :tbtc, logger: nil, proxy: nil)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/address",
method: :post,
logger: logger,
proxy: proxy,
)
end

def fee(coin_code: :tbtc, logger: nil)
client.request("#{base_path}/#{coin_code}/tx/fee", logger: logger)
def address(wallet_id, address, coin_code: :tbtc, logger: nil, proxy: nil)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/address/#{address}",
logger: logger,
proxy: proxy,
)
end

def get_transfer(wallet_id, transfer_id, coin_code: :tbtc, logger: nil)
client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/transfer/#{transfer_id}", logger: logger)
def fee(coin_code: :tbtc, logger: nil, proxy: nil)
client.request(
"#{base_path}/#{coin_code}/tx/fee",
logger: logger,
proxy: proxy,
)
end

def send_transaction(wallet_id, payload, coin_code: :tbtc, logger: nil)
def get_transfer(wallet_id, transfer_id, coin_code: :tbtc, logger: nil, proxy: nil)
client.request(
"#{express_path}/api/v2/#{coin_code}/wallet/#{wallet_id}/sendcoins",
payload,
method: :post,
logger: logger
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/transfer/#{transfer_id}",
logger: logger,
proxy: proxy,
)
end

def transactions(wallet_id, coin_code: :tbtc, logger: nil, limit: 25, prev_id: nil, all_tokens: nil)
def transactions(wallet_id, coin_code: :tbtc, logger: nil, limit: 25, prev_id: nil, all_tokens: nil, proxy: nil)
query_string = build_query_string(
limit: limit,
prevId: prev_id,
allTokens: all_tokens
)

client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/tx?#{query_string}", logger: logger)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/tx?#{query_string}",
logger: logger,
proxy: proxy,
)
end

def transaction(wallet_id, transaction_id, coin_code: :tbtc, logger: nil)
client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/tx/#{transaction_id}", logger: logger)
def transaction(wallet_id, transaction_id, coin_code: :tbtc, logger: nil, proxy: nil)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/tx/#{transaction_id}",
logger: logger,
proxy: proxy,
)
end

def transfers(wallet_id, coin_code: :tbtc, logger: nil, limit: 25, prev_id: nil, all_tokens: nil)
def transfers(wallet_id, coin_code: :tbtc, logger: nil, limit: 25, prev_id: nil, all_tokens: nil, proxy: nil)
query_string = build_query_string(
limit: limit,
prevId: prev_id,
allTokens: all_tokens
)

client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}/transfer?#{query_string}", logger: logger)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}/transfer?#{query_string}",
logger: logger,
proxy: proxy,
)
end

def wallet(wallet_id, coin_code: :tbtc, logger: nil, all_tokens: nil)
def wallet(wallet_id, coin_code: :tbtc, logger: nil, all_tokens: nil, proxy: nil)
query_string = build_query_string(allTokens: all_tokens)

client.request("#{base_path}/#{coin_code}/wallet/#{wallet_id}?#{query_string}", logger: logger)
client.request(
"#{base_path}/#{coin_code}/wallet/#{wallet_id}?#{query_string}",
logger: logger,
proxy: proxy,
)
end

def send_transaction(wallet_id, payload, coin_code: :tbtc, logger: nil)
client.request(
"#{express_path}/api/v2/#{coin_code}/wallet/#{wallet_id}/sendcoins",
payload,
method: :post,
logger: logger
)
end

private
Expand Down
1 change: 0 additions & 1 deletion spec/bitgo_client/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
subject(:client) { described_class.new(WebmockHelper::TOKEN) }

let(:base_path) { WebmockHelper::BASE_PATH }

let(:headers) do
{
"Authorization" => "Bearer #{WebmockHelper::TOKEN}",
Expand Down
32 changes: 16 additions & 16 deletions spec/bitgo_client/v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
it "calls client request with the correct path" do
api.wallet(wallet_id)

expect(client).to have_received(:request).with("#{api.base_path}/tbtc/wallet/#{wallet_id}?", logger: nil)
expect(client).to have_received(:request).with("#{api.base_path}/tbtc/wallet/#{wallet_id}?", logger: nil, proxy: nil)
end
end

context "with specific coin_code" do
it "calls client request with the correct path" do
api.wallet(wallet_id, coin_code: :xxx)

expect(client).to have_received(:request).with("#{api.base_path}/xxx/wallet/#{wallet_id}?", logger: nil)
expect(client).to have_received(:request).with("#{api.base_path}/xxx/wallet/#{wallet_id}?", logger: nil, proxy: nil)
end
end
end
Expand All @@ -75,7 +75,7 @@
api.create_address(wallet_id)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/address", method: :post, logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/address", method: :post, logger: nil, proxy: nil)
end
end

Expand All @@ -84,7 +84,7 @@
api.create_address(wallet_id, coin_code: :xxx)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/address", method: :post, logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/address", method: :post, logger: nil, proxy: nil)
end
end
end
Expand All @@ -97,7 +97,7 @@
api.address(wallet_id, address)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/address/#{address}", logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/address/#{address}", logger: nil, proxy: nil)
end
end

Expand All @@ -106,7 +106,7 @@
api.address(wallet_id, address, coin_code: :xxx)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/address/#{address}", logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/address/#{address}", logger: nil, proxy: nil)
end
end
end
Expand Down Expand Up @@ -139,7 +139,7 @@
api.transactions(wallet_id)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/tx?limit=25", logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/tx?limit=25", logger: nil, proxy: nil)
end
end

Expand All @@ -148,7 +148,7 @@
api.transactions(wallet_id, coin_code: :xxx, limit: 250, prev_id: "xxx42", all_tokens: true)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/tx?allTokens=true&limit=250&prevId=xxx42", logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/tx?allTokens=true&limit=250&prevId=xxx42", logger: nil, proxy: nil)
end
end
end
Expand All @@ -161,7 +161,7 @@
api.transaction(wallet_id, tx_id)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/tx/#{tx_id}", logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/tx/#{tx_id}", logger: nil, proxy: nil)
end
end

Expand All @@ -170,7 +170,7 @@
api.transaction(wallet_id, tx_id, coin_code: :xxx)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/tx/#{tx_id}", logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/tx/#{tx_id}", logger: nil, proxy: nil)
end
end
end
Expand All @@ -181,7 +181,7 @@
api.fee

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/tx/fee", logger: nil)
.with("#{api.base_path}/tbtc/tx/fee", logger: nil, proxy: nil)
end
end

Expand All @@ -190,7 +190,7 @@
api.fee(coin_code: :xxx)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/tx/fee", logger: nil)
.with("#{api.base_path}/xxx/tx/fee", logger: nil, proxy: nil)
end
end
end
Expand All @@ -201,7 +201,7 @@
api.transfers(wallet_id)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/transfer?limit=25", logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/transfer?limit=25", logger: nil, proxy: nil)
end
end

Expand All @@ -210,7 +210,7 @@
api.transfers(wallet_id, coin_code: :xxx, limit: 250, prev_id: "xxx42", all_tokens: true)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/transfer?allTokens=true&limit=250&prevId=xxx42", logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/transfer?allTokens=true&limit=250&prevId=xxx42", logger: nil, proxy: nil)
end
end
end
Expand All @@ -223,7 +223,7 @@
api.get_transfer(wallet_id, tx_id)

expect(client).to have_received(:request)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/transfer/#{tx_id}", logger: nil)
.with("#{api.base_path}/tbtc/wallet/#{wallet_id}/transfer/#{tx_id}", logger: nil, proxy: nil)
end
end

Expand All @@ -232,7 +232,7 @@
api.get_transfer(wallet_id, tx_id, coin_code: :xxx)

expect(client).to have_received(:request)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/transfer/#{tx_id}", logger: nil)
.with("#{api.base_path}/xxx/wallet/#{wallet_id}/transfer/#{tx_id}", logger: nil, proxy: nil)
end
end
end
Expand Down