diff --git a/lib/tapfall/api.rb b/lib/tapfall/api.rb index 6aa4951..ab01394 100644 --- a/lib/tapfall/api.rb +++ b/lib/tapfall/api.rb @@ -4,8 +4,9 @@ require 'uri' module Tapfall class API - def initialize(server) + def initialize(server, options = {}) @root_url = build_root_url(server) + @options = options end def add_repo(did) @@ -55,6 +56,10 @@ module Tapfall request.body = JSON.generate(json_data) request.content_type = "application/json" + if @options[:admin_password] + request.basic_auth('admin', @options[:admin_password]) + end + response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) do |http| http.request(request) end diff --git a/lib/tapfall/stream.rb b/lib/tapfall/stream.rb index 3b3d1c6..ef323b0 100644 --- a/lib/tapfall/stream.rb +++ b/lib/tapfall/stream.rb @@ -18,8 +18,9 @@ module Tapfall @options = options @root_url = ensure_empty_path(@root_url) @ack = true unless options[:ack] == false + @password = options[:admin_password] - @api = API.new(build_api_url) + @api = build_api end def connect @@ -48,6 +49,18 @@ module Tapfall private + def basic_auth(account, password) + 'Basic ' + ["#{account}:#{password}"].pack('m0') + end + + def request_headers + if @password + { 'Authorization' => basic_auth('admin', @password) } + else + {} + end + end + def build_websocket_url @root_url + "/channel" end @@ -59,5 +72,10 @@ module Tapfall @root_url.gsub(/^wss:/, 'https:') end end + + def build_api + api_url = build_api_url + API.new(api_url, { admin_password: @password }) + end end end