Sha256: 8df6323527e6c9554e3cc783b2c347e0a05dbde2ee87221ed99d549e28bcfe30

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Qstash
  class HttpClient
    attr_reader :uri

    def initialize(uri)
      @uri = uri
    end

    def delete(body, headers = {})
      request = Net::HTTP::Delete.new(uri)
      request.body = body.to_json
      set_headers(request, headers)

      make_request(request)
    end

    def get(headers = {})
      request = Net::HTTP::Get.new(uri)
      set_headers(request, headers)

      make_request(request)
    end

    def post(body, headers = {})
      request = Net::HTTP::Post.new(uri)
      request.body = body.to_json
      set_headers(request, headers)

      make_request(request)
    end

    private

    def make_request(request)
      request["Authorization"] = "Bearer #{Qstash.config.token}"
      request["User-Agent"] = "qstash-rb/#{Qstash::VERSION}"

      Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
        http.request(request).then do |response|
          Response.new(response)
        end
      end
    end

    def set_headers(request, headers)
      headers.each do |key, value|
        request[Qstash::Headers.qstash_header_key(key)] = value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qstash-rb-0.1.3 lib/qstash/http_client.rb
qstash-rb-0.1.2 lib/qstash/http_client.rb