Created
October 11, 2018 06:10
-
-
Save gpressutto5/f9ccfbdcf0e639c49106ce7c55649648 to your computer and use it in GitHub Desktop.
Multiple laravel http client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Facades; | |
use Illuminate\Support\Facades\Facade; | |
/** | |
* A facade for the Foo HttpClient service. | |
* | |
* @method static \Psr\Http\Message\ResponseInterface get(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \Psr\Http\Message\ResponseInterface head(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \Psr\Http\Message\ResponseInterface put(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \Psr\Http\Message\ResponseInterface post(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \Psr\Http\Message\ResponseInterface patch(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \Psr\Http\Message\ResponseInterface delete(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface getAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface headAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface putAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface postAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface patchAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @method static \GuzzleHttp\Promise\PromiseInterface deleteAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []) | |
* @see \GuzzleHttp\Client | |
*/ | |
class Foo extends Facade | |
{ | |
/** | |
* Get the registered name of the component. | |
* | |
* @return string | |
*/ | |
protected static function getFacadeAccessor() | |
{ | |
return 'foo'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
/** | |
* Foo Http Client register. | |
*/ | |
class FooHttpClientServiceProvider extends HttpClientServiceProvider | |
{ | |
/** | |
* Returns the HttpClient service name. | |
* | |
* @return string | |
*/ | |
protected function getServiceName(): string | |
{ | |
return 'foo'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use GuzzleHttp\Client as Guzzle; | |
use Illuminate\Support\ServiceProvider; | |
/** | |
* Abstract http client service register. | |
*/ | |
abstract class HttpClientServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Returns the HttpClient service name. | |
* | |
* @return string | |
*/ | |
abstract protected function getServiceName(): string; | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$serviceName = $this->getServiceName(); | |
$this->app->bind($serviceName, function () use ($serviceName) { | |
$servicesConfig = $this->app['config']['services']; | |
$guzzleConfig = $servicesConfig['guzzle']; | |
$httpClientConfig = $servicesConfig['http-clients'][$serviceName]; | |
$config = array_merge($guzzleConfig, $httpClientConfig); | |
$client = new Guzzle($config); | |
return $client; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
... | |
// add default guzzle configs to all requests | |
'guzzle' => [ | |
'headers' => [ | |
'Accept' => 'application/json', | |
], | |
], | |
// add default guzzle configs to specific clients | |
'http-clients' => [ | |
'foo' => [ | |
'base_uri' => env('FOO_API_URL'), | |
'auth' => env('FOO_API_SECRET_KEY'), | |
], | |
], | |
]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment