mardi 31 octobre 2017

Laravel bootstrap a default Guzzle client

I would like to bootstrap a default Guzzle client to connect with Github. Currently I am setting up a default client in a controller like so:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        $this->client = new Client([
            'base_uri' => 'https://api.github.com',
            'headers' => [
                'Authorization' => 'token ' . $this->getAccessToken()
            ]
        ]);

        return $next($request);
    });
}

I am wrapping it with the middleware closure because otherwise the user that is currently logged in is not available yet. getAccessToken needs an Auth::user.

How would I rewrite this to a generic re-usable client though? Judging by the documentation from Laravel a good idea might be to bind a GithubGuzzleClient into the container through a ServiceProvider:

http://ift.tt/2z1320p

It seems like ServiceProviders are bound into the container before the middleware is even run though. So how would I set up a generic ServiceProvider that can also access the logged in user to set the proper Authorization header on the freshly instantiated client?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gZcnyO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire