class HttpClient

Attributes

auth_token[RW]

Public Class Methods

new(client_id, client_secret) click to toggle source
# File lib/domain_com_au/http_client.rb, line 10
def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  @headers = {
    'content-type': 'application/json'
  }
  # @auth_action = true
  authorize
end

Public Instance Methods

get(uri, headers={}) click to toggle source
# File lib/domain_com_au/http_client.rb, line 29
def get(uri, headers={})
  authorize if @auth_token && is_token_expired?
  check_uri(uri)
  req = Net::HTTP::Get.new(uri)
  set_headers(req, headers)
  http_response(uri, req)
end
post(uri, params={}, headers={}) click to toggle source
# File lib/domain_com_au/http_client.rb, line 20
def post(uri, params={}, headers={})
  authorize if @auth_token && is_token_expired?
  check_uri(uri)
  req = Net::HTTP::Post.new(uri)
  set_headers(req, headers)
  set_body(req, params)
  http_response(uri, req)
end

Private Instance Methods

authorize() click to toggle source
# File lib/domain_com_au/http_client.rb, line 77
def authorize
  uri = URI("#{Constants::BASE_AUTH_URL}/connect/token")
  base_64_credentials = Base64.strict_encode64("#{@client_id}:#{@client_secret}")
  params = {
    grant_type: "client_credentials",
    scope: "api_listings_read api_agencies_read"
  }
  headers = {
    'authorization': "basic #{base_64_credentials}",
    'content-type': "application/x-www-form-urlencoded"
  }
  time_now = Time.now
  res = post(uri, params, headers)
  if res.body && res.code.to_i == 200
    body = JSON.parse(res.body)
    @auth_token = body['access_token']
    @auth_type = body['token_type']
    @auth_expiry = time_now + body['expires_in']
  else
    raise ArgumentError.new("Domain Auth API didn't return a token, body: #{res.body}, status: #{res.code}")
  end
end
check_uri(uri) click to toggle source
# File lib/domain_com_au/http_client.rb, line 43
def check_uri(uri)
  raise ArgumentError.new('Uri cannot be nil') unless uri
end
http_response(uri, req) click to toggle source
# File lib/domain_com_au/http_client.rb, line 63
def http_response(uri, req)
  return unless uri.hostname && uri.port
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request(req) }
  # if [401,403].include?(res.code.to_i) && @auth_action
  #   raise StandardError.new("Couldn't Authenticate, res.body: #{res.body}, res.code: #{res.code}")
  # elsif [401,403].include?(res.code.to_i)
  #   authorize && @auth_action = true
  #   set_headers(req, {})
  #   res = http_response(uri, req)
  # end
  # @auth_action = false
  res
end
is_token_expired?() click to toggle source
# File lib/domain_com_au/http_client.rb, line 39
def is_token_expired?
  @auth_expiry - 60 <= Time.now # add request buffer
end
set_body(req, params) click to toggle source
# File lib/domain_com_au/http_client.rb, line 53
def set_body(req, params)
  req.body = case req['content-type']
    when 'application/x-www-form-urlencoded' then
      URI.encode_www_form(params)
    when 'application/json' then
      params.to_json
    else nil
    end
end
set_headers(req, headers) click to toggle source
# File lib/domain_com_au/http_client.rb, line 47
def set_headers(req, headers)
  req['authorization'] = "#{@auth_type} #{@auth_token}" if @auth_token && @auth_type
  @headers.each { |key, value| req[key.to_s] = value }
  headers.each { |key, value| req[key.to_s] = value } if headers.class == Hash
end