CloudConvert\Api::rawCall PHP Метод

rawCall() приватный Метод

This is the main method of this wrapper. It will sign a given query and return its result.
private rawCall ( string $method, string $path, string $content = null, boolean $is_authenticated = true ) : mixed
$method string HTTP method of request (GET,POST,PUT,DELETE)
$path string relative url of API request
$content string body of the request
$is_authenticated boolean if the request use authentication
Результат mixed
    private function rawCall($method, $path, $content = null, $is_authenticated = true)
    {
        $url = $path;
        if (strpos($path, '//') === 0) {
            $url = $this->protocol . ":" . $path;
        } elseif (strpos($url, 'http') !== 0) {
            $url = $this->protocol . '://' . $this->endpoint . $path;
        }
        $options = array('query' => array(), 'body' => null, 'headers' => array());
        if (is_array($content) && $method == 'GET') {
            $options['query'] = $content;
        } elseif (gettype($content) == 'resource' && $method == 'PUT') {
            // is upload
            $options['body'] = \GuzzleHttp\Psr7\stream_for($content);
        } elseif (is_array($content)) {
            $body = json_encode($content);
            $options['body'] = \GuzzleHttp\Psr7\stream_for($body);
            $options['headers']['Content-Type'] = 'application/json; charset=utf-8';
        }
        if ($is_authenticated) {
            $options['headers']['Authorization'] = 'Bearer ' . $this->api_key;
        }
        try {
            $response = $this->http_client->request($method, $url, $options);
            if ($response->getHeader('Content-Type') && strpos($response->getHeader('Content-Type')[0], 'application/json') === 0) {
                return json_decode($response->getBody(), true);
            } elseif ($response->getBody()->isReadable()) {
                // if response is a download, return the stream
                return $response->getBody();
            }
        } catch (RequestException $e) {
            if (!$e->getResponse()) {
                throw $e;
            }
            // check if response is JSON error message from the CloudConvert API
            $json = json_decode($e->getResponse()->getBody(), true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                throw new \RuntimeException('Error parsing JSON response');
            }
            if (isset($json['message']) || isset($json['error'])) {
                $msg = isset($json['error']) ? $json['error'] : $json['message'];
                $code = $e->getResponse()->getStatusCode();
                if ($code == 400) {
                    throw new Exceptions\ApiBadRequestException($msg, $code);
                } elseif ($code == 422) {
                    throw new Exceptions\ApiConversionFailedException($msg, $code);
                } elseif ($code == 503) {
                    throw new Exceptions\ApiTemporaryUnavailableException($msg, $code, $e->getResponse()->getHeader('Retry-After') ? $e->getResponse()->getHeader('Retry-After')[0] : null);
                } else {
                    throw new Exceptions\ApiException($msg, $code);
                }
            } else {
                throw $e;
            }
        }
    }