Sha256: ec0b9cce7b7813a9d8a1f3c3e1df02af9a2768ca5e5a83335e60c9e90ba9c823

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require "json"
require "faraday"

module BrazeRuby
  class HTTP
    DEFAULT_TIMEOUT = 30
    DEFAULT_OPEN_TIMEOUT = 5

    def initialize(api_key, braze_url, options = {})
      @api_key = api_key
      @braze_url = braze_url
      @options = default_options.merge(options)
    end

    def post(path, payload)
      connection.post path do |request|
        request.body = JSON.dump(payload)
      end
    end

    def put(path, payload)
      connection.post path do |request|
        request.body = JSON.dump(payload)
      end
    end

    def get(path, query = {})
      connection.get path, query
    end

    def delete(path, payload = nil)
      connection.delete path do |request|
        request.body = JSON.dump(payload) if payload
      end
    end

    def connection
      @connection ||= Faraday.new(url: @braze_url) do |connection|
        connection.headers["Content-Type"] = "application/json"
        connection.headers["Accept"] = "application/json"
        connection.headers["User-Agent"] = "Braze Ruby gem v#{BrazeRuby::VERSION}"
        connection.headers["Authorization"] = "Bearer #{@api_key}"

        connection.response :logger if ENV["BRAZE_RUBY_DEBUG"]
        connection.request :retry, @options[:retry] if @options[:retry]

        connection.adapter Faraday.default_adapter

        connection.options[:timeout] = @options[:timeout]
        connection.options[:open_timeout] = @options[:open_timeout]
      end
    end

    private

    def default_options
      {timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
braze_ruby-0.10.0 lib/braze_ruby/http.rb