OpenApiClient.php 39 KB

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