go-sfdc icon indicating copy to clipboard operation
go-sfdc copied to clipboard

Unable to provide my own Session to the package

Open aheber opened this issue 5 years ago • 2 comments

It seems the package is designed to obtain its own session. I'm working from the idea that a calling org will provide a session for me to use and I do not obtain my own.

To make this work I needed to override part of the Session package but still provide a set of fake Credentials just to get a session created.

I wanted to put this up for discussion before I tried to mangle the package via PR.

I changed session.sessionPasswordResponse to be exported then created an alternate session.Open function that would accept it in and create a Session without trying to login. Staying compatible with the current Session interface so I could pass my session to your systems.

I don't think this solution is very clean because it still requires a sfdc.Configuration object with all the OAuth properties.

// SessionPasswordResponse is the relevant connection details for Salesforce
type SessionPasswordResponse struct {
	AccessToken string `json:"access_token"`
	InstanceURL string `json:"instance_url"`
	TokenType   string
}

// Open is used to authenticate with Salesforce and open a session.  The user will need to
// supply the proper credentials and a HTTP client.
func Open(config sfdc.Configuration, resp *SessionPasswordResponse) (*Session, error) {
	if config.Credentials == nil {
		return nil, errors.New("session: configuration crendentials can not be nil")
	}
	if config.Client == nil {
		return nil, errors.New("session: configuration client can not be nil")
	}
	if config.Version <= 0 {
		return nil, errors.New("session: configuration version can not be less than zero")
	}
	session := &Session{
		response: resp,
		config:   config,
	}

	return session, nil
}

aheber avatar Aug 20 '19 18:08 aheber

this is not the case. If you look at the session package, you just need to provide the session interfaces to any of the rest api calls. This allows the user to open a session any way that they would like and just implement the interfaces.

g8rswimmer avatar Aug 25 '19 02:08 g8rswimmer

I'm sorry, I don't think I stated my concern well. There is nothing in the package that is ready to accept a raw session & org URL. I did end up implementing something that satisfies the interfaces and leveraged that in my project, a little simpler than what I placed above.

Is there interest in having a built in part of the project to do this? If so, do you have a preference in how it is implemented? I'm happy to do it and get a PR back to you.

aheber avatar Aug 30 '19 01:08 aheber