-
Notifications
You must be signed in to change notification settings - Fork 44
Using separate Restforce sessions using OAuth #71
Comments
One possibility would be to provide a setter at the class level of your SObject subclass. Then you could set using a Restforce client with any configuration you want: class Territory < ActiveForce::SObject
class << self
def sfdc_client=(client)
@client = client
end
end
end
> territory = Territory.new
=> #<Territory id: nil>
> territory.send(:sfdc_client)
=> #<Restforce::Data::Client @options={:api_version=>"26.0", :username=>nil, :password=>nil, :security_token=>nil, :client_id=>nil, :client_secret=>nil, :host=>"login.salesforce.com", :oauth_token=>nil, :refresh_token=>nil, :instance_url=>nil, :cache=>nil, :authentication_retries=>3, :compress=>nil, :timeout=>nil, :adapter=>:net_http, :proxy_uri=>nil, :authentication_callback=>nil}>
> Territory.sfdc_client = Object.new
=> #<Object:0x007fab432bf480>
> territory.send(:sfdc_client)
=> #<Object:0x007fab432bf480> @olvap What do you think about providing this out of the box? |
It is a very interesting issue, and is one of the advantages of restforce over active_force. Within restforce, every sobject have and associated client instance, so the knowledge is not at the class level, it is at the instance level. # in the restforce world:
client_1 = RestForce::Client.new
client_2 = RestForce::Client.new(other_credentials)
account = client_1.find('Account', 'n11gg1')
account.client #=> client_1
account.update!('Name' => 'New Name')
#=> will do client_1.update!('Account', 'Id' => 'n11gg1', 'Name' => 'New Name') We are currently storing the client at the class level, so we can provide finders and queries. # In the active_force world there are a lot of ways of getting sobjects.
account = Account.find('n11gg1')
account2 = Account.where(name: 'New Name') The problem with changing the client at the class level, is that it will not work with two user at the same time (as both will try to change the same class). There are two possible strategies I can think of:
client_1 = RestForce::Client.new custom_credentials
client_1.Account.where(name: 'New Name').first
#=> an instance of class.new(Account).sfdc_client = client_1
Account.client(client_1).find('n11gg1').first
#=> <Account @client=client_1> We would also need an option to require that being set (because it will be just easy to forget setting this) |
I have an application where each user authenticates using omniauth to Salesforce to get their own Restforce client for requests to Salesforce.
What's the best way to override the active_force.SObject.client to allow for this to be used, rather than setting up a new Restforce connection that uses application wide environment variables?
The text was updated successfully, but these errors were encountered: