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

Add request headers and fix xsi object type #31

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
vendor
*.swp
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ client = Soapforce::Client.new
client.authenticate(username: 'foo', password: 'password_and_security_token')
```

#### Add headers to your request

```
# For any request you make, you can add your headers before hand like so:
client.request_headers = { AllOrNoneHeader: { allOrNone: 'true' } }
client.update('Account', Id: '006A000000Lbiiz', Name: 'Whizbang Corp')
```

#### Session authentication

```ruby
Expand Down Expand Up @@ -113,6 +121,13 @@ client.update('Account', Id: '006A000000Lbiiz', Name: 'Whizbang Corp')
# => {id: '006A000000Lbiiz', success: true}
```

```ruby
# Update the Account with Id '006A000000Lbiiz' using <AllOrNoneHeader>
client.request_headers = { AllOrNoneHeader: { allOrNone: 'true' } }
client.update('Account', {Id: '006A000000Lbiiz', Name: 'Whizbang Corp'})
# => {id: '006A000000Lbiiz', success: true}
```

### upsert

```ruby
Expand Down
33 changes: 25 additions & 8 deletions lib/soapforce/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ class Client
attr_reader :client
attr_reader :headers
attr_reader :tag_style
attr_accessor :logger
attr_reader :message_hash
attr_accessor :logger, :request_headers

# The partner.wsdl is used by default but can be changed by passing in a new :wsdl option.
# A client_id can be
def initialize(options = {})
@describe_cache = {}
@describe_layout_cache = {}
@headers = {}
@message_hash = {}
@request_headers = {}

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

Expand All @@ -19,6 +22,7 @@ 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

@version = options[:version] || Soapforce.configuration.version || 28.0
Expand Down Expand Up @@ -254,7 +258,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))
sobjects_hash(sobject_type, properties)
call_soap_api(:create)
end

# Public: Update a record.
Expand Down Expand Up @@ -288,7 +293,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))
sobjects_hash(sobject_type, properties)
call_soap_api(:update)
end

# Public: Update or create a record based on an external ID
Expand Down Expand Up @@ -590,10 +596,12 @@ def key_name(key)
end
end

def call_soap_api(method, message_hash={})
def call_soap_api(method, message={})
@message_hash = message unless message.empty?

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

# Convert SOAP XML to Hash
Expand Down Expand Up @@ -625,6 +633,15 @@ def call_soap_api(method, message_hash={})
result
end

def headers_hash(headers)
header = headers.map do |k,v|
name = "tns:#{k}"
fields = v.map {|k,v| ["tns:#{k}",v]}.to_h
[name, fields]
end
header.to_h
end

def sobjects_hash(sobject_type, sobject_hash)

if sobject_hash.is_a?(Array)
Expand All @@ -634,10 +651,10 @@ def sobjects_hash(sobject_type, sobject_hash)
end

sobjects.map! do |obj|
{"ins0:type" => sobject_type}.merge(obj)
# {"ins0:type" => sobject_type}.merge(obj)
{"@xsi:type" => sobject_type}.merge(obj)
end

{sObjects: sobjects}
@message_hash = {sObjects: sobjects}
end
end
end