Created
April 16, 2019 15:41
-
-
Save jclusso/cda0df8bd127d716cf1bb859cbf7d008 to your computer and use it in GitHub Desktop.
Basic HubSpot API class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HubSpotAPI | |
def self.oauth | |
OAuth2::Client.new( | |
ENVHelper.get('hubspot_client_id'), | |
ENVHelper.get('hubspot_client_secret'), | |
site: 'https://app.hubspot.com/oauth/authorize', | |
token_url: 'https://api.hubapi.com/oauth/v1/token' | |
) | |
end | |
def initialize(access_token) | |
@access_token = access_token | |
@conn = Faraday.new('https://api.hubapi.com/') do |f| | |
f.headers['Authorization'] = "Bearer #{@access_token}" | |
f.headers['Content-Type'] = 'application/json' | |
f.request :url_encoded | |
f.response :json, content_type: /\bjson/ | |
f.adapter Faraday.default_adapter | |
end | |
end | |
def access_token_info | |
api_request { @conn.get("oauth/v1/access-tokens/#{@access_token}").body } | |
end | |
def contact_lists(count: 250, offset: nil) | |
api_request do | |
@conn.get('contacts/v1/lists') do |req| | |
req.params = { count: count, offset: offset } | |
end.body | |
end | |
end | |
def contacts_by_list(list_id, count: 100, offset: nil) | |
api_request do | |
@conn.get("contacts/v1/lists/#{list_id}/contacts/all") do |req| | |
req.params = { | |
count: count, | |
vidOffset: offset, | |
property: 'email', | |
propertyMode: 'value_only' | |
} | |
end.body | |
end | |
end | |
def create_contact_list(name) | |
api_request do | |
@conn.post('contacts/v1/lists') do |req| | |
req.body = { 'name' => name }.to_json | |
end.body | |
end | |
end | |
def add_contacts_to_list(contacts, list_id) | |
raise TooManyContactsError if contacts.size > 500 | |
api_request do | |
@conn.post("contacts/v1/lists/#{list_id}/add") do |req| | |
req.body = contacts.to_json | |
end.body | |
end | |
end | |
private | |
def api_request | |
tries ||= 3 | |
response = yield | |
return response unless response['status'] == 'error' | |
if response['message'].include?('expired') | |
raise ExpiredOAuthTokenError, response['message'] | |
elsif response['message'].include?('proper permissions!') | |
raise InvalidPermissionError, response['message'] | |
else | |
raise Error, response['message'] | |
end | |
rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::SSLError, | |
EOFError, Errno::ECONNRESET, Errno::EINVAL, Errno::ENETUNREACH, | |
Errno::ETIMEDOUT, Timeout::Error, SocketError, Errno::ECONNREFUSED, | |
Faraday::ParsingError => e | |
retry unless (tries -= 1).zero? | |
raise e | |
end | |
class Error < StandardError; end | |
class ExpiredOAuthTokenError < Error; end | |
class TooManyContactsError < Error; end | |
class InvalidPermissionError < Error; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment