Make a request to the API.

@param string             $method   HTTP Method
@param string             $endpoint API endpoint
@param mixed|string|array $json     POST body (optional)

@throws Exception

@return mixed
332031
protected function request(string $method, string $endpoint, $json = null)
    {
        $uri = $this->buildUri($endpoint);
        $options = $json != null ? ['json' => $json] : [];

        try {
            $response = $this->client->request($method, $uri, $options);
        } catch (ClientException $exception) {
            throw $this->determineException($exception);
        }

        $body = (string) $response->getBody();

        return $this->jsonValidator($body) ? json_decode($body, true) : $body;
    }
1223328
public String putJson(String endpoint, String params, JSONObject json) throws IOException {
        return this.putJson(endpoint, params, json, "");
    }
800920
public function call($method, $params = [])
    {
        if (null === $this->client) {
            $this->client = new Client([
                'base_uri' => static::API_URL,
                'content-type' => 'application/json',
            ]);
        }

        $requestParams = array_merge($this->defaultParams, [
            $method => [$params],
        ]);

        if ($this->sandbox) {
            $requestParams['apikey'] = self::SANDBOX_KEY;
        }

        $response = $this->client->post(null, ['body' => json_encode($requestParams)]);
        if ($response->getStatusCode() != 200) {
            throw new \Exception('Api http error: ' . $response->getStatusCode(), $response->getStatusCode());
        }

        $result = json_decode($response->getBody(), true);
        if (isset($result['error'])) {
            throw new \BadMethodCallException('Api error: ' . $result['error']['description'],
                $result['error']['code']);
        }

        return $result;
    }
1028268
public function post($endpoint, array $options = [])
    {
        $jsonValues = [];

        if (isset($options['json'])) {
            $jsonValues = $options['json'];
        }

        return $this->httpClient->request('POST', 'https://api.craftcms.com/v1/'.$endpoint, [
            'auth' => $this->setAuth(),
            'json' => $jsonValues
        ]);
    }
236843
public function api(string $method, string $endpoint, array $data = []): array
    {
        $endpoint = '/' . ltrim($endpoint, '/'); // Ensure leading slash is present
        return $this->api->call($method, $endpoint, $data);
    }
956849
private function getOrCreateEndpoint($endpoint)
    {
        if (!array_key_exists($endpoint, $this->endpoints)) {
            if (!array_key_exists($endpoint, self::$endpointClassMap)) {
                throw new \Exception(sprintf('Unknown endpoint "%s"', $endpoint));
            }

            $endpointClass = self::$endpointClassMap[$endpoint];
            $this->endpoints[$endpoint] = $this->createEndpoint($endpointClass);
        }

        return $this->endpoints[$endpoint];
    }
799674
public HttpRequest any(int method,String endpoint, JsonArray array){
        switch (method) {
            case HttpRequest.GET:
                return get(endpoint);
            case HttpRequest.POST:
                return post(endpoint, array);
            case HttpRequest.PUT:
                return put(endpoint, array);
            case HttpRequest.DELETE:
                return delete(endpoint,array);
            case HttpRequest.PATCH:
                throw new UnsupportedOperationException("method not supported");
            default:
                throw new IllegalArgumentException("method is not valid");
        }
    }
535902
public function sendRequest($action, $json = false, $expectResponse = false, $method = 'POST')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->endpoint.$action);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

        curl_setopt(
            $ch,
            CURLOPT_HTTPHEADER,
            array(
                "Authorization: $this->service_key",
                "Content-Type: application/json",
                "X-wp-client-user-agent: $this->client_user_agent",
                "Content-Length: " . strlen($json)
            )
        );
        // Disabling SSL used for localhost testing
        if ($this->ssl_check === false) {
            if (substr($this->service_key, 0, 1) != 'T') {
                Error::throwError('ssl');
            }
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }

        $result = curl_exec($ch);
        $info = curl_getinfo($ch);
        $err = curl_error($ch);
        $errno = curl_errno($ch);
        curl_close($ch);

        // Curl error
        if ($result === false) {
            if ($errno === 60) {
                Error::throwError('sslerror', false, $errno, null, $err);
            } elseif ($errno === 28) {
                Error::throwError('timeouterror', false, $errno, null, $err);
            } else {
                Error::throwError('uanv', false, $errno, null, $err);
            }
        }

        if (substr($result, -1) != '}') {
            $result = substr($result, 0, -1);
        }

        // Decode JSON
        $response = self::handleResponse($result);

        // Check JSON has decoded correctly
        if ($expectResponse && ($response === null || $response === false )) {
            Error::throwError('uanv', Error::$errors['json'], 503);
        }

        // Check the status code exists
        if (isset($response["httpStatusCode"])) {

            if ($response["httpStatusCode"] != 200) {
                Error::throwError(
                    false,
                    $response["message"],
                    $info['http_code'],
                    $response['httpStatusCode'],
                    $response['description'],
                    $response['customCode']
                );

            }

        } elseif ($expectResponse && $info['http_code'] != 200) {
            // If we expect a result and we have an error
            Error::throwError('uanv', Error::$errors['json'], 503);

        } elseif (!$expectResponse) {

            if ($info['http_code'] != 200) {
                Error::throwError('apierror', $result, $info['http_code']);
            } else {
                $response = true;
            }
        }

        return $response;
    }
902141
protected function createRequest($endpoint, $method, $data = [], $parameters = [])
    {
        $body    = '';
        $url     = $this->url . $endpoint;
        $hasData = !empty($data);

        // Setup authentication.
        $parameters = $this->authenticate($url, $method, $parameters);

        // Setup method.
        $this->setupMethod($method);

        // Include post fields.
        if ($hasData) {
            $body = \json_encode($data);
            \curl_setopt($this->ch, CURLOPT_POSTFIELDS, $body);
        }

        $this->request = new Request(
            $this->buildUrlQuery($url, $parameters),
            $method,
            $parameters,
            $this->getRequestHeaders($hasData),
            $body
        );

        return $this->getRequest();
    }
1117368
private function doUpload ($endpoint, $type, $body)
    {
        return json_decode(
            $this->client->send(
                new Request(
                    'POST',
                    Uri::withQueryValue(new Uri($endpoint), 'type', $type),
                    [],
                    $body
                )
            )->getBody()
        );
    }
223633
public static function createSerializer(Service $api, $endpoint)
    {
        static $mapping = [
            'json'      => 'Aws\Api\Serializer\JsonRpcSerializer',
            'query'     => 'Aws\Api\Serializer\QuerySerializer',
            'rest-json' => 'Aws\Api\Serializer\RestJsonSerializer',
            'rest-xml'  => 'Aws\Api\Serializer\RestXmlSerializer'
        ];

        $proto = $api->getProtocol();

        if (isset($mapping[$proto])) {
            return new $mapping[$proto]($api, $endpoint);
        }

        if ($proto == 'ec2') {
            return new QuerySerializer($api, $endpoint, new Ec2ParamBuilder());
        }

        throw new \UnexpectedValueException(
            'Unknown protocol: ' . $api->getProtocol()
        );
    }
339050
private function validateEndpoint($endpoint)
    {
        $isValid = filter_var($endpoint, FILTER_VALIDATE_URL);

        if ($isValid) {
            $scheme = strtolower(parse_url($endpoint, PHP_URL_SCHEME));
            if (in_array($scheme, ['http', 'https'], true)) {
                return;
            }
        }

        throw new Exception(self::ERR_INVALID_ENDPOINT);
    }
590887
public function post($endpoint, array $parameters = array())
    {
        return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters);
    }
675044
protected function fetchEndpoint($url, $method = "get", $body = null)
    {
        $result = array();

        if ($method == "post") {
            $request = $this->post($url);

            if (!is_null($body)) {
                $request->setBody($body);
            }
        } else {
            $request = $this->get($url);
        }


        //
        // Error Handling
        //

        try {
            $response = $request->send();
            $result = json_decode($response->getBody(), true);
        } catch (ClientErrorResponseException $e) {
            $result = json_decode($e->getResponse()->getBody(), true);
        } catch (CurlException $e) {
            $result = array(
                "error" => $e->getMessage(),
                "status" => "bad_request",
            );
        } catch (Exception $e) {
            $result = array(
                "error" => $e->getMessage(),
                "status" => "critical_error",
            );
        }

        if (!isset($result["status"])) {
            $result = array(
                "error" => "Unexpected response from server. Check URL?",
                "status" => "critical_error",
            );
        }

        return $result;

    }
1294281
public function execute(TallantoMethodInterface $method)
  {
    // Build options
    $options = [];
    // Is endpoint secured?
    if ($method instanceof AbstractSecuredTallantoMethod) {
      if ($method->isSecured()) {
        $options['auth'] = [$method->getLogin(), $method->getPassword()];
      }
    }
    // Should we supply JSON data in the body?
    if (!is_null($jsonData = $method->getJsonData())) {
      $options['json'] = $jsonData;
    }
    // Get query parameters
    $options['query'] = $method->getQueryParameters();
    // Get request headers
    $options['headers'] = $method->getRequestHeaders();

    // Does endpoint pageable?
    if ($method instanceof PageableTallantoMethodInterface) {
      $options['query']['total_count'] = 'true';
      $options['query']['page_size'] = $method->getPageSize();
      $options['query']['page_number'] = $method->getPageNumber();
    }

    $this->logger->debug(
      'Performing Guzzle {method} request to {uri}',
      [
        'method'  => $method->getMethod(),
        'uri'     => $method->getUri(),
        'options' => $options,
      ]
    );

    // Make request
    try {
      $response = $this->getClient()
        ->request(
          $method->getMethod(),
          $method->getUri(),
          $options
        );
    } catch (RequestException $e) {
      throw new TransportTallantoException(
        'Guzzle request failed: '.$e->getResponse()
          ->getBody()
          ->getContents(), 0, $e
      );
    } catch (\Exception $e) {
      throw new TransportTallantoException('Guzzle request failed.', 0, $e);
    }
    // Assign response
    $method->setResponse($response);
  }
1235684
public function request($method, array $params = [])
    {
        return new Request($this->endpoint, $this->secret, $method, $params);
    }
1099281
protected function createRequest($method, $endpoint, $body = null, array $headers = [])
    {
        $uri = $this->makeBaseUri($endpoint);
        return new Request($method, $uri, $headers, $body);
    }