OpenApiClient.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. <?php
  2. // This file is auto-generated, don't edit it. Thanks.
  3. namespace Darabonba\OpenApi;
  4. use AlibabaCloud\Credentials\Credential;
  5. use AlibabaCloud\Credentials\Credential\Config;
  6. use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
  7. use AlibabaCloud\Tea\Exception\TeaError;
  8. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  9. use AlibabaCloud\Tea\Request;
  10. use AlibabaCloud\Tea\Tea;
  11. use AlibabaCloud\Tea\Utils\Utils;
  12. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  13. use Darabonba\OpenApi\Models\OpenApiRequest;
  14. use Darabonba\OpenApi\Models\Params;
  15. use Exception;
  16. /**
  17. * This is for OpenApi SDK.
  18. */
  19. class OpenApiClient
  20. {
  21. protected $_endpoint;
  22. protected $_regionId;
  23. protected $_protocol;
  24. protected $_userAgent;
  25. protected $_endpointRule;
  26. protected $_endpointMap;
  27. protected $_suffix;
  28. protected $_readTimeout;
  29. protected $_connectTimeout;
  30. protected $_httpProxy;
  31. protected $_httpsProxy;
  32. protected $_socks5Proxy;
  33. protected $_socks5NetWork;
  34. protected $_noProxy;
  35. protected $_network;
  36. protected $_productId;
  37. protected $_maxIdleConns;
  38. protected $_endpointType;
  39. protected $_openPlatformEndpoint;
  40. protected $_credential;
  41. protected $_signatureAlgorithm;
  42. protected $_headers;
  43. /**
  44. * Init client with Config.
  45. *
  46. * @param config config contains the necessary information to create a client
  47. * @param mixed $config
  48. */
  49. public function __construct($config)
  50. {
  51. if (Utils::isUnset($config)) {
  52. throw new TeaError([
  53. 'code' => 'ParameterMissing',
  54. 'message' => "'config' can not be unset",
  55. ]);
  56. }
  57. if (!Utils::empty_($config->accessKeyId) && !Utils::empty_($config->accessKeySecret)) {
  58. if (!Utils::empty_($config->securityToken)) {
  59. $config->type = 'sts';
  60. } else {
  61. $config->type = 'access_key';
  62. }
  63. $credentialConfig = new Config([
  64. 'accessKeyId' => $config->accessKeyId,
  65. 'type' => $config->type,
  66. 'accessKeySecret' => $config->accessKeySecret,
  67. 'securityToken' => $config->securityToken,
  68. ]);
  69. $this->_credential = new Credential($credentialConfig);
  70. } elseif (!Utils::isUnset($config->credential)) {
  71. $this->_credential = $config->credential;
  72. }
  73. $this->_endpoint = $config->endpoint;
  74. $this->_endpointType = $config->endpointType;
  75. $this->_protocol = $config->protocol;
  76. $this->_regionId = $config->regionId;
  77. $this->_userAgent = $config->userAgent;
  78. $this->_readTimeout = $config->readTimeout;
  79. $this->_connectTimeout = $config->connectTimeout;
  80. $this->_httpProxy = $config->httpProxy;
  81. $this->_httpsProxy = $config->httpsProxy;
  82. $this->_noProxy = $config->noProxy;
  83. $this->_socks5Proxy = $config->socks5Proxy;
  84. $this->_socks5NetWork = $config->socks5NetWork;
  85. $this->_maxIdleConns = $config->maxIdleConns;
  86. $this->_signatureAlgorithm = $config->signatureAlgorithm;
  87. }
  88. /**
  89. * Encapsulate the request and invoke the network.
  90. *
  91. * @param string $action api name
  92. * @param string $version product version
  93. * @param string $protocol http or https
  94. * @param string $method e.g. GET
  95. * @param string $authType authorization type e.g. AK
  96. * @param string $bodyType response body type e.g. String
  97. * @param OpenApiRequest $request object of OpenApiRequest
  98. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  99. *
  100. * @throws TeaError
  101. * @throws Exception
  102. * @throws TeaUnableRetryError
  103. *
  104. * @return array the response
  105. */
  106. public function doRPCRequest($action, $version, $protocol, $method, $authType, $bodyType, $request, $runtime)
  107. {
  108. $request->validate();
  109. $runtime->validate();
  110. $_runtime = [
  111. 'timeouted' => 'retry',
  112. 'readTimeout' => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  113. 'connectTimeout' => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  114. 'httpProxy' => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  115. 'httpsProxy' => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  116. 'noProxy' => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  117. 'maxIdleConns' => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  118. 'retry' => [
  119. 'retryable' => $runtime->autoretry,
  120. 'maxAttempts' => Utils::defaultNumber($runtime->maxAttempts, 3),
  121. ],
  122. 'backoff' => [
  123. 'policy' => Utils::defaultString($runtime->backoffPolicy, 'no'),
  124. 'period' => Utils::defaultNumber($runtime->backoffPeriod, 1),
  125. ],
  126. 'ignoreSSL' => $runtime->ignoreSSL,
  127. ];
  128. $_lastRequest = null;
  129. $_lastException = null;
  130. $_now = time();
  131. $_retryTimes = 0;
  132. while (Tea::allowRetry(@$_runtime['retry'], $_retryTimes, $_now)) {
  133. if ($_retryTimes > 0) {
  134. $_backoffTime = Tea::getBackoffTime(@$_runtime['backoff'], $_retryTimes);
  135. if ($_backoffTime > 0) {
  136. Tea::sleep($_backoffTime);
  137. }
  138. }
  139. $_retryTimes = $_retryTimes + 1;
  140. try {
  141. $_request = new Request();
  142. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  143. $_request->method = $method;
  144. $_request->pathname = '/';
  145. $_request->query = Tea::merge([
  146. 'Action' => $action,
  147. 'Format' => 'json',
  148. 'Version' => $version,
  149. 'Timestamp' => OpenApiUtilClient::getTimestamp(),
  150. 'SignatureNonce' => Utils::getNonce(),
  151. ], $request->query);
  152. $headers = $this->getRpcHeaders();
  153. if (Utils::isUnset($headers)) {
  154. // endpoint is setted in product client
  155. $_request->headers = [
  156. 'host' => $this->_endpoint,
  157. 'x-acs-version' => $version,
  158. 'x-acs-action' => $action,
  159. 'user-agent' => $this->getUserAgent(),
  160. ];
  161. } else {
  162. $_request->headers = Tea::merge([
  163. 'host' => $this->_endpoint,
  164. 'x-acs-version' => $version,
  165. 'x-acs-action' => $action,
  166. 'user-agent' => $this->getUserAgent(),
  167. ], $headers);
  168. }
  169. if (!Utils::isUnset($request->body)) {
  170. $m = Utils::assertAsMap($request->body);
  171. $tmp = Utils::anyifyMapValue(OpenApiUtilClient::query($m));
  172. $_request->body = Utils::toFormString($tmp);
  173. $_request->headers['content-type'] = 'application/x-www-form-urlencoded';
  174. }
  175. if (!Utils::equalString($authType, 'Anonymous')) {
  176. $accessKeyId = $this->getAccessKeyId();
  177. $accessKeySecret = $this->getAccessKeySecret();
  178. $securityToken = $this->getSecurityToken();
  179. if (!Utils::empty_($securityToken)) {
  180. $_request->query['SecurityToken'] = $securityToken;
  181. }
  182. $_request->query['SignatureMethod'] = 'HMAC-SHA1';
  183. $_request->query['SignatureVersion'] = '1.0';
  184. $_request->query['AccessKeyId'] = $accessKeyId;
  185. $t = null;
  186. if (!Utils::isUnset($request->body)) {
  187. $t = Utils::assertAsMap($request->body);
  188. }
  189. $signedParam = Tea::merge($_request->query, OpenApiUtilClient::query($t));
  190. $_request->query['Signature'] = OpenApiUtilClient::getRPCSignature($signedParam, $_request->method, $accessKeySecret);
  191. }
  192. $_lastRequest = $_request;
  193. $_response = Tea::send($_request, $_runtime);
  194. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  195. $_res = Utils::readAsJSON($_response->body);
  196. $err = Utils::assertAsMap($_res);
  197. $requestId = self::defaultAny(@$err['RequestId'], @$err['requestId']);
  198. throw new TeaError([
  199. 'code' => '' . (string) (self::defaultAny(@$err['Code'], @$err['code'])) . '',
  200. 'message' => 'code: ' . (string) ($_response->statusCode) . ', ' . (string) (self::defaultAny(@$err['Message'], @$err['message'])) . ' request id: ' . (string) ($requestId) . '',
  201. 'data' => $err,
  202. ]);
  203. }
  204. if (Utils::equalString($bodyType, 'binary')) {
  205. $resp = [
  206. 'body' => $_response->body,
  207. 'headers' => $_response->headers,
  208. ];
  209. return $resp;
  210. }
  211. if (Utils::equalString($bodyType, 'byte')) {
  212. $byt = Utils::readAsBytes($_response->body);
  213. return [
  214. 'body' => $byt,
  215. 'headers' => $_response->headers,
  216. ];
  217. }
  218. if (Utils::equalString($bodyType, 'string')) {
  219. $str = Utils::readAsString($_response->body);
  220. return [
  221. 'body' => $str,
  222. 'headers' => $_response->headers,
  223. ];
  224. }
  225. if (Utils::equalString($bodyType, 'json')) {
  226. $obj = Utils::readAsJSON($_response->body);
  227. $res = Utils::assertAsMap($obj);
  228. return [
  229. 'body' => $res,
  230. 'headers' => $_response->headers,
  231. ];
  232. }
  233. if (Utils::equalString($bodyType, 'array')) {
  234. $arr = Utils::readAsJSON($_response->body);
  235. return [
  236. 'body' => $arr,
  237. 'headers' => $_response->headers,
  238. ];
  239. }
  240. return [
  241. 'headers' => $_response->headers,
  242. ];
  243. } catch (Exception $e) {
  244. if (!($e instanceof TeaError)) {
  245. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  246. }
  247. if (Tea::isRetryable($e)) {
  248. $_lastException = $e;
  249. continue;
  250. }
  251. throw $e;
  252. }
  253. }
  254. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  255. }
  256. /**
  257. * Encapsulate the request and invoke the network.
  258. *
  259. * @param string $action api name
  260. * @param string $version product version
  261. * @param string $protocol http or https
  262. * @param string $method e.g. GET
  263. * @param string $authType authorization type e.g. AK
  264. * @param string $pathname pathname of every api
  265. * @param string $bodyType response body type e.g. String
  266. * @param OpenApiRequest $request object of OpenApiRequest
  267. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  268. *
  269. * @throws TeaError
  270. * @throws Exception
  271. * @throws TeaUnableRetryError
  272. *
  273. * @return array the response
  274. */
  275. public function doROARequest($action, $version, $protocol, $method, $authType, $pathname, $bodyType, $request, $runtime)
  276. {
  277. $request->validate();
  278. $runtime->validate();
  279. $_runtime = [
  280. 'timeouted' => 'retry',
  281. 'readTimeout' => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  282. 'connectTimeout' => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  283. 'httpProxy' => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  284. 'httpsProxy' => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  285. 'noProxy' => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  286. 'maxIdleConns' => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  287. 'retry' => [
  288. 'retryable' => $runtime->autoretry,
  289. 'maxAttempts' => Utils::defaultNumber($runtime->maxAttempts, 3),
  290. ],
  291. 'backoff' => [
  292. 'policy' => Utils::defaultString($runtime->backoffPolicy, 'no'),
  293. 'period' => Utils::defaultNumber($runtime->backoffPeriod, 1),
  294. ],
  295. 'ignoreSSL' => $runtime->ignoreSSL,
  296. ];
  297. $_lastRequest = null;
  298. $_lastException = null;
  299. $_now = time();
  300. $_retryTimes = 0;
  301. while (Tea::allowRetry(@$_runtime['retry'], $_retryTimes, $_now)) {
  302. if ($_retryTimes > 0) {
  303. $_backoffTime = Tea::getBackoffTime(@$_runtime['backoff'], $_retryTimes);
  304. if ($_backoffTime > 0) {
  305. Tea::sleep($_backoffTime);
  306. }
  307. }
  308. $_retryTimes = $_retryTimes + 1;
  309. try {
  310. $_request = new Request();
  311. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  312. $_request->method = $method;
  313. $_request->pathname = $pathname;
  314. $_request->headers = Tea::merge([
  315. 'date' => Utils::getDateUTCString(),
  316. 'host' => $this->_endpoint,
  317. 'accept' => 'application/json',
  318. 'x-acs-signature-nonce' => Utils::getNonce(),
  319. 'x-acs-signature-method' => 'HMAC-SHA1',
  320. 'x-acs-signature-version' => '1.0',
  321. 'x-acs-version' => $version,
  322. 'x-acs-action' => $action,
  323. 'user-agent' => Utils::getUserAgent($this->_userAgent),
  324. ], $request->headers);
  325. if (!Utils::isUnset($request->body)) {
  326. $_request->body = Utils::toJSONString($request->body);
  327. $_request->headers['content-type'] = 'application/json; charset=utf-8';
  328. }
  329. if (!Utils::isUnset($request->query)) {
  330. $_request->query = $request->query;
  331. }
  332. if (!Utils::equalString($authType, 'Anonymous')) {
  333. $accessKeyId = $this->getAccessKeyId();
  334. $accessKeySecret = $this->getAccessKeySecret();
  335. $securityToken = $this->getSecurityToken();
  336. if (!Utils::empty_($securityToken)) {
  337. $_request->headers['x-acs-accesskey-id'] = $accessKeyId;
  338. $_request->headers['x-acs-security-token'] = $securityToken;
  339. }
  340. $stringToSign = OpenApiUtilClient::getStringToSign($_request);
  341. $_request->headers['authorization'] = 'acs ' . $accessKeyId . ':' . OpenApiUtilClient::getROASignature($stringToSign, $accessKeySecret) . '';
  342. }
  343. $_lastRequest = $_request;
  344. $_response = Tea::send($_request, $_runtime);
  345. if (Utils::equalNumber($_response->statusCode, 204)) {
  346. return [
  347. 'headers' => $_response->headers,
  348. ];
  349. }
  350. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  351. $_res = Utils::readAsJSON($_response->body);
  352. $err = Utils::assertAsMap($_res);
  353. $requestId = self::defaultAny(@$err['RequestId'], @$err['requestId']);
  354. $requestId = self::defaultAny($requestId, @$err['requestid']);
  355. throw new TeaError([
  356. 'code' => '' . (string) (self::defaultAny(@$err['Code'], @$err['code'])) . '',
  357. 'message' => 'code: ' . (string) ($_response->statusCode) . ', ' . (string) (self::defaultAny(@$err['Message'], @$err['message'])) . ' request id: ' . (string) ($requestId) . '',
  358. 'data' => $err,
  359. ]);
  360. }
  361. if (Utils::equalString($bodyType, 'binary')) {
  362. $resp = [
  363. 'body' => $_response->body,
  364. 'headers' => $_response->headers,
  365. ];
  366. return $resp;
  367. }
  368. if (Utils::equalString($bodyType, 'byte')) {
  369. $byt = Utils::readAsBytes($_response->body);
  370. return [
  371. 'body' => $byt,
  372. 'headers' => $_response->headers,
  373. ];
  374. }
  375. if (Utils::equalString($bodyType, 'string')) {
  376. $str = Utils::readAsString($_response->body);
  377. return [
  378. 'body' => $str,
  379. 'headers' => $_response->headers,
  380. ];
  381. }
  382. if (Utils::equalString($bodyType, 'json')) {
  383. $obj = Utils::readAsJSON($_response->body);
  384. $res = Utils::assertAsMap($obj);
  385. return [
  386. 'body' => $res,
  387. 'headers' => $_response->headers,
  388. ];
  389. }
  390. if (Utils::equalString($bodyType, 'array')) {
  391. $arr = Utils::readAsJSON($_response->body);
  392. return [
  393. 'body' => $arr,
  394. 'headers' => $_response->headers,
  395. ];
  396. }
  397. return [
  398. 'headers' => $_response->headers,
  399. ];
  400. } catch (Exception $e) {
  401. if (!($e instanceof TeaError)) {
  402. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  403. }
  404. if (Tea::isRetryable($e)) {
  405. $_lastException = $e;
  406. continue;
  407. }
  408. throw $e;
  409. }
  410. }
  411. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  412. }
  413. /**
  414. * Encapsulate the request and invoke the network with form body.
  415. *
  416. * @param string $action api name
  417. * @param string $version product version
  418. * @param string $protocol http or https
  419. * @param string $method e.g. GET
  420. * @param string $authType authorization type e.g. AK
  421. * @param string $pathname pathname of every api
  422. * @param string $bodyType response body type e.g. String
  423. * @param OpenApiRequest $request object of OpenApiRequest
  424. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  425. *
  426. * @throws TeaError
  427. * @throws Exception
  428. * @throws TeaUnableRetryError
  429. *
  430. * @return array the response
  431. */
  432. public function doROARequestWithForm($action, $version, $protocol, $method, $authType, $pathname, $bodyType, $request, $runtime)
  433. {
  434. $request->validate();
  435. $runtime->validate();
  436. $_runtime = [
  437. 'timeouted' => 'retry',
  438. 'readTimeout' => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  439. 'connectTimeout' => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  440. 'httpProxy' => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  441. 'httpsProxy' => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  442. 'noProxy' => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  443. 'maxIdleConns' => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  444. 'retry' => [
  445. 'retryable' => $runtime->autoretry,
  446. 'maxAttempts' => Utils::defaultNumber($runtime->maxAttempts, 3),
  447. ],
  448. 'backoff' => [
  449. 'policy' => Utils::defaultString($runtime->backoffPolicy, 'no'),
  450. 'period' => Utils::defaultNumber($runtime->backoffPeriod, 1),
  451. ],
  452. 'ignoreSSL' => $runtime->ignoreSSL,
  453. ];
  454. $_lastRequest = null;
  455. $_lastException = null;
  456. $_now = time();
  457. $_retryTimes = 0;
  458. while (Tea::allowRetry(@$_runtime['retry'], $_retryTimes, $_now)) {
  459. if ($_retryTimes > 0) {
  460. $_backoffTime = Tea::getBackoffTime(@$_runtime['backoff'], $_retryTimes);
  461. if ($_backoffTime > 0) {
  462. Tea::sleep($_backoffTime);
  463. }
  464. }
  465. $_retryTimes = $_retryTimes + 1;
  466. try {
  467. $_request = new Request();
  468. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  469. $_request->method = $method;
  470. $_request->pathname = $pathname;
  471. $_request->headers = Tea::merge([
  472. 'date' => Utils::getDateUTCString(),
  473. 'host' => $this->_endpoint,
  474. 'accept' => 'application/json',
  475. 'x-acs-signature-nonce' => Utils::getNonce(),
  476. 'x-acs-signature-method' => 'HMAC-SHA1',
  477. 'x-acs-signature-version' => '1.0',
  478. 'x-acs-version' => $version,
  479. 'x-acs-action' => $action,
  480. 'user-agent' => Utils::getUserAgent($this->_userAgent),
  481. ], $request->headers);
  482. if (!Utils::isUnset($request->body)) {
  483. $m = Utils::assertAsMap($request->body);
  484. $_request->body = OpenApiUtilClient::toForm($m);
  485. $_request->headers['content-type'] = 'application/x-www-form-urlencoded';
  486. }
  487. if (!Utils::isUnset($request->query)) {
  488. $_request->query = $request->query;
  489. }
  490. if (!Utils::equalString($authType, 'Anonymous')) {
  491. $accessKeyId = $this->getAccessKeyId();
  492. $accessKeySecret = $this->getAccessKeySecret();
  493. $securityToken = $this->getSecurityToken();
  494. if (!Utils::empty_($securityToken)) {
  495. $_request->headers['x-acs-accesskey-id'] = $accessKeyId;
  496. $_request->headers['x-acs-security-token'] = $securityToken;
  497. }
  498. $stringToSign = OpenApiUtilClient::getStringToSign($_request);
  499. $_request->headers['authorization'] = 'acs ' . $accessKeyId . ':' . OpenApiUtilClient::getROASignature($stringToSign, $accessKeySecret) . '';
  500. }
  501. $_lastRequest = $_request;
  502. $_response = Tea::send($_request, $_runtime);
  503. if (Utils::equalNumber($_response->statusCode, 204)) {
  504. return [
  505. 'headers' => $_response->headers,
  506. ];
  507. }
  508. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  509. $_res = Utils::readAsJSON($_response->body);
  510. $err = Utils::assertAsMap($_res);
  511. throw new TeaError([
  512. 'code' => '' . (string) (self::defaultAny(@$err['Code'], @$err['code'])) . '',
  513. 'message' => 'code: ' . (string) ($_response->statusCode) . ', ' . (string) (self::defaultAny(@$err['Message'], @$err['message'])) . ' request id: ' . (string) (self::defaultAny(@$err['RequestId'], @$err['requestId'])) . '',
  514. 'data' => $err,
  515. ]);
  516. }
  517. if (Utils::equalString($bodyType, 'binary')) {
  518. $resp = [
  519. 'body' => $_response->body,
  520. 'headers' => $_response->headers,
  521. ];
  522. return $resp;
  523. }
  524. if (Utils::equalString($bodyType, 'byte')) {
  525. $byt = Utils::readAsBytes($_response->body);
  526. return [
  527. 'body' => $byt,
  528. 'headers' => $_response->headers,
  529. ];
  530. }
  531. if (Utils::equalString($bodyType, 'string')) {
  532. $str = Utils::readAsString($_response->body);
  533. return [
  534. 'body' => $str,
  535. 'headers' => $_response->headers,
  536. ];
  537. }
  538. if (Utils::equalString($bodyType, 'json')) {
  539. $obj = Utils::readAsJSON($_response->body);
  540. $res = Utils::assertAsMap($obj);
  541. return [
  542. 'body' => $res,
  543. 'headers' => $_response->headers,
  544. ];
  545. }
  546. if (Utils::equalString($bodyType, 'array')) {
  547. $arr = Utils::readAsJSON($_response->body);
  548. return [
  549. 'body' => $arr,
  550. 'headers' => $_response->headers,
  551. ];
  552. }
  553. return [
  554. 'headers' => $_response->headers,
  555. ];
  556. } catch (Exception $e) {
  557. if (!($e instanceof TeaError)) {
  558. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  559. }
  560. if (Tea::isRetryable($e)) {
  561. $_lastException = $e;
  562. continue;
  563. }
  564. throw $e;
  565. }
  566. }
  567. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  568. }
  569. /**
  570. * Encapsulate the request and invoke the network.
  571. *
  572. * @param Params $params
  573. * @param OpenApiRequest $request object of OpenApiRequest
  574. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  575. *
  576. * @throws TeaError
  577. * @throws Exception
  578. * @throws TeaUnableRetryError
  579. *
  580. * @return array the response
  581. */
  582. public function doRequest($params, $request, $runtime)
  583. {
  584. $params->validate();
  585. $request->validate();
  586. $runtime->validate();
  587. $_runtime = [
  588. 'timeouted' => 'retry',
  589. 'readTimeout' => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  590. 'connectTimeout' => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  591. 'httpProxy' => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  592. 'httpsProxy' => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  593. 'noProxy' => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  594. 'maxIdleConns' => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  595. 'retry' => [
  596. 'retryable' => $runtime->autoretry,
  597. 'maxAttempts' => Utils::defaultNumber($runtime->maxAttempts, 3),
  598. ],
  599. 'backoff' => [
  600. 'policy' => Utils::defaultString($runtime->backoffPolicy, 'no'),
  601. 'period' => Utils::defaultNumber($runtime->backoffPeriod, 1),
  602. ],
  603. 'ignoreSSL' => $runtime->ignoreSSL,
  604. ];
  605. $_lastRequest = null;
  606. $_lastException = null;
  607. $_now = time();
  608. $_retryTimes = 0;
  609. while (Tea::allowRetry(@$_runtime['retry'], $_retryTimes, $_now)) {
  610. if ($_retryTimes > 0) {
  611. $_backoffTime = Tea::getBackoffTime(@$_runtime['backoff'], $_retryTimes);
  612. if ($_backoffTime > 0) {
  613. Tea::sleep($_backoffTime);
  614. }
  615. }
  616. $_retryTimes = $_retryTimes + 1;
  617. try {
  618. $_request = new Request();
  619. $_request->protocol = Utils::defaultString($this->_protocol, $params->protocol);
  620. $_request->method = $params->method;
  621. $_request->pathname = $params->pathname;
  622. $_request->query = $request->query;
  623. // endpoint is setted in product client
  624. $_request->headers = Tea::merge([
  625. 'host' => $this->_endpoint,
  626. 'x-acs-version' => $params->version,
  627. 'x-acs-action' => $params->action,
  628. 'user-agent' => $this->getUserAgent(),
  629. 'x-acs-date' => OpenApiUtilClient::getTimestamp(),
  630. 'x-acs-signature-nonce' => Utils::getNonce(),
  631. 'accept' => 'application/json',
  632. ], $request->headers);
  633. $signatureAlgorithm = Utils::defaultString($this->_signatureAlgorithm, 'ACS3-HMAC-SHA256');
  634. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes(''), $signatureAlgorithm));
  635. if (!Utils::isUnset($request->body)) {
  636. if (Utils::equalString($params->reqBodyType, 'json')) {
  637. $jsonObj = Utils::toJSONString($request->body);
  638. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes($jsonObj), $signatureAlgorithm));
  639. $_request->body = $jsonObj;
  640. } else {
  641. $m = Utils::assertAsMap($request->body);
  642. $formObj = OpenApiUtilClient::toForm($m);
  643. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes($formObj), $signatureAlgorithm));
  644. $_request->body = $formObj;
  645. $_request->headers['content-type'] = 'application/x-www-form-urlencoded';
  646. }
  647. }
  648. if (!Utils::isUnset($request->stream)) {
  649. $tmp = Utils::readAsBytes($request->stream);
  650. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash($tmp, $signatureAlgorithm));
  651. $_request->body = $tmp;
  652. }
  653. $_request->headers['x-acs-content-sha256'] = $hashedRequestPayload;
  654. if (!Utils::equalString($params->authType, 'Anonymous')) {
  655. $accessKeyId = $this->getAccessKeyId();
  656. $accessKeySecret = $this->getAccessKeySecret();
  657. $securityToken = $this->getSecurityToken();
  658. if (!Utils::empty_($securityToken)) {
  659. $_request->headers['x-acs-security-token'] = $securityToken;
  660. }
  661. $_request->headers['Authorization'] = OpenApiUtilClient::getAuthorization($_request, $signatureAlgorithm, $hashedRequestPayload, $accessKeyId, $accessKeySecret);
  662. }
  663. $_lastRequest = $_request;
  664. $_response = Tea::send($_request, $_runtime);
  665. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  666. $_res = Utils::readAsJSON($_response->body);
  667. $err = Utils::assertAsMap($_res);
  668. throw new TeaError([
  669. 'code' => '' . (string) (self::defaultAny(@$err['Code'], @$err['code'])) . '',
  670. 'message' => 'code: ' . (string) ($_response->statusCode) . ', ' . (string) (self::defaultAny(@$err['Message'], @$err['message'])) . ' request id: ' . (string) (self::defaultAny(@$err['RequestId'], @$err['requestId'])) . '',
  671. 'data' => $err,
  672. ]);
  673. }
  674. if (Utils::equalString($params->bodyType, 'binary')) {
  675. $resp = [
  676. 'body' => $_response->body,
  677. 'headers' => $_response->headers,
  678. ];
  679. return $resp;
  680. }
  681. if (Utils::equalString($params->bodyType, 'byte')) {
  682. $byt = Utils::readAsBytes($_response->body);
  683. return [
  684. 'body' => $byt,
  685. 'headers' => $_response->headers,
  686. ];
  687. }
  688. if (Utils::equalString($params->bodyType, 'string')) {
  689. $str = Utils::readAsString($_response->body);
  690. return [
  691. 'body' => $str,
  692. 'headers' => $_response->headers,
  693. ];
  694. }
  695. if (Utils::equalString($params->bodyType, 'json')) {
  696. $obj = Utils::readAsJSON($_response->body);
  697. $res = Utils::assertAsMap($obj);
  698. return [
  699. 'body' => $res,
  700. 'headers' => $_response->headers,
  701. ];
  702. }
  703. if (Utils::equalString($params->bodyType, 'array')) {
  704. $arr = Utils::readAsJSON($_response->body);
  705. return [
  706. 'body' => $arr,
  707. 'headers' => $_response->headers,
  708. ];
  709. }
  710. return [
  711. 'headers' => $_response->headers,
  712. ];
  713. } catch (Exception $e) {
  714. if (!($e instanceof TeaError)) {
  715. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  716. }
  717. if (Tea::isRetryable($e)) {
  718. $_lastException = $e;
  719. continue;
  720. }
  721. throw $e;
  722. }
  723. }
  724. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  725. }
  726. /**
  727. * @param Params $params
  728. * @param OpenApiRequest $request
  729. * @param RuntimeOptions $runtime
  730. *
  731. * @throws TeaError
  732. *
  733. * @return array
  734. */
  735. public function callApi($params, $request, $runtime)
  736. {
  737. if (Utils::isUnset($params)) {
  738. throw new TeaError([
  739. 'code' => 'ParameterMissing',
  740. 'message' => "'params' can not be unset",
  741. ]);
  742. }
  743. if (Utils::isUnset($this->_signatureAlgorithm) || !Utils::equalString($this->_signatureAlgorithm, 'v2')) {
  744. return $this->doRequest($params, $request, $runtime);
  745. }
  746. if (Utils::equalString($params->style, 'ROA') && Utils::equalString($params->reqBodyType, 'json')) {
  747. return $this->doROARequest($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->pathname, $params->bodyType, $request, $runtime);
  748. }
  749. if (Utils::equalString($params->style, 'ROA')) {
  750. return $this->doROARequestWithForm($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->pathname, $params->bodyType, $request, $runtime);
  751. }
  752. return $this->doRPCRequest($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->bodyType, $request, $runtime);
  753. }
  754. /**
  755. * Get user agent.
  756. *
  757. * @return string user agent
  758. */
  759. public function getUserAgent()
  760. {
  761. return Utils::getUserAgent($this->_userAgent);
  762. }
  763. /**
  764. * Get accesskey id by using credential.
  765. *
  766. * @return string accesskey id
  767. */
  768. public function getAccessKeyId()
  769. {
  770. if (Utils::isUnset($this->_credential)) {
  771. return '';
  772. }
  773. return $this->_credential->getAccessKeyId();
  774. }
  775. /**
  776. * Get accesskey secret by using credential.
  777. *
  778. * @return string accesskey secret
  779. */
  780. public function getAccessKeySecret()
  781. {
  782. if (Utils::isUnset($this->_credential)) {
  783. return '';
  784. }
  785. return $this->_credential->getAccessKeySecret();
  786. }
  787. /**
  788. * Get security token by using credential.
  789. *
  790. * @return string security token
  791. */
  792. public function getSecurityToken()
  793. {
  794. if (Utils::isUnset($this->_credential)) {
  795. return '';
  796. }
  797. return $this->_credential->getSecurityToken();
  798. }
  799. /**
  800. * If inputValue is not null, return it or return defaultValue.
  801. *
  802. * @param mixed $inputValue users input value
  803. * @param mixed $defaultValue default value
  804. *
  805. * @return any the final result
  806. */
  807. public static function defaultAny($inputValue, $defaultValue)
  808. {
  809. if (Utils::isUnset($inputValue)) {
  810. return $defaultValue;
  811. }
  812. return $inputValue;
  813. }
  814. /**
  815. * If the endpointRule and config.endpoint are empty, throw error.
  816. *
  817. * @param \Darabonba\OpenApi\Models\Config $config config contains the necessary information to create a client
  818. *
  819. * @throws TeaError
  820. */
  821. public function checkConfig($config)
  822. {
  823. if (Utils::empty_($this->_endpointRule) && Utils::empty_($config->endpoint)) {
  824. throw new TeaError([
  825. 'code' => 'ParameterMissing',
  826. 'message' => "'config.endpoint' can not be empty",
  827. ]);
  828. }
  829. }
  830. /**
  831. * set RPC header for debug.
  832. *
  833. * @param string[] $headers headers for debug, this header can be used only once
  834. */
  835. public function setRpcHeaders($headers)
  836. {
  837. $this->_headers = $headers;
  838. }
  839. /**
  840. * get RPC header for debug.
  841. *
  842. * @return array
  843. */
  844. public function getRpcHeaders()
  845. {
  846. $headers = $this->_headers;
  847. $this->_headers = null;
  848. return $headers;
  849. }
  850. }