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

Get Metrics from 2 or more projects #24

Open
papazetis opened this issue Aug 23, 2019 · 3 comments
Open

Get Metrics from 2 or more projects #24

papazetis opened this issue Aug 23, 2019 · 3 comments

Comments

@papazetis
Copy link

papazetis commented Aug 23, 2019

I am trying to get the metrics from 2 projects with following code:

Delighted\Client::setApiKey('project_1_api_key');
$customer_metrics = \Delighted\Metrics::retrieve();

Delighted\Client::setApiKey('project_2_api_key');
$partner_metrics = \Delighted\Metrics::retrieve();

But $partner_metrics always has the value from 1st project.

I think that second connection never happens.

@mkdynamic
Copy link
Contributor

@papazetis This is a good question! By default, the library uses a shared client which is what received the API key during the setApiKey call. If you want to use multiple API keys, you need to create a client manually. This is covered briefly here: https://github.com/delighted/delighted-php#advanced-configuration-and-testing -- but here is a concrete example:

$customer_client = \Delighted\Client::getInstance(['apiKey' => 'project_1_api_key']);
$customer_metrics = \Delighted\Metrics::retrieve([], $customer_client);

$partner_client = \Delighted\Client::getInstance(['apiKey' => 'project_2_api_key']);
$partner_metrics = \Delighted\Metrics::retrieve([], $partner_client);

The trick is to pass the client that you manually create as the last argument when calling the relevant API method. It's a bit cumbersome, since you need to pass in the arguments before the last one -- to see the exact arguments needed I would scope the source code https://github.com/delighted/delighted-php/tree/master/lib/Delighted

Hope this helps, let me know if you have any questions.

@papazetis
Copy link
Author

@mkdynamic I tried that trick but it didn't work.
I changed the code a little bit on getInstance method.
I removed if (is_null(self::$instance)) and now I can set 2 apis using setApiKey method

@mkdynamic
Copy link
Contributor

Ah, sounds like a bug, sorry! Looking into this...

@papazetis papazetis reopened this Aug 27, 2019
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