Sha256: 292749aaefe81678b3ef03fbca9e19f64647fa108903492cd4bb83c8587524ca

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'net/http'
require 'json'
require 'uffizzi/config_file'

module Uffizzi
  class HttpClient
    class << self
      def make_request(request_uri, method, require_cookies, params = {})
        uri = URI(request_uri)
        use_ssl = request_uri.start_with?('https')

        response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
          request = build_request(uri, params, method, require_cookies)

          http.request(request)
        end

        if response.instance_of?(Net::HTTPUnauthorized)
          raise StandardError.new('Not authorized')
        end

        response
      end

      private

      def build_request(uri, params, method, require_cookies)
        headers = { 'Content-Type' => 'application/json' }
        request = case method
                  when :get
                    Net::HTTP::Get.new(uri.path, headers)
                  when :post
                    Net::HTTP::Post.new(uri.path, headers)
                  when :delete
                    Net::HTTP::Delete.new(uri.path, headers)
                  when :put
                    Net::HTTP::Put.new(uri.path, headers)
        end
        if request.instance_of?(Net::HTTP::Post)
          request.body = params.to_json
        end
        request['Cookie'] = ConfigFile.read_option(:cookie) if require_cookies
        if ConfigFile.exists? && ConfigFile.option_exists?(:basic_auth_user) && ConfigFile.option_exists?(:basic_auth_password)
          request.basic_auth(ConfigFile.read_option(:basic_auth_user), ConfigFile.read_option(:basic_auth_password))
        end
        request
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
uffizzi-cli-0.1.4.3 lib/uffizzi/clients/api/http_client.rb
uffizzi-cli-0.1.4.2 lib/uffizzi/clients/api/http_client.rb
uffizzi-cli-0.1.3 lib/uffizzi/clients/api/http_client.rb
uffizzi-cli-0.1.2 lib/uffizzi/clients/api/http_client.rb
uffizzi-cli-0.1.0 lib/uffizzi/clients/api/http_client.rb