Skip to content
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

Accepts Headers #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
72 changes: 42 additions & 30 deletions lib/soapforce/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Client
def initialize(options = {})
@describe_cache = {}
@describe_layout_cache = {}
@headers = {}
@headers = {}

@wsdl = options[:wsdl] || File.dirname(__FILE__) + '/../../resources/partner.wsdl.xml'

Expand All @@ -19,6 +19,9 @@ def initialize(options = {})
# to make SOAP calls in Professional/Group Edition organizations.

client_id = options[:client_id] || Soapforce.configuration.client_id

@headers = options[:headers] if options[:headers]

@headers = { 'tns:CallOptions' => { 'tns:client' => client_id } } if client_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This combination of client_id would not work with optional headers. What if you merge the hashes? @headers.merge(options[:headers]) if options[:headers].is_a?(Hash)


@version = options[:version] || Soapforce.configuration.version || 28.0
Expand Down Expand Up @@ -204,21 +207,21 @@ def describe_layout(sobject_type, layout_id=nil)
response
end

def query(soql)
call_soap_api(:query, {:queryString => soql})
def query(soql, header={})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing header into every method I think you can use @headers in call_soap_api and it would apply to all calls. Do you ever find yourself needing different headers per request?

call_soap_api(:query, {:queryString => soql}, header)
end

# Includes deleted (isDeleted) or archived (isArchived) records
def query_all(soql)
call_soap_api(:query_all, {:queryString => soql})
def query_all(soql, header={})
call_soap_api(:query_all, {:queryString => soql}, header)
end

def query_more(locator)
call_soap_api(:query_more, {:queryLocator => locator})
def query_more(locator, header={})
call_soap_api(:query_more, {:queryLocator => locator}, header)
end

def search(sosl)
call_soap_api(:search, {:searchString => sosl})
def search(sosl, header={})
call_soap_api(:search, {:searchString => sosl}, header)
end

# Public: Insert a new record.
Expand All @@ -234,8 +237,8 @@ def search(sosl)
#
# Returns the String Id of the newly created sobject.
# Returns false if something bad happens.
def create(sobject_type, properties)
create!(sobject_type, properties)
def create(sobject_type, properties, header={})
create!(sobject_type, properties, header)
rescue
false
end
Expand All @@ -253,8 +256,8 @@ def create(sobject_type, properties)
#
# Returns the String Id of the newly created sobject.
# Raises exceptions if an error is returned from Salesforce.
def create!(sobject_type, properties)
call_soap_api(:create, sobjects_hash(sobject_type, properties))
def create!(sobject_type, properties, header={})
call_soap_api(:create, sobjects_hash(sobject_type, properties), header)
end

# Public: Update a record.
Expand All @@ -269,8 +272,8 @@ def create!(sobject_type, properties)
#
# Returns Hash if the sobject was successfully updated.
# Returns false if there was an error.
def update(sobject_type, properties)
update!(sobject_type, properties)
def update(sobject_type, properties, header={})
update!(sobject_type, properties, header)
rescue
false
end
Expand All @@ -287,8 +290,8 @@ def update(sobject_type, properties)
#
# Returns Hash if the sobject was successfully updated.
# Raises an exception if an error is returned from Salesforce
def update!(sobject_type, properties)
call_soap_api(:update, sobjects_hash(sobject_type, properties))
def update!(sobject_type, properties, header={})
call_soap_api(:update, sobjects_hash(sobject_type, properties), header)
end

# Public: Update or create a record based on an external ID
Expand All @@ -304,8 +307,8 @@ def update!(sobject_type, properties)
#
# Returns Hash if the record was found and updated or newly created.
# Raises an exception if an error is returned from Salesforce.
def upsert(sobject_type, external_id_field_name, objects)
upsert!(sobject_type, external_id_field_name, objects)
def upsert(sobject_type, external_id_field_name, objects, header={})
upsert!(sobject_type, external_id_field_name, objects, header)
rescue
false
end
Expand All @@ -323,9 +326,9 @@ def upsert(sobject_type, external_id_field_name, objects)
#
# Returns Hash if the record was found and updated or newly created.
# Raises an exception if an error is returned from Salesforce.
def upsert!(sobject_type, external_id_field_name, objects)
def upsert!(sobject_type, external_id_field_name, objects, header={})
message = {externalIDFieldName: external_id_field_name}.merge(sobjects_hash(sobject_type, objects))
call_soap_api(:upsert, message)
call_soap_api(:upsert, message, header)
end

# Public: Delete a record.
Expand All @@ -340,8 +343,8 @@ def upsert!(sobject_type, external_id_field_name, objects)
#
# Returns true if the sobject was successfully deleted.
# Returns false if an error is returned from Salesforce.
def delete(id)
delete!(id)
def delete(id, header = {})
delete!(id, header)
rescue
false
end
Expand All @@ -359,9 +362,9 @@ def delete(id)
#
# Returns Hash if the sobject was successfully deleted.
# Raises an exception if an error is returned from Salesforce.
def delete!(id)
def delete!(id, header={})
ids = id.is_a?(Array) ? id : [id]
call_soap_api(:delete, {:ids => ids})
call_soap_api(:delete, {:ids => ids}, header)
end
alias_method :destroy!, :delete

Expand All @@ -376,9 +379,9 @@ def delete!(id)
# convertedStatus: 'Closed - Converted')
#
# Returns Soapforce::Result
def convert_lead(attributes)
def convert_lead(attributes, header={})
leads = attributes.is_a?(Array) ? attributes : [attributes]
call_soap_api(:convert_lead, leadConverts: leads)
call_soap_api(:convert_lead, {leadConverts: leads}, header)
end

# Public: Merges records together
Expand All @@ -402,7 +405,6 @@ def merge!(sobject_type, master_record_hash, ids)
}
)
end

# Public: Merges records together
#
# sobject - String name of the sobject
Expand Down Expand Up @@ -590,10 +592,20 @@ def key_name(key)
end
end

def call_soap_api(method, message_hash={})

def header_hash(header)
xml_header = header.map do |k,v|
header_fields = v.map {|k,v| ["tns:#{k}",v]}.to_h
header_name = "tns:#{k}"
[header_name, header_fields]
end
xml_header.to_h
end

def call_soap_api(method, message_hash={}, header={})

response = @client.call(method.to_sym) do |locals|
locals.message message_hash
locals.soap_header header_hash(header) unless header.empty?
end

# Convert SOAP XML to Hash
Expand Down
Loading