Sha256: 00149961b503030fcc9053a2afd0418a99d39c21d0f63392f2ecd15321f6ac4d

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require "net/http"
require "uri"
require "json"

class Connect_Api_Host
	API_HOST = "connect.gettyimages.com"
	# API_HOST = "gibson.candidate-gettyimages.com/Public"
	API_BASE_URL = "https://#{API_HOST}"
end

class HttpHelper

	def initialize(api_key, access_token)
		@api_key = api_key
		@access_token = access_token
	end	

	public
	def get_uri(path)
		return URI.parse "#{Connect_Api_Host::API_BASE_URL}#{path}"
	end

	public
	def get(endpoint_path, query_params)
		
		uri = get_uri(endpoint_path)
		#puts uri		
		if !query_params.nil?
			uri.query = URI.encode_www_form query_params
		end
		#puts "REQUEST URI: #{uri.request_uri}" 
		req = Net::HTTP::Get.new uri.request_uri
		return send uri, req, @api_key, @access_token

	end

	public
	def post(endpoint_path)

		uri = get_uri endpoint_path	
		req = Net::HTTP::Post.new uri.request_uri
		return send uri, req, @api_key, @access_token

	end

	def send(connect_uri, connect_request, api_key, bearer_token = "")

		# define HTTPS connection
		https = Net::HTTP.new(connect_uri.host, connect_uri.port)
		https.use_ssl = true
		https.verify_mode = OpenSSL::SSL::VERIFY_NONE

		# define headers
		connect_request["Api-Key"] = api_key
		connect_request["Authorization"] = "Bearer #{bearer_token}" unless bearer_token.empty?

		# send request		
		resp = https.request connect_request
		# puts "HTTP RESPONSE: #{resp}" 

		if !resp.is_a?(Net::HTTPSuccess)			
			raise "HTTP RESPONSE: #{resp}" 
		end

		return JSON.parse(resp.body)

	end 

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ConnectSDK-1.0.1.Beta lib/HttpHelper.rb