58 lines
1.3 KiB
Ruby
58 lines
1.3 KiB
Ruby
class Lndhub
|
|
attr_accessor :auth_token
|
|
|
|
def initialize
|
|
@base_url = ENV["LNDHUB_API_URL"]
|
|
end
|
|
|
|
def post(endpoint, payload)
|
|
headers = { "Content-Type" => "application/json" }
|
|
if auth_token
|
|
headers.merge!({ "Authorization" => "Bearer #{auth_token}" })
|
|
end
|
|
|
|
res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers
|
|
|
|
if res.status != 200
|
|
Rails.logger.error "[lndhub] API request failed:"
|
|
Rails.logger.error res.body
|
|
#TODO add some kind of exception tracking/notifications
|
|
end
|
|
|
|
JSON.parse(res.body)
|
|
end
|
|
|
|
def get(endpoint, auth_token)
|
|
res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
|
|
"Content-Type" => "application/json",
|
|
"Accept" => "application/json",
|
|
"Authorization" => "Bearer #{auth_token}"
|
|
})
|
|
|
|
JSON.parse(res.body)
|
|
end
|
|
|
|
def create(payload)
|
|
post "create", payload
|
|
end
|
|
|
|
def authenticate(user)
|
|
credentials = post "auth?type=auth", { login: user.ln_login, password: user.ln_password }
|
|
self.auth_token = credentials["access_token"]
|
|
self.auth_token
|
|
end
|
|
|
|
def balance(user_token)
|
|
get "balance", user_token || auth_token
|
|
end
|
|
|
|
def addinvoice(payload)
|
|
invoice = post "addinvoice", {
|
|
amt: payload[:amount],
|
|
description_hash: payload[:description_hash]
|
|
}
|
|
|
|
invoice["payment_request"]
|
|
end
|
|
end
|