DRY up btcpay and lndhub services
Removing initialize methods from the main/manager class also allows for different iniitalizers in specific task services
This commit is contained in:
parent
fee951c05c
commit
69b3afb8f7
@ -1,7 +1,7 @@
|
|||||||
module BtcpayManager
|
module BtcpayManager
|
||||||
class FetchLightningWalletBalance < BtcpayManagerService
|
class FetchLightningWalletBalance < BtcpayManagerService
|
||||||
def call
|
def call
|
||||||
res = get "stores/#{store_id}/lightning/BTC/balance"
|
res = get "/lightning/BTC/balance"
|
||||||
|
|
||||||
{
|
{
|
||||||
confirmed_balance: res["offchain"]["local"].to_i / 1000 # msats to sats
|
confirmed_balance: res["offchain"]["local"].to_i / 1000 # msats to sats
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
module BtcpayManager
|
module BtcpayManager
|
||||||
class FetchOnchainWalletBalance < BtcpayManagerService
|
class FetchOnchainWalletBalance < BtcpayManagerService
|
||||||
def call
|
def call
|
||||||
res = get "stores/#{store_id}/payment-methods/onchain/BTC/wallet"
|
res = get "/payment-methods/onchain/BTC/wallet"
|
||||||
|
|
||||||
{
|
{
|
||||||
balance: (res["balance"].to_f * 100000000).to_i, # BTC to sats
|
balance: (res["balance"].to_f * 100000000).to_i, # BTC to sats
|
||||||
|
@ -2,23 +2,35 @@
|
|||||||
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
||||||
#
|
#
|
||||||
class BtcpayManagerService < ApplicationService
|
class BtcpayManagerService < ApplicationService
|
||||||
attr_reader :base_url, :store_id, :auth_token
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@base_url = Setting.btcpay_api_url
|
|
||||||
@store_id = Setting.btcpay_store_id
|
|
||||||
@auth_token = Setting.btcpay_auth_token
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get(endpoint)
|
def base_url
|
||||||
res = Faraday.get("#{base_url}/#{endpoint}", {}, {
|
@base_url ||= "#{Setting.btcpay_api_url}/stores/#{Setting.btcpay_store_id}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def auth_token
|
||||||
|
@auth_token ||= Setting.btcpay_auth_token
|
||||||
|
end
|
||||||
|
|
||||||
|
def headers
|
||||||
|
{
|
||||||
"Content-Type" => "application/json",
|
"Content-Type" => "application/json",
|
||||||
"Accept" => "application/json",
|
"Accept" => "application/json",
|
||||||
"Authorization" => "token #{auth_token}"
|
"Authorization" => "token #{auth_token}"
|
||||||
})
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def endpoint_url(path)
|
||||||
|
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(path)
|
||||||
|
res = Faraday.get endpoint_url(path), {}, headers
|
||||||
|
JSON.parse(res.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
def post(path, payload)
|
||||||
|
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||||
JSON.parse(res.body)
|
JSON.parse(res.body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,24 +1,20 @@
|
|||||||
class Lndhub
|
class Lndhub < ApplicationService
|
||||||
attr_accessor :auth_token
|
attr_accessor :auth_token
|
||||||
|
|
||||||
def initialize
|
def post(path, payload)
|
||||||
@base_url = ENV["LNDHUB_API_URL"]
|
|
||||||
end
|
|
||||||
|
|
||||||
def post(endpoint, payload)
|
|
||||||
headers = { "Content-Type" => "application/json" }
|
headers = { "Content-Type" => "application/json" }
|
||||||
if auth_token
|
if auth_token
|
||||||
headers.merge!({ "Authorization" => "Bearer #{auth_token}" })
|
headers.merge!({ "Authorization" => "Bearer #{auth_token}" })
|
||||||
end
|
end
|
||||||
|
|
||||||
res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers
|
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||||
log_error(res) if res.status != 200
|
log_error(res) if res.status != 200
|
||||||
|
|
||||||
JSON.parse(res.body)
|
JSON.parse(res.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(endpoint, auth_token)
|
def get(path, auth_token)
|
||||||
res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
|
res = Faraday.get(endpoint_url(path), {}, {
|
||||||
"Content-Type" => "application/json",
|
"Content-Type" => "application/json",
|
||||||
"Accept" => "application/json",
|
"Accept" => "application/json",
|
||||||
"Authorization" => "Bearer #{auth_token}"
|
"Authorization" => "Bearer #{auth_token}"
|
||||||
@ -42,7 +38,7 @@ class Lndhub
|
|||||||
self.auth_token
|
self.auth_token
|
||||||
end
|
end
|
||||||
|
|
||||||
def balance(user_token=nil)
|
def fetch_balance(user_token=nil)
|
||||||
get "balance", user_token || auth_token
|
get "balance", user_token || auth_token
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -72,4 +68,14 @@ class Lndhub
|
|||||||
Sentry.capture_message("Lndhub API request failed: #{res.body}")
|
Sentry.capture_message("Lndhub API request failed: #{res.body}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def base_url
|
||||||
|
@base_url ||= Setting.lndhub_api_url
|
||||||
|
end
|
||||||
|
|
||||||
|
def endpoint_url(path)
|
||||||
|
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
class LndhubV2 < Lndhub
|
class LndhubV2 < Lndhub
|
||||||
|
|
||||||
def post(endpoint, payload, options={})
|
def post(path, payload, options={})
|
||||||
headers = { "Content-Type" => "application/json" }
|
headers = { "Content-Type" => "application/json" }
|
||||||
if auth_token
|
if auth_token
|
||||||
headers.merge!({ "Authorization" => "Bearer #{auth_token}" })
|
headers.merge!({ "Authorization" => "Bearer #{auth_token}" })
|
||||||
elsif options[:admin_token]
|
elsif options[:admin_token]
|
||||||
headers.merge!({ "Authorization" => "Bearer #{options[:admin_token]}" })
|
headers.merge!({ "Authorization" => "Bearer #{options[:admin_token]}" })
|
||||||
end
|
end
|
||||||
res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers
|
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||||
log_error(res) if res.status != 200
|
log_error(res) if res.status != 200
|
||||||
|
|
||||||
JSON.parse(res.body)
|
JSON.parse(res.body)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user