mtvbrianking /
laravel-beyonic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bmatovu\Beyonic\Traits; |
||
| 4 | |||
| 5 | use GuzzleHttp\Client; |
||
| 6 | use GuzzleHttp\HandlerStack; |
||
| 7 | use GuzzleHttp\MessageFormatter; |
||
| 8 | use GuzzleHttp\Middleware; |
||
| 9 | use Illuminate\Container\Container; |
||
| 10 | use Illuminate\Contracts\Config\Repository; |
||
| 11 | use Monolog\Handler\StreamHandler; |
||
| 12 | use Monolog\Level; |
||
| 13 | use Psr\Log\LoggerInterface; |
||
| 14 | |||
| 15 | trait HttpClient |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Create concrete HTTP client. |
||
| 19 | * |
||
| 20 | * @throws \Exception |
||
| 21 | * |
||
| 22 | * @return \GuzzleHttp\ClientInterface |
||
| 23 | */ |
||
| 24 | 3 | protected function makeClient() |
|
| 25 | { |
||
| 26 | 3 | $config = Container::getInstance()->make(Repository::class); |
|
| 27 | |||
| 28 | 3 | $handlerStack = HandlerStack::create(); |
|
| 29 | |||
| 30 | 3 | if ($config->get('debug')) { |
|
| 31 | $handlerStack->push($this->getLogMiddleware()); |
||
| 32 | } |
||
| 33 | |||
| 34 | 3 | $options = array_merge([ |
|
| 35 | 3 | 'handler' => $handlerStack, |
|
| 36 | // 'progress' => function () { |
||
| 37 | // echo '. '; |
||
| 38 | // }, |
||
| 39 | 3 | 'base_uri' => $config->get('beyonic.api.base_uri'), |
|
| 40 | 3 | 'headers' => [ |
|
| 41 | 3 | 'Accept' => 'application/json', |
|
| 42 | 3 | 'Beyonic-Version' => $config->get('beyonic.api.version'), |
|
| 43 | 3 | 'Authorization' => 'Token ' . $config->get('beyonic.api.token'), |
|
| 44 | 3 | 'Content-Type' => 'application/json', |
|
| 45 | 3 | ], |
|
| 46 | 3 | ], (array) $config->get('beyonic.guzzle.options')); |
|
| 47 | |||
| 48 | 3 | return new Client($options); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get log middleware. |
||
| 53 | * |
||
| 54 | * @throws \Exception |
||
| 55 | * |
||
| 56 | * @return callable GuzzleHttp middleware |
||
| 57 | */ |
||
| 58 | protected function getLogMiddleware() |
||
| 59 | { |
||
| 60 | $logManager = Container::getInstance()->make(LoggerInterface::class); |
||
| 61 | $logger = $logManager->getLogger(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 62 | $streamHandler = new StreamHandler(storage_path('logs/beyonic.log')); |
||
| 63 | $logger->pushHandler($streamHandler, Level::Debug); |
||
| 64 | $messageFormatter = new MessageFormatter(MessageFormatter::DEBUG); |
||
| 65 | |||
| 66 | return Middleware::log($logger, $messageFormatter); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |