public function doRequest()

in php/src/OpenApiClient.php [1053:1316]


  public function doRequest($params, $request, $runtime)
  {
    $_runtime = [
      'key' => ''.($runtime->key ? $runtime->key : $this->_key),
      'cert' => ''.($runtime->cert ? $runtime->cert : $this->_cert),
      'ca' => ''.($runtime->ca ? $runtime->ca : $this->_ca),
      'readTimeout' => (($runtime->readTimeout ? $runtime->readTimeout : $this->_readTimeout) + 0),
      'connectTimeout' => (($runtime->connectTimeout ? $runtime->connectTimeout : $this->_connectTimeout) + 0),
      'httpProxy' => ''.($runtime->httpProxy ? $runtime->httpProxy : $this->_httpProxy),
      'httpsProxy' => ''.($runtime->httpsProxy ? $runtime->httpsProxy : $this->_httpsProxy),
      'noProxy' => ''.($runtime->noProxy ? $runtime->noProxy : $this->_noProxy),
      'socks5Proxy' => ''.($runtime->socks5Proxy ? $runtime->socks5Proxy : $this->_socks5Proxy),
      'socks5NetWork' => ''.($runtime->socks5NetWork ? $runtime->socks5NetWork : $this->_socks5NetWork),
      'maxIdleConns' => (($runtime->maxIdleConns ? $runtime->maxIdleConns : $this->_maxIdleConns) + 0),
      'retryOptions' => $this->_retryOptions,
      'ignoreSSL' => $runtime->ignoreSSL,
      'tlsMinVersion' => $this->_tlsMinVersion,
    ];

    $_retriesAttempted = 0;
    $_lastRequest = null;
    $_lastResponse = null;
    $_context = new RetryPolicyContext([
      'retriesAttempted' => $_retriesAttempted,
    ]);
    while (Dara::shouldRetry($_runtime['retryOptions'], $_context)) {
      if ($_retriesAttempted > 0) {
        $_backoffTime = Dara::getBackoffDelay($_runtime['retryOptions'], $_context);
        if ($_backoffTime > 0) {
          Dara::sleep($_backoffTime);
        }
      }

      $_retriesAttempted++;
      try {
        $_request = new Request();
        $_request->protocol = ''.($this->_protocol ? $this->_protocol : $params->protocol);
        $_request->method = $params->method;
        $_request->pathname = $params->pathname;
        $globalQueries = [ ];
        $globalHeaders = [ ];
        if (!is_null($this->_globalParameters)) {
          $globalParams = $this->_globalParameters;
          if (!is_null($globalParams->queries)) {
            $globalQueries = $globalParams->queries;
          }

          if (!is_null($globalParams->headers)) {
            $globalHeaders = $globalParams->headers;
          }

        }

        $extendsHeaders = [ ];
        $extendsQueries = [ ];
        if (!is_null($runtime->extendsParameters)) {
          $extendsParameters = $runtime->extendsParameters;
          if (!is_null($extendsParameters->headers)) {
            $extendsHeaders = $extendsParameters->headers;
          }

          if (!is_null($extendsParameters->queries)) {
            $extendsQueries = $extendsParameters->queries;
          }

        }

        $_request->query = Dara::merge([
        ], $globalQueries, $extendsQueries, $request->query);
        // endpoint is setted in product client
        $_request->headers = Dara::merge([
          'host' => $this->_endpoint,
          'x-acs-version' => $params->version,
          'x-acs-action' => $params->action,
          'user-agent' => Utils::getUserAgent($this->_userAgent),
          'x-acs-date' => Utils::getTimestamp(),
          'x-acs-signature-nonce' => Utils::getNonce(),
          'accept' => 'application/json',
        ], $globalHeaders, $extendsHeaders, $request->headers);
        if ($params->style == 'RPC') {
          $headers = $this->getRpcHeaders();
          if (!is_null($headers)) {
            $_request->headers = Dara::merge([
            ], $_request->headers, $headers);
          }

        }

        $signatureAlgorithm = ''.($this->_signatureAlgorithm ? $this->_signatureAlgorithm : 'ACS3-HMAC-SHA256');
        $hashedRequestPayload = Utils::hash(BytesUtil::from('', 'utf-8'), $signatureAlgorithm);
        if (!is_null($request->stream)) {
          $tmp = StreamUtil::readAsBytes($request->stream);
          $hashedRequestPayload = Utils::hash($tmp, $signatureAlgorithm);
          $_request->body = $tmp;
          @$_request->headers['content-type'] = 'application/octet-stream';
        } else {
          if (!is_null($request->body)) {
            if ($params->reqBodyType == 'byte') {
              $byteObj = unpack('C*', $request->body);
              $hashedRequestPayload = Utils::hash($byteObj, $signatureAlgorithm);
              $_request->body = $byteObj;
            } else if ($params->reqBodyType == 'json') {
              $jsonObj = json_encode($request->body, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
              $hashedRequestPayload = Utils::hash(StringUtil::toBytes($jsonObj, 'utf8'), $signatureAlgorithm);
              $_request->body = $jsonObj;
              @$_request->headers['content-type'] = 'application/json; charset=utf-8';
            } else {
              $m = $request->body;
              $formObj = Utils::toForm($m);
              $hashedRequestPayload = Utils::hash(StringUtil::toBytes($formObj, 'utf8'), $signatureAlgorithm);
              $_request->body = $formObj;
              @$_request->headers['content-type'] = 'application/x-www-form-urlencoded';
            }

          }

        }

        @$_request->headers['x-acs-content-sha256'] = bin2hex(BytesUtil::toString($hashedRequestPayload));
        if ($params->authType != 'Anonymous') {
          if (is_null($this->_credential)) {
            throw new ClientException([
              'code' => 'InvalidCredentials',
              'message' => 'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.',
            ]);
          }

          $credentialModel = $this->_credential->getCredential();
          if (!is_null($credentialModel->providerName)) {
            @$_request->headers['x-acs-credentials-provider'] = $credentialModel->providerName;
          }

          $authType = $credentialModel->type;
          if ($authType == 'bearer') {
            $bearerToken = $credentialModel->bearerToken;
            @$_request->headers['x-acs-bearer-token'] = $bearerToken;
            if ($params->style == 'RPC') {
              @$_request->query['SignatureType'] = 'BEARERTOKEN';
            } else {
              @$_request->headers['x-acs-signature-type'] = 'BEARERTOKEN';
            }

          } else {
            $accessKeyId = $credentialModel->accessKeyId;
            $accessKeySecret = $credentialModel->accessKeySecret;
            $securityToken = $credentialModel->securityToken;
            if (!is_null($securityToken) && $securityToken != '') {
              @$_request->headers['x-acs-accesskey-id'] = $accessKeyId;
              @$_request->headers['x-acs-security-token'] = $securityToken;
            }

            @$_request->headers['Authorization'] = Utils::getAuthorization($_request, $signatureAlgorithm, bin2hex(BytesUtil::toString($hashedRequestPayload)), $accessKeyId, $accessKeySecret);
          }

        }

        $_response = Dara::send($_request, $_runtime);
        $_lastRequest = $_request;
        $_lastResponse = $_response;

        if (($_response->statusCode >= 400) && ($_response->statusCode < 600)) {
          $err = [ ];
          if (!is_null(@$_response->headers['content-type']) && @$_response->headers['content-type'] == 'text/xml;charset=utf-8') {
            $_str = StreamUtil::readAsString($_response->body);
            $respMap = XML::parseXml($_str, null);
            $err = @$respMap['Error'];
          } else {
            $_res = StreamUtil::readAsJSON($_response->body);
            $err = $_res;
          }

          $requestId = ''.(@$err['RequestId'] ? @$err['RequestId'] : @$err['requestId']);
          $code = ''.(@$err['Code'] ? @$err['Code'] : @$err['code']);
          if (('' . $code . '' == 'Throttling') || ('' . $code . '' == 'Throttling.User') || ('' . $code . '' == 'Throttling.Api')) {
            throw new ThrottlingException([
              'statusCode' => $_response->statusCode,
              'code' => '' . $code . '',
              'message' => 'code: ' . (string)$_response->statusCode . ', ' . (string)(@$err['Message'] ? @$err['Message'] : @$err['message']) . ' request id: ' . $requestId . '',
              'description' => '' . (string)(@$err['Description'] ? @$err['Description'] : @$err['description']) . '',
              'retryAfter' => Utils::getThrottlingTimeLeft($_response->headers),
              'data' => $err,
              'requestId' => '' . $requestId . '',
            ]);
          } else if (($_response->statusCode >= 400) && ($_response->statusCode < 500)) {
            throw new ClientException([
              'statusCode' => $_response->statusCode,
              'code' => '' . $code . '',
              'message' => 'code: ' . (string)$_response->statusCode . ', ' . (string)(@$err['Message'] ? @$err['Message'] : @$err['message']) . ' request id: ' . $requestId . '',
              'description' => '' . (string)(@$err['Description'] ? @$err['Description'] : @$err['description']) . '',
              'data' => $err,
              'accessDeniedDetail' => $this->getAccessDeniedDetail($err),
              'requestId' => '' . $requestId . '',
            ]);
          } else {
            throw new ServerException([
              'statusCode' => $_response->statusCode,
              'code' => '' . $code . '',
              'message' => 'code: ' . (string)$_response->statusCode . ', ' . (string)(@$err['Message'] ? @$err['Message'] : @$err['message']) . ' request id: ' . $requestId . '',
              'description' => '' . (string)(@$err['Description'] ? @$err['Description'] : @$err['description']) . '',
              'data' => $err,
              'requestId' => '' . $requestId . '',
            ]);
          }

        }

        if ($params->bodyType == 'binary') {
          $resp = [
            'body' => $_response->body,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
          return $resp;
        } else if ($params->bodyType == 'byte') {
          $byt = StreamUtil::readAsBytes($_response->body);
          return [
            'body' => $byt,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
        } else if ($params->bodyType == 'string') {
          $respStr = StreamUtil::readAsString($_response->body);
          return [
            'body' => $respStr,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
        } else if ($params->bodyType == 'json') {
          $obj = StreamUtil::readAsJSON($_response->body);
          $res = $obj;
          return [
            'body' => $res,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
        } else if ($params->bodyType == 'array') {
          $arr = StreamUtil::readAsJSON($_response->body);
          return [
            'body' => $arr,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
        } else {
          $anything = StreamUtil::readAsString($_response->body);
          return [
            'body' => $anything,
            'headers' => $_response->headers,
            'statusCode' => $_response->statusCode,
          ];
        }

      } catch (DaraException $e) {
        $_context = new RetryPolicyContext([
          'retriesAttempted' => $_retriesAttempted,
          'lastRequest' => $_lastRequest,
          'lastResponse' => $_lastResponse,
          'exception' => $e,
        ]);
        continue;
      }
    }

    throw new DaraUnableRetryException($_context);
  }