Rpc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. // This file is auto-generated, don't edit it. Thanks.
  3. namespace AlibabaCloud\Tea\Rpc;
  4. use AlibabaCloud\Credentials\Credential;
  5. use AlibabaCloud\Tea\Exception\TeaError;
  6. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  7. use AlibabaCloud\Tea\Request;
  8. use AlibabaCloud\Tea\Rpc\Rpc\Config;
  9. use AlibabaCloud\Tea\RpcUtils\RpcUtils;
  10. use AlibabaCloud\Tea\Tea;
  11. use AlibabaCloud\Tea\Utils\Utils;
  12. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  13. use Exception;
  14. /**
  15. * This is for RPC SDK.
  16. */
  17. class Rpc
  18. {
  19. protected $_endpoint;
  20. protected $_regionId;
  21. protected $_protocol;
  22. protected $_userAgent;
  23. protected $_endpointRule;
  24. protected $_endpointMap;
  25. protected $_suffix;
  26. protected $_readTimeout;
  27. protected $_connectTimeout;
  28. protected $_httpProxy;
  29. protected $_httpsProxy;
  30. protected $_socks5Proxy;
  31. protected $_socks5NetWork;
  32. protected $_noProxy;
  33. protected $_network;
  34. protected $_productId;
  35. protected $_maxIdleConns;
  36. protected $_endpointType;
  37. protected $_openPlatformEndpoint;
  38. protected $_credential;
  39. /**
  40. * Init client with Config.
  41. *
  42. * @param config config contains the necessary information to create a client
  43. * @param mixed $config
  44. */
  45. public function __construct($config)
  46. {
  47. if (Utils::isUnset($config)) {
  48. throw new TeaError([
  49. 'code' => 'ParameterMissing',
  50. 'message' => "'config' can not be unset",
  51. ]);
  52. }
  53. Utils::validateModel($config);
  54. if (!Utils::empty_($config->accessKeyId) && !Utils::empty_($config->accessKeySecret)) {
  55. if (!Utils::empty_($config->securityToken)) {
  56. $config->type = 'sts';
  57. } else {
  58. $config->type = 'access_key';
  59. }
  60. $credentialConfig = new \AlibabaCloud\Credentials\Credential\Config([
  61. 'accessKeyId' => $config->accessKeyId,
  62. 'type' => $config->type,
  63. 'accessKeySecret' => $config->accessKeySecret,
  64. 'securityToken' => $config->securityToken,
  65. ]);
  66. $this->_credential = new Credential($credentialConfig);
  67. } elseif (!Utils::isUnset($config->credential)) {
  68. $this->_credential = $config->credential;
  69. } else {
  70. throw new TeaError([
  71. 'code' => 'ParameterMissing',
  72. 'message' => "'accessKeyId' and 'accessKeySecret' or 'credential' can not be unset",
  73. ]);
  74. }
  75. $this->_network = $config->network;
  76. $this->_suffix = $config->suffix;
  77. $this->_endpoint = $config->endpoint;
  78. $this->_protocol = $config->protocol;
  79. $this->_regionId = $config->regionId;
  80. $this->_userAgent = $config->userAgent;
  81. $this->_readTimeout = $config->readTimeout;
  82. $this->_connectTimeout = $config->connectTimeout;
  83. $this->_httpProxy = $config->httpProxy;
  84. $this->_httpsProxy = $config->httpsProxy;
  85. $this->_noProxy = $config->noProxy;
  86. $this->_socks5Proxy = $config->socks5Proxy;
  87. $this->_socks5NetWork = $config->socks5NetWork;
  88. $this->_maxIdleConns = $config->maxIdleConns;
  89. $this->_endpointType = $config->endpointType;
  90. $this->_openPlatformEndpoint = $config->openPlatformEndpoint;
  91. }
  92. /**
  93. * Encapsulate the request and invoke the network.
  94. *
  95. * @param string $action api name
  96. * @param string $protocol http or https
  97. * @param string $method e.g. GET
  98. * @param string $version product version
  99. * @param string $authType when authType is Anonymous, the signature will not be calculate
  100. * @param mixed[] $query which contains request params
  101. * @param mixed[] $body content of request
  102. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  103. *
  104. * @throws TeaError
  105. * @throws Exception
  106. * @throws TeaUnableRetryError
  107. *
  108. * @return array the response
  109. */
  110. public function doRequest($action, $protocol, $method, $version, $authType, $query, $body, $runtime)
  111. {
  112. $runtime->validate();
  113. $_runtime = [
  114. 'timeouted' => 'retry',
  115. 'readTimeout' => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  116. 'connectTimeout' => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  117. 'httpProxy' => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  118. 'httpsProxy' => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  119. 'noProxy' => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  120. 'maxIdleConns' => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  121. 'retry' => [
  122. 'retryable' => $runtime->autoretry,
  123. 'maxAttempts' => Utils::defaultNumber($runtime->maxAttempts, 3),
  124. ],
  125. 'backoff' => [
  126. 'policy' => Utils::defaultString($runtime->backoffPolicy, 'no'),
  127. 'period' => Utils::defaultNumber($runtime->backoffPeriod, 1),
  128. ],
  129. 'ignoreSSL' => $runtime->ignoreSSL,
  130. ];
  131. $_lastRequest = null;
  132. $_lastException = null;
  133. $_now = time();
  134. $_retryTimes = 0;
  135. while (Tea::allowRetry(@$_runtime['retry'], $_retryTimes, $_now)) {
  136. if ($_retryTimes > 0) {
  137. $_backoffTime = Tea::getBackoffTime(@$_runtime['backoff'], $_retryTimes);
  138. if ($_backoffTime > 0) {
  139. Tea::sleep($_backoffTime);
  140. }
  141. }
  142. $_retryTimes = $_retryTimes + 1;
  143. try {
  144. $_request = new Request();
  145. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  146. $_request->method = $method;
  147. $_request->pathname = '/';
  148. $_request->query = RpcUtils::query(Tea::merge([
  149. 'Action' => $action,
  150. 'Format' => 'json',
  151. 'Timestamp' => RpcUtils::getTimestamp(),
  152. 'Version' => $version,
  153. 'SignatureNonce' => Utils::getNonce(),
  154. ], $query));
  155. // endpoint is setted in product client
  156. $_request->headers = [
  157. 'x-acs-version' => $version,
  158. 'x-acs-action' => $action,
  159. 'host' => $this->_endpoint,
  160. 'user-agent' => $this->getUserAgent(),
  161. ];
  162. if (!Utils::isUnset($body)) {
  163. $tmp = Utils::anyifyMapValue(RpcUtils::query($body));
  164. $_request->body = Utils::toFormString($tmp);
  165. $_request->headers['content-type'] = 'application/x-www-form-urlencoded';
  166. }
  167. if (!Utils::equalString($authType, 'Anonymous')) {
  168. $accessKeyId = $this->getAccessKeyId();
  169. $accessKeySecret = $this->getAccessKeySecret();
  170. $securityToken = $this->getSecurityToken();
  171. if (!Utils::empty_($securityToken)) {
  172. $_request->query['SecurityToken'] = $securityToken;
  173. }
  174. $_request->query['SignatureMethod'] = 'HMAC-SHA1';
  175. $_request->query['SignatureVersion'] = '1.0';
  176. $_request->query['AccessKeyId'] = $accessKeyId;
  177. $signedParam = Tea::merge($_request->query, RpcUtils::query($body));
  178. $_request->query['Signature'] = RpcUtils::getSignatureV1($signedParam, $_request->method, $accessKeySecret);
  179. }
  180. $_lastRequest = $_request;
  181. $_response = Tea::send($_request, $_runtime);
  182. $obj = Utils::readAsJSON($_response->body);
  183. $res = Utils::assertAsMap($obj);
  184. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  185. throw new TeaError([
  186. 'code' => '' . (string) (self::defaultAny(@$res['Code'], @$res['code'])) . '',
  187. 'message' => 'code: ' . (string) ($_response->statusCode) . ', ' . (string) (self::defaultAny(@$res['Message'], @$res['message'])) . ' request id: ' . (string) (self::defaultAny(@$res['RequestId'], @$res['requestId'])) . '',
  188. 'data' => $res,
  189. ]);
  190. }
  191. return $res;
  192. } catch (Exception $e) {
  193. if (!($e instanceof TeaError)) {
  194. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  195. }
  196. if (Tea::isRetryable($e)) {
  197. $_lastException = $e;
  198. continue;
  199. }
  200. throw $e;
  201. }
  202. }
  203. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  204. }
  205. /**
  206. * Get user agent.
  207. *
  208. * @return string user agent
  209. */
  210. public function getUserAgent()
  211. {
  212. return Utils::getUserAgent($this->_userAgent);
  213. }
  214. /**
  215. * Get accesskey id by using credential.
  216. *
  217. * @return string accesskey id
  218. */
  219. public function getAccessKeyId()
  220. {
  221. if (Utils::isUnset($this->_credential)) {
  222. return '';
  223. }
  224. return $this->_credential->getAccessKeyId();
  225. }
  226. /**
  227. * Get accesskey secret by using credential.
  228. *
  229. * @return string accesskey secret
  230. */
  231. public function getAccessKeySecret()
  232. {
  233. if (Utils::isUnset($this->_credential)) {
  234. return '';
  235. }
  236. return $this->_credential->getAccessKeySecret();
  237. }
  238. /**
  239. * Get security token by using credential.
  240. *
  241. * @return string security token
  242. */
  243. public function getSecurityToken()
  244. {
  245. if (Utils::isUnset($this->_credential)) {
  246. return '';
  247. }
  248. return $this->_credential->getSecurityToken();
  249. }
  250. /**
  251. * If the endpointRule and config.endpoint are empty, throw error.
  252. *
  253. * @param Config $config config contains the necessary information to create a client
  254. *
  255. * @throws TeaError
  256. */
  257. public function checkConfig($config)
  258. {
  259. if (Utils::empty_($this->_endpointRule) && Utils::empty_($config->endpoint)) {
  260. throw new TeaError([
  261. 'code' => 'ParameterMissing',
  262. 'message' => "'config.endpoint' can not be empty",
  263. ]);
  264. }
  265. }
  266. /**
  267. * If inputValue is not null, return it or return defaultValue.
  268. *
  269. * @param mixed $inputValue users input value
  270. * @param mixed $defaultValue default value
  271. *
  272. * @return any the final result
  273. */
  274. public static function defaultAny($inputValue, $defaultValue)
  275. {
  276. if (Utils::isUnset($inputValue)) {
  277. return $defaultValue;
  278. }
  279. return $inputValue;
  280. }
  281. }