-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added OAuth support. #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
require 'logger' | ||
require 'hubspot/connection' | ||
|
||
module Hubspot | ||
class Config | ||
|
||
CONFIG_KEYS = [:hapikey, :base_url, :portal_id, :logger] | ||
CONFIG_KEYS = [ | ||
:hapikey, :base_url, :portal_id, :logger, :access_token, :client_id, | ||
:client_secret, :redirect_uri | ||
] | ||
DEFAULT_LOGGER = Logger.new('/dev/null') | ||
|
||
class << self | ||
|
@@ -14,7 +18,18 @@ def configure(config) | |
@hapikey = config["hapikey"] | ||
@base_url = config["base_url"] || "https://api.hubapi.com" | ||
@portal_id = config["portal_id"] | ||
@logger = config['logger'] || DEFAULT_LOGGER | ||
@logger = config["logger"] || DEFAULT_LOGGER | ||
@access_token = config["access_token"] | ||
@client_id = config["client_id"] if config["client_id"].present? | ||
@client_secret = config["client_secret"] if config["client_secret"].present? | ||
@redirect_uri = config["redirect_uri"] if config["redirect_uri"].present? | ||
|
||
unless access_token.present? ^ hapikey.present? | ||
Hubspot::ConfigurationError.new("You must provide either an access_token or an hapikey") | ||
end | ||
if access_token.present? | ||
Hubspot::Connection.headers("Authorization" => "Bearer #{access_token}") | ||
end | ||
self | ||
end | ||
|
||
|
@@ -23,6 +38,8 @@ def reset! | |
@base_url = "https://api.hubapi.com" | ||
@portal_id = nil | ||
@logger = DEFAULT_LOGGER | ||
@access_token = nil | ||
Hubspot::Connection.headers({}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also reset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbisnett I would say no, because those things ( |
||
end | ||
|
||
def ensure!(*params) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'httparty' | ||
module Hubspot | ||
class OAuth < Connection | ||
include HTTParty | ||
DEFAULT_OAUTH_HEADERS = {"Content-Type" => "application/x-www-form-urlencoded;charset=utf-8"} | ||
class << self | ||
def refresh(params) | ||
params.stringify_keys! | ||
no_parse = params.delete("no_parse") { false } | ||
body = { | ||
client_id: params["client_id"] || Hubspot::Config.client_id, | ||
grant_type: "refresh_token", | ||
client_secret: params["client_secret"] || Hubspot::Config.client_secret, | ||
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri, | ||
refresh_token: params["refresh_token"] | ||
} | ||
response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS) | ||
log_request_and_response oauth_url, response, body | ||
raise(Hubspot::RequestError.new(response)) unless response.success? | ||
|
||
no_parse ? response : response.parsed_response | ||
end | ||
|
||
def create(params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the implementations of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbisnett they aren't quite the same. Notice the def make_oauth_call(body)
params.stringify_keys!
no_parse = params.delete("no_parse") { false }
response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS)
log_request_and_response oauth_url, response, body
raise(Hubspot::RequestError.new(response)) unless response.success?
no_parse ? response : response.parsed_response
end
def refresh(params)
body = {
client_id: params["client_id"] || Hubspot::Config.client_id,
grant_type: "refresh_token",
client_secret: params["client_secret"] || Hubspot::Config.client_secret,
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri,
refresh_token: params["refresh_token"]
}
make_oauth_call(body)
end
def create(params)
body = {
client_id: params["client_id"] || Hubspot::Config.client_id,
grant_type: "authorization_code",
client_secret: params["client_secret"] || Hubspot::Config.client_secret,
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri,
code: params["code"]
}
make_oauth_call(body)
end |
||
params.stringify_keys! | ||
no_parse = params.delete("no_parse") { false } | ||
body = { | ||
client_id: params["client_id"] || Hubspot::Config.client_id, | ||
grant_type: "authorization_code", | ||
client_secret: params["client_secret"] || Hubspot::Config.client_secret, | ||
redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri, | ||
code: params["code"] | ||
} | ||
response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS) | ||
log_request_and_response oauth_url, response, body | ||
raise(Hubspot::RequestError.new(response)) unless response.success? | ||
|
||
no_parse ? response : response.parsed_response | ||
end | ||
|
||
def oauth_url | ||
oauth_url = Hubspot::Config.base_url + "/oauth/v1/token" | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a binary operator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulodiniz yeah that's ruby's XOR operator. Meaning, "Exactly one of these must be present" (not both, and not none).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to #present? as it's a rails specific method? Also, I think the logical OR (||) is more appropriate. This should do it:
unless access_token || hapikey
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I jumped the gun on this a bit before looking through the code more, my bad. I didn't realize that activesupport was included. The bitwise comparison still smells funny though and at the least the docs and/or error message should be made a bit clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tmac yeah I'm happy to switch that to not use the XOR operator. But I think logically, the semantics of XOR is correct. If you have both
access_token
andhapikey
, then calls will not work as expected. You must have exactly one of them, but not both, and not neither. So I could switch it to maybe...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine in it's current version and is succinct without being obtuse. The
^
operator works on binary values and booleans are a form of binary value:Maybe update this to be
@access_token
and@hapikey