|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * PHP version 8 |
| 5 | + * |
| 6 | + * @category Library |
| 7 | + * @package Libraries |
| 8 | + * @author Made Mas Adi Winata <m45adiwinata@gmail.com> |
| 9 | + * @license https://mit-license.org/ MIT License |
| 10 | + * @version GIT: 0.3.7 |
| 11 | + * @link https://github.com/spotlibs |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace Spotlibs\PhpLib\Libraries; |
| 17 | + |
| 18 | +use Carbon\Exceptions\InvalidTypeException; |
| 19 | +use Exception; |
| 20 | +use GuzzleHttp\Client as BaseClient; |
| 21 | +use GuzzleHttp\Exception\GuzzleException; |
| 22 | +use GuzzleHttp\Psr7\Request; |
| 23 | +use GuzzleHttp\RequestOptions; |
| 24 | +use Psr\Http\Message\ResponseInterface; |
| 25 | +use Spotlibs\PhpLib\Libraries\ClientHelpers\Multipart; |
| 26 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 27 | + |
| 28 | +/** |
| 29 | + * ClientTimeoutUnit |
| 30 | + * |
| 31 | + * Name for HTTP Client timeout unit |
| 32 | + * |
| 33 | + * @category HttpClient |
| 34 | + * @package Client |
| 35 | + * @author Made Mas Adi Winata <m45adiwinata@gmail.com> |
| 36 | + * @license https://mit-license.org/ MIT License |
| 37 | + * @link https://github.com/spotlibs |
| 38 | + */ |
| 39 | +class Client extends BaseClient |
| 40 | +{ |
| 41 | + /** |
| 42 | + * Timeout in seconds, default is 10 seconds |
| 43 | + * |
| 44 | + * @var float $timeout |
| 45 | + */ |
| 46 | + public float $timeout = 10; |
| 47 | + /** |
| 48 | + * Set to true to enable SSL certificate verification and use the default CA bundle provided by operating system |
| 49 | + * |
| 50 | + * @var bool $verify |
| 51 | + */ |
| 52 | + public bool $verify = false; |
| 53 | + /** |
| 54 | + * Request body, set according to the request |
| 55 | + * |
| 56 | + * @var array $body |
| 57 | + */ |
| 58 | + protected array $body = []; |
| 59 | + /** |
| 60 | + * Request header if only headers are not set in the request. Will be appended on call method |
| 61 | + * |
| 62 | + * @var array $requestHeaders |
| 63 | + */ |
| 64 | + protected array $requestHeaders = []; |
| 65 | + /** |
| 66 | + * Customize response header |
| 67 | + * |
| 68 | + * @var array $responseHeaders |
| 69 | + */ |
| 70 | + protected array $responseHeaders = []; |
| 71 | + /** |
| 72 | + * Body type of the |
| 73 | + * |
| 74 | + * @var array $responseHeaders |
| 75 | + */ |
| 76 | + protected string $request_body_type = 'json'; |
| 77 | + |
| 78 | + /** |
| 79 | + * Create a new Client instance. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function __construct() |
| 84 | + { |
| 85 | + parent::__construct(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the timeout for Http Client |
| 90 | + * |
| 91 | + * @param int $timeout number of desired timeout |
| 92 | + * |
| 93 | + * @return self |
| 94 | + */ |
| 95 | + public function setTimeout(float $timeout): self |
| 96 | + { |
| 97 | + $this->timeout = $timeout; |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set verify |
| 103 | + * |
| 104 | + * @param bool $verify number of desired timeout |
| 105 | + * |
| 106 | + * @return self |
| 107 | + */ |
| 108 | + public function setVerify(bool $verify): self |
| 109 | + { |
| 110 | + $this->verify = $verify; |
| 111 | + return $this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Set request body |
| 116 | + * |
| 117 | + * @param array $body number of desired timeout |
| 118 | + * |
| 119 | + * @return self |
| 120 | + */ |
| 121 | + public function setRequestBody(array $body): self |
| 122 | + { |
| 123 | + $this->body = $body; |
| 124 | + return $this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Set request body in associative array |
| 129 | + * |
| 130 | + * @param string $form_type RequestOptions::FORM_PARAMS|JSON|MULTIPART|QUERY|... |
| 131 | + * |
| 132 | + * @return self |
| 133 | + */ |
| 134 | + public function setFormType(string $form_type): self |
| 135 | + { |
| 136 | + $allowed = [ |
| 137 | + RequestOptions::FORM_PARAMS, |
| 138 | + RequestOptions::JSON, |
| 139 | + RequestOptions::MULTIPART, |
| 140 | + RequestOptions::QUERY, |
| 141 | + RequestOptions::BODY |
| 142 | + ]; |
| 143 | + if (!in_array($form_type, $allowed)) { |
| 144 | + throw new Exception('form type not allowed. supporting ' . implode(", ", $allowed)); |
| 145 | + } |
| 146 | + $this->request_body_type = $form_type; |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Set request headers in associative array |
| 152 | + * |
| 153 | + * @param array<string[]> $headers example: ['Content-Type' => ['application/json']] |
| 154 | + * |
| 155 | + * @return self |
| 156 | + */ |
| 157 | + public function injectRequestHeader(array $headers): self |
| 158 | + { |
| 159 | + $this->requestHeaders = $headers; |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Set response headers in associative array |
| 165 | + * |
| 166 | + * @param array<string[]> $headers example: ['Content-Type' => ['application/json']] |
| 167 | + * |
| 168 | + * @return self |
| 169 | + */ |
| 170 | + public function injectResponseHeader(array $headers): self |
| 171 | + { |
| 172 | + $this->responseHeaders = $headers; |
| 173 | + return $this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Set the timeout for Http Client |
| 178 | + * |
| 179 | + * @param Request $request HTTP Request instance |
| 180 | + * |
| 181 | + * @return ResponseInterface |
| 182 | + */ |
| 183 | + public function call(Request $request): ResponseInterface |
| 184 | + { |
| 185 | + $body = []; |
| 186 | + if (!empty($this->body)) { |
| 187 | + $this->checkMultipartBody(); |
| 188 | + $body = [ |
| 189 | + $this->request_body_type => $this->body |
| 190 | + ]; |
| 191 | + } |
| 192 | + $options = ['timeout' => $this->timeout]; |
| 193 | + $options = array_merge($options, $body); |
| 194 | + foreach ($this->requestHeaders as $key => $header) { |
| 195 | + $request->withHeader($key, $header); |
| 196 | + } |
| 197 | + $response = $this->send($request, $options); |
| 198 | + foreach ($this->responseHeaders as $key => $header) { |
| 199 | + $response->withHeader($key, $header); |
| 200 | + } |
| 201 | + return $response; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Check and setup multipart request body if form type is multipart |
| 206 | + * |
| 207 | + * @return void |
| 208 | + */ |
| 209 | + private function checkMultipartBody(): void |
| 210 | + { |
| 211 | + if ($this->request_body_type == RequestOptions::MULTIPART) { |
| 212 | + $temp = []; |
| 213 | + $key_of_contents = []; |
| 214 | + foreach ($this->body as $key => $b) { |
| 215 | + if (! $b instanceof Multipart) { |
| 216 | + throw new InvalidTypeException('Request body does not comply multipart form-data structure'); |
| 217 | + } |
| 218 | + if (is_array($b->contents)) { |
| 219 | + $key_of_contents[] = $key; |
| 220 | + /** |
| 221 | + * Check if contents is array of files |
| 222 | + * |
| 223 | + * @var array $b->contents |
| 224 | + */ |
| 225 | + if (isset($b->contents[0]) && $b->contents[0] instanceof \Illuminate\Http\UploadedFile) { |
| 226 | + $z = $b->contents; |
| 227 | + /** |
| 228 | + * Array $b->contents |
| 229 | + * |
| 230 | + * @var \Illuminate\Http\UploadedFile[] $z |
| 231 | + */ |
| 232 | + foreach ($z as $v) { |
| 233 | + /** |
| 234 | + * Multipart |
| 235 | + * |
| 236 | + * @var \Illuminate\Http\UploadedFile $v multipart |
| 237 | + */ |
| 238 | + $y = new Multipart(['name' => $b->name . '[]', 'headers' => ['Content-Type' => $v->getMimeType()]]); |
| 239 | + $y->contents = fopen($v->getRealPath(), 'r'); |
| 240 | + array_push($temp, $y->toArray()); |
| 241 | + } |
| 242 | + } |
| 243 | + } else { |
| 244 | + $x = $this->body[$key]; |
| 245 | + /** |
| 246 | + * Multipart |
| 247 | + * |
| 248 | + * @var Multipart $x multipart |
| 249 | + */ |
| 250 | + if ($x->contents instanceof \Illuminate\Http\UploadedFile) { |
| 251 | + $z = $x->contents; |
| 252 | + /** |
| 253 | + * Uploaded file |
| 254 | + * |
| 255 | + * @var \Illuminate\Http\UploadedFile $z uploaded file |
| 256 | + */ |
| 257 | + $x->contents = fopen($z->getRealPath(), 'r'); |
| 258 | + } |
| 259 | + $this->body[$key] = $x->toArray(); |
| 260 | + } |
| 261 | + } |
| 262 | + if (count($temp) > 0) { |
| 263 | + foreach ($key_of_contents as $key) { |
| 264 | + unset($this->body[$key]); |
| 265 | + } |
| 266 | + $this->body = array_values($this->body); |
| 267 | + $this->body = array_merge($this->body, $temp); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | +} |
0 commit comments