GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: //var/www/html/vendor/cloudinary/cloudinary_php/src/HttpClient/HttpClient.php
<?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 $httpClient;

    /**
     * HttpClient constructor.
     * @param Configuration|null $configuration
     */
    public function __construct($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
     *
     * @return mixed
     *
     * @throws Error
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getJson($url)
    {
        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
     *
     * @return mixed
     *
     * @throws Error
     */
    private static function parseJsonResponse($response)
    {
        try {
            $responseJson = JsonUtils::decode($response->getBody(), true);
        } catch (InvalidArgumentException $iae) {
            $message = sprintf(
                'Error parsing server response (%s) - %s. Got - %s',
                $response->getStatusCode(),
                $response->getBody(),
                $iae
            );
            throw new Error($message);
        }

        return $responseJson;
    }
}