Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #61 from Privy/fix/threadsafety
Browse files Browse the repository at this point in the history
make api threadsafe
  • Loading branch information
csciuto authored Jul 31, 2017
2 parents 3d3cdde + 6a70b43 commit 3abe8f9
Show file tree
Hide file tree
Showing 24 changed files with 1,440 additions and 1,434 deletions.
208 changes: 111 additions & 97 deletions lib/constantcontact/api.rb

Large diffs are not rendered by default.

42 changes: 20 additions & 22 deletions lib/constantcontact/services/account_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@
module ConstantContact
module Services
class AccountService < BaseService
class << self

# Get a summary of account information
# @return [AccountInfo]
def get_account_info()
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_info')
url = build_url(url)
response = RestClient.get(url, get_headers())
Components::AccountInfo.create(JSON.parse(response.body))
end
# Get a summary of account information
# @return [AccountInfo]
def get_account_info()
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_info')
url = build_url(url)
response = RestClient.get(url, get_headers())
Components::AccountInfo.create(JSON.parse(response.body))
end


# Get all verified email addresses associated with an account
# @param [Hash] params - hash of query parameters/values to append to the request
# @return [Array<VerifiedEmailAddress>]
def get_verified_email_addresses(params)
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_verified_addresses')
url = build_url(url, params)
response = RestClient.get(url, get_headers())
email_addresses = []
JSON.parse(response.body).each do |email_address|
email_addresses << Components::VerifiedEmailAddress.create(email_address)
end
email_addresses
# Get all verified email addresses associated with an account
# @param [Hash] params - hash of query parameters/values to append to the request
# @return [Array<VerifiedEmailAddress>]
def get_verified_email_addresses(params)
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_verified_addresses')
url = build_url(url, params)
response = RestClient.get(url, get_headers())
email_addresses = []
JSON.parse(response.body).each do |email_address|
email_addresses << Components::VerifiedEmailAddress.create(email_address)
end

email_addresses
end

end
end
end
198 changes: 98 additions & 100 deletions lib/constantcontact/services/activity_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,130 +7,128 @@
module ConstantContact
module Services
class ActivityService < BaseService
class << self

# Get a set of activities
# @param [Hash] params - query parameters to be appended to the url
# @return [Array<Activity>]
def get_activities(params = {})
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.activities')
url = build_url(url, params)
response = RestClient.get(url, get_headers())

activities = []
JSON.parse(response.body).each do |activity|
activities << Components::Activity.create(activity)
end

activities

# Get a set of activities
# @param [Hash] params - query parameters to be appended to the url
# @return [Array<Activity>]
def get_activities(params = {})
url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.activities')
url = build_url(url, params)
response = RestClient.get(url, get_headers())

activities = []
JSON.parse(response.body).each do |activity|
activities << Components::Activity.create(activity)
end

activities
end

# Get an array of activities
# @param [String] activity_id - Activity id
# @return [Activity]
def get_activity(activity_id)
url = Util::Config.get('endpoints.base_url') +
sprintf(Util::Config.get('endpoints.activity'), activity_id)
url = build_url(url)
response = RestClient.get(url, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Get an array of activities
# @param [String] activity_id - Activity id
# @return [Activity]
def get_activity(activity_id)
url = Util::Config.get('endpoints.base_url') +
sprintf(Util::Config.get('endpoints.activity'), activity_id)
url = build_url(url)
response = RestClient.get(url, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Add Contacts Activity
# @param [AddContacts] add_contacts
# @return [Activity]
def create_add_contacts_activity(add_contacts)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.add_contacts_activity')
url = build_url(url)
payload = add_contacts.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Add Contacts Activity
# @param [AddContacts] add_contacts
# @return [Activity]
def create_add_contacts_activity(add_contacts)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.add_contacts_activity')
url = build_url(url)
payload = add_contacts.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Add Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
# @param [String] file_name - The name of the file (ie: contacts.csv)
# @param [String] contents - The content of the file
# @param [String] lists - Comma separated list of ContactList id's to add the contacts to
# @return [Activity]
def create_add_contacts_activity_from_file(file_name, contents, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.add_contacts_activity')
url = build_url(url)

payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }
# Create an Add Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
# @param [String] file_name - The name of the file (ie: contacts.csv)
# @param [String] contents - The content of the file
# @param [String] lists - Comma separated list of ContactList id's to add the contacts to
# @return [Activity]
def create_add_contacts_activity_from_file(file_name, contents, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.add_contacts_activity')
url = build_url(url)

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end
payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create a Clear Lists Activity
# @param [Array<lists>] lists - array of list id's to be cleared
# @return [Activity]
def add_clear_lists_activity(lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.clear_lists_activity')
url = build_url(url)
payload = {'lists' => lists}.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create a Clear Lists Activity
# @param [Array<lists>] lists - array of list id's to be cleared
# @return [Activity]
def add_clear_lists_activity(lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.clear_lists_activity')
url = build_url(url)
payload = {'lists' => lists}.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Export Contacts Activity
# @param [ExportContacts] export_contacts
# @return [Activity]
def add_export_contacts_activity(export_contacts)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.export_contacts_activity')
url = build_url(url)
payload = export_contacts.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Export Contacts Activity
# @param [ExportContacts] export_contacts
# @return [Activity]
def add_export_contacts_activity(export_contacts)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.export_contacts_activity')
url = build_url(url)
payload = export_contacts.to_json
response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create a Remove Contacts From Lists Activity
# @param [<Array>EmailAddress] email_addresses
# @param [<Array>RemoveFromLists] lists
# @return [Activity]
def add_remove_contacts_from_lists_activity(email_addresses, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.remove_from_lists_activity')
url = build_url(url)

payload = { 'import_data' => [], 'lists' => lists }
email_addresses.each do |email_address|
payload['import_data'] << { 'email_addresses' => [email_address] }
end
payload = payload.to_json
# Create a Remove Contacts From Lists Activity
# @param [<Array>EmailAddress] email_addresses
# @param [<Array>RemoveFromLists] lists
# @return [Activity]
def add_remove_contacts_from_lists_activity(email_addresses, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.remove_from_lists_activity')
url = build_url(url)

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
payload = { 'import_data' => [], 'lists' => lists }
email_addresses.each do |email_address|
payload['import_data'] << { 'email_addresses' => [email_address] }
end
payload = payload.to_json

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

# Create an Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
# @param [String] file_name - The name of the file (ie: contacts.csv)
# @param [String] contents - The content of the file
# @param [String] lists - Comma separated list of ContactList id' to add the contacts too
# @return [Activity]
def add_remove_contacts_from_lists_activity_from_file(file_name, contents, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.remove_from_lists_activity')
url = build_url(url)

payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }
# Create an Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
# @param [String] file_name - The name of the file (ie: contacts.csv)
# @param [String] contents - The content of the file
# @param [String] lists - Comma separated list of ContactList id' to add the contacts too
# @return [Activity]
def add_remove_contacts_from_lists_activity_from_file(file_name, contents, lists)
url = Util::Config.get('endpoints.base_url') +
Util::Config.get('endpoints.remove_from_lists_activity')
url = build_url(url)

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end
payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }

response = RestClient.post(url, payload, get_headers())
Components::Activity.create(JSON.parse(response.body))
end

end
end
end
90 changes: 46 additions & 44 deletions lib/constantcontact/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,65 @@
module ConstantContact
module Services
class BaseService
class << self
attr_accessor :api_key, :access_token
attr_accessor :api_client

def initialize(api_client = nil)
@api_client = api_client
end

protected
protected

# Return required headers for making an http request with Constant Contact
# @param [String] content_type - The MIME type of the body of the request, default is 'application/json'
# @return [Hash] - authorization headers
def get_headers(content_type = 'application/json')
{
:content_type => content_type,
:accept => 'application/json',
:authorization => "Bearer #{BaseService.access_token}",
:user_agent => "AppConnect Ruby SDK v#{ConstantContact::SDK::VERSION} (#{RUBY_DESCRIPTION})",
:x_ctct_request_source => "sdk.ruby.#{ConstantContact::SDK::VERSION}"
}
end
# Return required headers for making an http request with Constant Contact
# @param [String] content_type - The MIME type of the body of the request, default is 'application/json'
# @return [Hash] - authorization headers
def get_headers(content_type = 'application/json')
{
:content_type => content_type,
:accept => 'application/json',
:authorization => "Bearer #{api_client.access_token}",
:user_agent => "AppConnect Ruby SDK v#{ConstantContact::SDK::VERSION} (#{RUBY_DESCRIPTION})",
:x_ctct_request_source => "sdk.ruby.#{ConstantContact::SDK::VERSION}"
}
end


# returns the id of a ConstantContact component
def get_id_for(obj)
if obj.kind_of? ConstantContact::Components::Component
obj.id
elsif obj.kind_of? Hash
obj["id"]
else
obj
end
# returns the id of a ConstantContact component
def get_id_for(obj)
if obj.kind_of? ConstantContact::Components::Component
obj.id
elsif obj.kind_of? Hash
obj["id"]
else
obj
end
end


# Build a url from the base url and query parameters hash. Query parameters
# should not be URL encoded because this method will handle that
# @param [String] url - The base url
# @param [Hash] params - A hash with query parameters
# @return [String] - the url with query parameters hash
def build_url(url, params = nil)
if params.respond_to? :each
params.each do |key, value|
# Convert dates to CC date format
if value.respond_to? :iso8601
params[key] = value.iso8601
end
# Build a url from the base url and query parameters hash. Query parameters
# should not be URL encoded because this method will handle that
# @param [String] url - The base url
# @param [Hash] params - A hash with query parameters
# @return [String] - the url with query parameters hash
def build_url(url, params = nil)
if params.respond_to? :each
params.each do |key, value|
# Convert dates to CC date format
if value.respond_to? :iso8601
params[key] = value.iso8601
end

if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
params[key] = $1
end
if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
params[key] = $1
end
else
params ||= {}
end

params['api_key'] = BaseService.api_key
url += '?' + Util::Helpers.http_build_query(params)
else
params ||= {}
end

params['api_key'] = api_client.api_key
url += '?' + Util::Helpers.http_build_query(params)
end

end
end
end
Loading

0 comments on commit 3abe8f9

Please sign in to comment.