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

How to use a client object other than the defaultClient? #5

Open
junpayment opened this issue Nov 19, 2018 · 1 comment
Open

How to use a client object other than the defaultClient? #5

junpayment opened this issue Nov 19, 2018 · 1 comment

Comments

@junpayment
Copy link

Hi,

It seems that the defaultClient is present in the parse package as a singleton.

https://github.com/kylemcc/parse/blob/master/rest.go#L75

var defaultClient *clientT

// Initialize the parse library with your API keys
func Initialize(appId, restKey, masterKey string) {
    defaultClient = &clientT{
        appId:      appId,
        restKey:    restKey,
        masterKey:  masterKey,
        userAgent:  "github.com/kylemcc/parse",
        httpClient: &http.Client{},
    }
}

Therefore, for example, if I want to access multiple parse endpoints, it seems to be necessary to initialize each time, like below.

func (cli *ParseClient) initParseClient() {
	parse.Initialize(cli.appId, cli.restKey, cli.masterKey)
	parse.ServerURL(cli.serverUrl)
}

func (cli *ParseClient) NewQuery(m interface{}) (parse.Query, error) {
	cli.initParseClient()
	query, err := parse.NewQuery(m)
	if err != nil {
		return nil, err
	}

	return query, nil
}

I would like to use multiple client objects, how can I do that?

Thanks your great product!

@kylemcc
Copy link
Owner

kylemcc commented Nov 21, 2018

Hello, @junpayment, unfortunately, that's not possible with the current API. I remember thinking of that when I was putting this together, but never ended up needing it.

It should be easy enough to modify the current API to add an exported Client type, then have the DefaultClient be just that - sort of like Go's http package.

Unfortunately, I don't have time to work on it, but I'll happily accept a PR. If you'd like to take a stab at it, I might do something like the following:

  • Add a Client interface
  • Make all of the things that use defaultClient methods on the Client interface, e.g. NewQuery
  • Implement the Client interface on this type
  • Add new package-level functions such as NewQuery to wrap defaultClient.NewQuery
  • Add a client field to each of unexported struct types returned by those methods, e.g. the query type
  • Add a new package-level function, e.g. NewClient with the same API as Initialize

The result should be a new client type that behaves the same way as the package-level client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants