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

Help needed. Dynamic API token #21

Closed
BKirev opened this issue Jun 27, 2018 · 5 comments
Closed

Help needed. Dynamic API token #21

BKirev opened this issue Jun 27, 2018 · 5 comments

Comments

@BKirev
Copy link

BKirev commented Jun 27, 2018

So far I have this put in a InitializeDO method. Not sure if it's the right way but it works just fine. Correct me if there's a better way to set the API token dynamically.

$this->adapter = new Adapters\ConnectionFactory();
$config = [
	'driver'  => 'guzzlehttp',
	'token'   => Auth::user()->do_api_token
];
$this->adapter->make($config);
$this->dof = new DigitalOceanFactory($this->adapter);
$this->dof->make($config);
$this->do = $this->dof->make($config);

The problem is that I can't access the user(Auth::user()) in the consturctor of the controller, so I'm calling InitializeDO in each one of my methods(index, show, edit, etc.). I don't think this is the best approach.
And here comes my question, how can I write this, so it's called just once and working with $this->do afterwards?

@GrahamCampbell
Copy link
Owner

No need to do all this in the constructor. Just, in your controller method:

$c = DigitalOcean::getFactory()->make(['driver' => 'guzzlehttp', 'token' => Auth::user()->do_api_token]);

Also, you definitely shouldn't ever need to write "new factory". That should always set off alarm bells. You should always check the source code to see if it's registered in the service container. In the case of this package it is, but you can also access it from the facade as in my first example. If you wanted a dependency injection approach, you could have:

use GrahamCampbell\DigitalOcean\DigitalOceanFactory;

// ...

public function myHandler(DigitalOceanFactory $do)
{
    $c = $do->make(['driver' => 'guzzlehttp', 'token' => Auth::user()->do_api_token]);
}

@ppshobi
Copy link

ppshobi commented Jul 29, 2020

When I do this, I am getting invalid Argument Exception 'The DigitalOcean factory requires an auth method'

@GrahamCampbell
Copy link
Owner

@ppshobi That's because that code example was for an old version of this package. In the new version, you don't need to specify a driver (in fact, if you do, it just gets ignored), but you must now specify an authentication method. Currently, there's a choice of none or token, as documented at https://github.com/GrahamCampbell/Laravel-DigitalOcean#digitalocean-connections. Some example config is provided at https://github.com/GrahamCampbell/Laravel-DigitalOcean/blob/7.0/config/digitalocean.php#L42-L45. That exact config is what can be passed at runtime to the factory, if you aren't specifying the token in the config file.

$client = DigitalOcean::getFactory()->make(['method' => 'token', 'token' => 'YOUR-TOKEN-HERE']);

@GrahamCampbell
Copy link
Owner

You can also look at the tests to see how the factory should be used: https://github.com/GrahamCampbell/Laravel-DigitalOcean/blob/7.0/tests/DigitalOceanFactoryTest.php#L31-L39.

@ppshobi
Copy link

ppshobi commented Jul 29, 2020 via email

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

3 participants