lib/urbanairship/client.rb in urbanairship-5.8.0 vs lib/urbanairship/client.rb in urbanairship-5.9.0

- old
+ new

@@ -11,26 +11,29 @@ # Initialize the Client # # @param [Object] key Application Key # @param [Object] secret Application Secret + # @param [String] token Application Auth Token (for custom events endpoint) # @return [Object] Client - def initialize(key: required('key'), secret: required('secret')) + def initialize(key: required('key'), secret: required('secret'), token: nil) @key = key @secret = secret + @token = token end # Send a request to Airship's API # # @param [Object] method HTTP Method # @param [Object] body Request Body # @param [Object] url Request URL # @param [Object] content_type Content-Type - # @param [Object] version API Version + # @param [Object] encoding Encoding + # @param [Symbol] auth_type (:basic|:bearer) # @return [Object] Push Response def send_request(method: required('method'), url: required('url'), body: nil, - content_type: nil, encoding: nil) + content_type: nil, encoding: nil, auth_type: :basic) req_type = case method when 'GET' :get when 'POST' :post @@ -44,28 +47,38 @@ headers = {'User-agent' => 'UARubyLib/' + Urbanairship::VERSION} headers['Accept'] = 'application/vnd.urbanairship+json; version=3' headers['Content-type'] = content_type unless content_type.nil? headers['Content-Encoding'] = encoding unless encoding.nil? + + if auth_type == :bearer + raise ArgumentError.new('token must be provided as argument if auth_type=bearer') if @token.nil? + headers['X-UA-Appkey'] = @key + headers['Authorization'] = "Bearer #{@token}" + end - debug = "Making #{method} request to #{url}.\n"+ - "\tHeaders:\n" + debug = "Making #{method} request to #{url}.\n"+ "\tHeaders:\n" debug += "\t\tcontent-type: #{content_type}\n" unless content_type.nil? debug += "\t\tcontent-encoding: gzip\n" unless encoding.nil? debug += "\t\taccept: application/vnd.urbanairship+json; version=3\n" debug += "\tBody:\n#{body}" unless body.nil? logger.debug(debug) - response = RestClient::Request.execute( + params = { method: method, url: url, headers: headers, - user: @key, - password: @secret, payload: body, timeout: Urbanairship.configuration.timeout - ) + } + + if auth_type == :basic + params[:user] = @key + params[:password] = @secret + end + + response = RestClient::Request.execute(params) logger.debug("Received #{response.code} response. Headers:\n\t#{response.headers}\nBody:\n\t#{response.body}") Response.check_code(response.code, response) self.class.build_response(response)