-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathHttpClient.php
More file actions
100 lines (90 loc) Β· 2.36 KB
/
HttpClient.php
File metadata and controls
100 lines (90 loc) Β· 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Exception\Error;
use Cloudinary\Log\LoggerTrait;
use GuzzleHttp\Client;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
/**
* Class HttpClient
*
* @internal
*/
class HttpClient
{
use LoggerTrait;
/**
* @var Client $httpClient The HTTP client instance.
*/
protected Client $httpClient;
/**
* HttpClient constructor.
*
* @param Configuration|null $configuration Configuration source.
*/
public function __construct(?Configuration $configuration = null)
{
$this->httpClient = new Client();
if ($configuration === null) {
$configuration = Configuration::instance();
}
$this->logging = $configuration->logging;
}
/**
* Retrieves JSON from url.
*
* @param string $url The url
*
*
* @throws Error
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getJson(string $url): mixed
{
try {
return self::parseJsonResponse($this->httpClient->get($url));
} catch (Error $e) {
$this->getLogger()->critical(
'Error parsing JSON server response',
[
'exception' => $e->getMessage(),
'class' => self::class,
'url' => $url,
]
);
throw $e;
}
}
/**
* Parses JSON response.
*
* @param ResponseInterface $response Response from HTTP request to Cloudinary server
*
*
* @throws Error
*/
private static function parseJsonResponse(ResponseInterface $response): mixed
{
try {
$responseJson = JsonUtils::decode($response->getBody());
} catch (InvalidArgumentException $iae) {
$message = sprintf(
'Error parsing server response (%s) - %s. Got - %s',
$response->getStatusCode(),
$response->getBody(),
$iae
);
throw new Error($message);
}
return $responseJson;
}
}