Class: Hubspot::ApiClient

Inherits:
Object show all
Includes:
HTTParty
Defined in:
lib/hubspot/api_client.rb

Overview

All interations with the Hubspot API happen here…

Direct Known Subclasses

Batch, PagedBatch, PagedCollection, Resource

Constant Summary collapse

MAX_RETRIES =
3
RETRY_WAIT_TIME =

seconds

1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(url, options = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/hubspot/api_client.rb', line 53

def delete(url, options = {})
  ensure_configuration!
  start_time = Time.now
  response = super(url, options)
  log_request(:delete, url, response, start_time)
  response
end

.get(url, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hubspot/api_client.rb', line 22

def get(url, options = {})
  ensure_configuration!

  if options[:query] && options[:query][:properties].is_a?(Array)
    options[:query][:properties] = options[:query][:properties].join(',')
  end

  start_time = Time.now
  response = super(url, options)

  request = HTTParty::Request.new(Net::HTTP::Get, url, options)
  log_request(:get, request.uri.to_s, response, start_time)
  response
end

.handle_response(response, retries = 0) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/hubspot/api_client.rb', line 74

def handle_response(response, retries = 0)
  case response.code
  when 200..299
    response.parsed_response
  when 429
    handle_rate_limit(response, retries)
  else
    log_and_raise_error(response)
  end
end

.log_request(http_method, url, response, start_time, extra = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hubspot/api_client.rb', line 61

def log_request(http_method, url, response, start_time, extra = nil)
  d = Time.now - start_time

  Hubspot.logger.info(
    "#{http_method.to_s.upcase} #{url} took #{d.round(2)}s with status #{response.code}"
  )

  return unless Hubspot.logger.debug?

  Hubspot.logger.debug("Request body: #{extra[:body]}") if extra
  Hubspot.logger.debug("Response body: #{response.body}")
end

.patch(url, options = {}) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/hubspot/api_client.rb', line 45

def patch(url, options = {})
  ensure_configuration!
  start_time = Time.now
  response = super(url, options)
  log_request(:patch, url, response, start_time, options)
  response
end

.post(url, options = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/hubspot/api_client.rb', line 37

def post(url, options = {})
  ensure_configuration!
  start_time = Time.now
  response = super(url, options)
  log_request(:post, url, response, start_time, options)
  response
end

Instance Method Details

#handle_response(response) ⇒ Object



17
18
19
# File 'lib/hubspot/api_client.rb', line 17

def handle_response(response)
  self.class.handle_response(response)
end