PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.8
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.8
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / vendor / codeinwp / optimole-sdk / src / Http / GuzzleClient.php

GuzzleClient.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.8, at vendor/codeinwp/optimole-sdk/src/Http/GuzzleClient.php

89 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * This file is part of Optimole PHP SDK.
7 *
8 * (c) Optimole Team <friends@optimole.com>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Optimole\Sdk\Http;
15
16 use GuzzleHttp\ClientInterface as GuzzleClientInterface;
17 use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponseException;
18 use GuzzleHttp\Exception\GuzzleException;
19 use GuzzleHttp\Psr7\Request;
20 use Optimole\Sdk\Exception\BadResponseException;
21 use Optimole\Sdk\Exception\InvalidArgumentException;
22 use Optimole\Sdk\Exception\RuntimeException;
23 use Optimole\Sdk\Optimole;
24
25 class GuzzleClient implements ClientInterface
26 {
27 /**
28 * The Guzzle HTTP client.
29 */
30 private GuzzleClientInterface $client;
31
32 /**
33 * Constructor.
34 */
35 public function __construct(GuzzleClientInterface $client)
36 {
37 $this->client = $client;
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array
44 {
45 try {
46 $response = $this->client->send($this->createRequest($method, $url, $body, $headers), ['verify' => false]);
47 } catch (GuzzleBadResponseException $exception) {
48 throw new BadResponseException($exception->getMessage(), $exception->getCode(), $exception);
49 } catch (GuzzleException $exception) {
50 throw new RuntimeException($exception->getMessage(), $exception->getCode(), $exception);
51 }
52
53 $body = (string) $response->getBody();
54
55 if (empty($body)) {
56 return null;
57 }
58
59 $body = (array) json_decode($body, true);
60
61 if (JSON_ERROR_NONE !== json_last_error()) {
62 throw new BadResponseException(sprintf('Unable to decode JSON response: %s', json_last_error_msg()));
63 }
64
65 return $body;
66 }
67
68 /**
69 * Create a request object.
70 */
71 private function createRequest(string $method, string $url, $body = null, array $headers = []): Request
72 {
73 if (is_array($body)) {
74 $body = json_encode($body);
75 }
76
77 if (null !== $body && !is_string($body)) {
78 throw new InvalidArgumentException('"body" must be a string or an array');
79 }
80
81 $headers = array_merge($headers, [
82 'User-Agent' => sprintf('optimole-sdk-php/%s', Optimole::VERSION),
83 ]);
84 $method = strtolower($method);
85
86 return new Request($method, $url, $headers, $body);
87 }
88 }
89