| 1 | <?php |
||
| 2 | |||
| 3 | namespace Freyo\ApiGateway\Kernel\Traits; |
||
| 4 | |||
| 5 | use Freyo\ApiGateway\Kernel\Support\Arr; |
||
| 6 | use GuzzleHttp\Client; |
||
| 7 | use GuzzleHttp\ClientInterface; |
||
| 8 | use GuzzleHttp\HandlerStack; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Trait HasHttpRequests |
||
| 12 | */ |
||
| 13 | trait HasHttpRequests |
||
| 14 | { |
||
| 15 | use ResponseCastable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var \GuzzleHttp\ClientInterface |
||
| 19 | */ |
||
| 20 | protected $httpClient; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $middlewares = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \GuzzleHttp\HandlerStack |
||
| 29 | */ |
||
| 30 | protected $handlerStack; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $defaults = [ |
||
| 36 | 'curl' => [ |
||
| 37 | CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, |
||
| 38 | ], |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Set guzzle default settings. |
||
| 43 | * |
||
| 44 | * @param array $defaults |
||
| 45 | */ |
||
| 46 | public static function setDefaultOptions($defaults = []) |
||
| 47 | { |
||
| 48 | self::$defaults = $defaults; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return current guzzle default settings. |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | public static function getDefaultOptions() |
||
| 57 | { |
||
| 58 | return self::$defaults; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set GuzzleHttp\Client. |
||
| 63 | * |
||
| 64 | * @param \GuzzleHttp\ClientInterface $httpClient |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function setHttpClient(ClientInterface $httpClient) |
||
| 69 | { |
||
| 70 | $this->httpClient = $httpClient; |
||
| 71 | |||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return GuzzleHttp\Client instance. |
||
| 77 | * |
||
| 78 | * @return \GuzzleHttp\Client |
||
| 79 | */ |
||
| 80 | public function getHttpClient() |
||
| 81 | { |
||
| 82 | if (!($this->httpClient instanceof ClientInterface)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 83 | $this->httpClient = new Client(); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this->httpClient; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add a middleware. |
||
| 91 | * |
||
| 92 | * @param callable $middleware |
||
| 93 | * @param null|string $name |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function pushMiddleware(callable $middleware, $name = null) |
||
| 98 | { |
||
| 99 | if (!is_null($name)) { |
||
| 100 | $this->middlewares[$name] = $middleware; |
||
| 101 | } else { |
||
| 102 | array_push($this->middlewares, $middleware); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return all middlewares. |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public function getMiddlewares() |
||
| 114 | { |
||
| 115 | return $this->middlewares; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Make a request. |
||
| 120 | * |
||
| 121 | * @param string $url |
||
| 122 | * @param string $method |
||
| 123 | * @param array $options |
||
| 124 | * |
||
| 125 | * @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
||
| 126 | */ |
||
| 127 | public function request($url, $method = 'GET', $options = []) |
||
| 128 | { |
||
| 129 | $method = strtoupper($method); |
||
| 130 | |||
| 131 | $options = array_merge(self::$defaults, $options, ['handler' => $this->getHandlerStack()]); |
||
| 132 | |||
| 133 | $options = $this->fixJsonIssue($options); |
||
| 134 | |||
| 135 | if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) { |
||
| 136 | $options['base_uri'] = $this->baseUri; |
||
| 137 | } |
||
| 138 | |||
| 139 | $response = $this->getHttpClient()->request($method, $url, $options); |
||
| 140 | $response->getBody()->rewind(); |
||
| 141 | |||
| 142 | return $response; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param \GuzzleHttp\HandlerStack $handlerStack |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function setHandlerStack(HandlerStack $handlerStack) |
||
| 151 | { |
||
| 152 | $this->handlerStack = $handlerStack; |
||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Build a handler stack. |
||
| 158 | * |
||
| 159 | * @return \GuzzleHttp\HandlerStack |
||
| 160 | */ |
||
| 161 | public function getHandlerStack() |
||
| 162 | { |
||
| 163 | if ($this->handlerStack) { |
||
| 164 | return $this->handlerStack; |
||
| 165 | } |
||
| 166 | |||
| 167 | $this->handlerStack = HandlerStack::create(); |
||
| 168 | |||
| 169 | foreach ($this->middlewares as $name => $middleware) { |
||
| 170 | $this->handlerStack->push($middleware, $name); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this->handlerStack; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $options |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function fixJsonIssue(array $options) |
||
| 182 | { |
||
| 183 | if (isset($options['json']) && is_array($options['json'])) { |
||
| 184 | $options['headers'] = array_merge(Arr::get($options, 'headers', []), ['Content-Type' => 'application/json']); |
||
| 185 | if (empty($options['json'])) { |
||
| 186 | $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT); |
||
| 187 | } else { |
||
| 188 | $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE); |
||
| 189 | } |
||
| 190 | unset($options['json']); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $options; |
||
| 194 | } |
||
| 195 | } |