Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ A Ruby wrapper for the Infusionsoft API
2. Enable the API on your Infusionsoft account (if you haven't already) and generate your API Key: [See Infusionsoft Doc](https://classic-infusionsoft.knowledgeowl.com/help/api-key)
3. Then create an initializer in `config\initializers` called infusionsoft.rb and the following

## Service Account Key (SAK)

```ruby
# Added to your config\initializers file
Infusionsoft.configure do |config|
config.api_url = 'YOUR_INFUSIONSOFT_URL' # example infused.infusionsoft.com DO NOT INCLUDE https://
config.api_url = 'api.infusionsoft.com' # do not include https://
config.sak_key = 'YOUR_INFUSIONSOFT_SAK_KEY' # See: https://developer.infusionsoft.com/pat-and-sak/
config.api_logger = Logger.new("#{Rails.root}/log/infusionsoft_api.log") # optional logger file
end
```

## Legacy Key (will be deprecated)

```ruby
# Added to your config\initializers file
Infusionsoft.configure do |config|
config.api_url = 'api.infusionsoft.com' # do not include https://
config.api_key = 'YOUR_INFUSIONSOFT_API_KEY'
config.api_logger = Logger.new("#{Rails.root}/log/infusionsoft_api.log") # optional logger file
end
Expand Down
1 change: 1 addition & 0 deletions lib/infusionsoft/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Configuration
VALID_OPTION_KEYS = [
:api_url,
:api_key,
:sak_key,
:api_logger,
:use_oauth,
:user_agent # allows you to change the User-Agent of the request headers
Expand Down
24 changes: 19 additions & 5 deletions lib/infusionsoft/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ module Connection
private

def connection(service_call, *args)
path = use_oauth ? "/crm/xmlrpc/v1?access_token=#{api_key}" : "/api/xmlrpc"

client = XMLRPC::Client.new3({
'host' => api_url,
'path' => path,
'path' => '/crm/xmlrpc/v1',
'port' => 443,
'use_ssl' => true
})
client.http_header_extra = {'User-Agent' => user_agent}
client.http_header_extra = define_headers

begin
# Fill the api_key param and doesn't send real api_key if is present - Avoid error https://github.com/nateleavitt/infusionsoft/pull/85#issuecomment-2404189445
args.insert(0, '')
api_logger.info "CALL: #{service_call} api_url: #{api_url} at:#{Time.now} args:#{args.inspect}"
result = client.call("#{service_call}", api_key, *args)
result = client.call("#{service_call}", *args)
if result.nil?; ok_to_retry('nil response') end
rescue Timeout::Error => timeout
# Retry up to 5 times on a Timeout before raising it
Expand All @@ -44,5 +45,18 @@ def ok_to_retry(e)
end
end

def define_headers
headers = { 'User-Agent' => user_agent }

if use_oauth
headers['Authorization'] = "Bearer #{api_key}"
elsif sak_key
headers['Authorization'] = "Bearer #{sak_key}"
else
headers['X-Keap-API-Key'] = api_key
end

headers
end
end
end