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

Second NewClient() overrides initial NewClient()? #20

Closed
darrenparkinson opened this issue Jul 22, 2021 · 4 comments
Closed

Second NewClient() overrides initial NewClient()? #20

darrenparkinson opened this issue Jul 22, 2021 · 4 comments

Comments

@darrenparkinson
Copy link

Hi, me again ;)

I'm experiencing a weird issue which I suspect is obvious, but I can't quite get my head around it...

Essentially, if I create two clients with NewClient() using two separate tokens with SetAuthToken() the second one seems to overwrite the first.

For example, this code works as expected:

	client1 := webexteams.NewClient()
	client1.SetAuthToken("first token here")
	me1, _, _ := client1.People.GetMe()
	fmt.Println(me1.DisplayName)
	
	client2 := webexteams.NewClient()
	client2.SetAuthToken("second token here")
	me2, _, _ := client2.People.GetMe()
	fmt.Println(me2.DisplayName)

But this does not, it basically prints the second clients display name twice

	client1 := webexteams.NewClient()
	client1.SetAuthToken("first token here")
	client2 := webexteams.NewClient()
	client2.SetAuthToken("second token here")

	me1, _, _ := client1.People.GetMe()
	fmt.Println(me1.DisplayName)
	me2, _, _ := client2.People.GetMe()
	fmt.Println(me2.DisplayName)

Any thoughts before I start digging into it further greatly appreciated.

Thanks.

@darrenparkinson
Copy link
Author

darrenparkinson commented Jul 23, 2021

I think it might be the use of the global variable RestyClient which is overridden each time a new client is initialised and then used for API calls:

var RestyClient *resty.Client

@darrenparkinson
Copy link
Author

darrenparkinson commented Aug 2, 2021

I tried the following changes which seemed to work. Any chance you could try it out and maybe implement? Thanks.

api-client.go:

  • modify service client to be *resty.Client instead of *Client (not sure if this gets used anywhere else?)
  • update NewClient to add the result of resty.New() to c.common.client where c := &Client{}
type service struct {
	client *resty.Client
}
func NewClient() *Client {
	client := resty.New()
        client.SetHostURL(apiURL)
	if os.Getenv("WEBEX_TEAMS_ACCESS_TOKEN") != "" {
		client.SetAuthToken(os.Getenv("WEBEX_TEAMS_ACCESS_TOKEN"))
	}

	c := &Client{}
	c.common.client = client

        ...
}

then in each of the services, e.g. people_api.go:

  • replace instances of RestyClient.R() with s.client.R()
func (s *PeopleService) GetMe() (*Person, *resty.Response, error) {

	path := "/people/me"

	response, err := s.client.R().
		SetResult(&Person{}).
		SetError(&Error{}).
		Get(path)

	if err != nil {
		return nil, nil, err
	}

	result := response.Result().(*Person)
	return result, response, err

}

I also had to update the pagination functions to make them methods of the service and then update the List methods to reference it under s.

@jbogarin
Copy link
Owner

jbogarin commented Aug 5, 2021

@darrenparkinson Sorry for the delay. Let me test this tomorrow.

@jbogarin
Copy link
Owner

jbogarin commented Aug 7, 2021

@darrenparkinson Thanks a lot for your valuable input. I tested your recommendations and included them in the repo, plus I created tag 0.4.2

@jbogarin jbogarin closed this as completed Aug 7, 2021
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