OpenApiClient.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. <?php
  2. // This file is auto-generated, don't edit it. Thanks.
  3. namespace Darabonba\OpenApi;
  4. use AlibabaCloud\Tea\Utils\Utils;
  5. use AlibabaCloud\Tea\Exception\TeaError;
  6. use AlibabaCloud\Credentials\Credential;
  7. use \Exception;
  8. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  9. use AlibabaCloud\Tea\Tea;
  10. use AlibabaCloud\Tea\Request;
  11. use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
  12. use AlibabaCloud\Tea\XML\XML;
  13. use Darabonba\GatewaySpi\Client;
  14. use AlibabaCloud\Credentials\Credential\Config;
  15. use Darabonba\OpenApi\Models\OpenApiRequest;
  16. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  17. use Darabonba\OpenApi\Models\Params;
  18. use Darabonba\GatewaySpi\Models\InterceptorContext\configuration;
  19. use Darabonba\GatewaySpi\Models\InterceptorContext;
  20. use Darabonba\GatewaySpi\Models\AttributeMap;
  21. use Darabonba\GatewaySpi\Models\InterceptorContext\response;
  22. /**
  23. * This is for OpenApi SDK
  24. */
  25. class OpenApiClient
  26. {
  27. protected $_endpoint;
  28. protected $_regionId;
  29. protected $_protocol;
  30. protected $_method;
  31. protected $_userAgent;
  32. protected $_endpointRule;
  33. protected $_endpointMap;
  34. protected $_suffix;
  35. protected $_readTimeout;
  36. protected $_connectTimeout;
  37. protected $_httpProxy;
  38. protected $_httpsProxy;
  39. protected $_socks5Proxy;
  40. protected $_socks5NetWork;
  41. protected $_noProxy;
  42. protected $_network;
  43. protected $_productId;
  44. protected $_maxIdleConns;
  45. protected $_endpointType;
  46. protected $_openPlatformEndpoint;
  47. protected $_credential;
  48. protected $_signatureVersion;
  49. protected $_signatureAlgorithm;
  50. protected $_headers;
  51. protected $_spi;
  52. protected $_globalParameters;
  53. protected $_key;
  54. protected $_cert;
  55. protected $_ca;
  56. /**
  57. * Init client with Config
  58. * @param config config contains the necessary information to create a client
  59. */
  60. public function __construct($config)
  61. {
  62. if (Utils::isUnset($config)) {
  63. throw new TeaError([
  64. "code" => "ParameterMissing",
  65. "message" => "'config' can not be unset"
  66. ]);
  67. }
  68. if (!Utils::empty_($config->accessKeyId) && !Utils::empty_($config->accessKeySecret)) {
  69. if (!Utils::empty_($config->securityToken)) {
  70. $config->type = "sts";
  71. } else {
  72. $config->type = "access_key";
  73. }
  74. $credentialConfig = new Config([
  75. "accessKeyId" => $config->accessKeyId,
  76. "type" => $config->type,
  77. "accessKeySecret" => $config->accessKeySecret
  78. ]);
  79. $credentialConfig->securityToken = $config->securityToken;
  80. $this->_credential = new Credential($credentialConfig);
  81. } else if (!Utils::isUnset($config->credential)) {
  82. $this->_credential = $config->credential;
  83. }
  84. $this->_endpoint = $config->endpoint;
  85. $this->_endpointType = $config->endpointType;
  86. $this->_network = $config->network;
  87. $this->_suffix = $config->suffix;
  88. $this->_protocol = $config->protocol;
  89. $this->_method = $config->method;
  90. $this->_regionId = $config->regionId;
  91. $this->_userAgent = $config->userAgent;
  92. $this->_readTimeout = $config->readTimeout;
  93. $this->_connectTimeout = $config->connectTimeout;
  94. $this->_httpProxy = $config->httpProxy;
  95. $this->_httpsProxy = $config->httpsProxy;
  96. $this->_noProxy = $config->noProxy;
  97. $this->_socks5Proxy = $config->socks5Proxy;
  98. $this->_socks5NetWork = $config->socks5NetWork;
  99. $this->_maxIdleConns = $config->maxIdleConns;
  100. $this->_signatureVersion = $config->signatureVersion;
  101. $this->_signatureAlgorithm = $config->signatureAlgorithm;
  102. $this->_globalParameters = $config->globalParameters;
  103. $this->_key = $config->key;
  104. $this->_cert = $config->cert;
  105. $this->_ca = $config->ca;
  106. }
  107. /**
  108. * Encapsulate the request and invoke the network
  109. * @param string $action api name
  110. * @param string $version product version
  111. * @param string $protocol http or https
  112. * @param string $method e.g. GET
  113. * @param string $authType authorization type e.g. AK
  114. * @param string $bodyType response body type e.g. String
  115. * @param OpenApiRequest $request object of OpenApiRequest
  116. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  117. * @return array the response
  118. * @throws TeaError
  119. * @throws Exception
  120. * @throws TeaUnableRetryError
  121. */
  122. public function doRPCRequest($action, $version, $protocol, $method, $authType, $bodyType, $request, $runtime)
  123. {
  124. $request->validate();
  125. $runtime->validate();
  126. $_runtime = [
  127. "timeouted" => "retry",
  128. "key" => Utils::defaultString($runtime->key, $this->_key),
  129. "cert" => Utils::defaultString($runtime->cert, $this->_cert),
  130. "ca" => Utils::defaultString($runtime->ca, $this->_ca),
  131. "readTimeout" => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  132. "connectTimeout" => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  133. "httpProxy" => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  134. "httpsProxy" => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  135. "noProxy" => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  136. "socks5Proxy" => Utils::defaultString($runtime->socks5Proxy, $this->_socks5Proxy),
  137. "socks5NetWork" => Utils::defaultString($runtime->socks5NetWork, $this->_socks5NetWork),
  138. "maxIdleConns" => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  139. "retry" => [
  140. "retryable" => $runtime->autoretry,
  141. "maxAttempts" => Utils::defaultNumber($runtime->maxAttempts, 3)
  142. ],
  143. "backoff" => [
  144. "policy" => Utils::defaultString($runtime->backoffPolicy, "no"),
  145. "period" => Utils::defaultNumber($runtime->backoffPeriod, 1)
  146. ],
  147. "ignoreSSL" => $runtime->ignoreSSL
  148. ];
  149. $_lastRequest = null;
  150. $_lastException = null;
  151. $_now = time();
  152. $_retryTimes = 0;
  153. while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
  154. if ($_retryTimes > 0) {
  155. $_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
  156. if ($_backoffTime > 0) {
  157. Tea::sleep($_backoffTime);
  158. }
  159. }
  160. $_retryTimes = $_retryTimes + 1;
  161. try {
  162. $_request = new Request();
  163. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  164. $_request->method = $method;
  165. $_request->pathname = "/";
  166. $globalQueries = [];
  167. $globalHeaders = [];
  168. if (!Utils::isUnset($this->_globalParameters)) {
  169. $globalParams = $this->_globalParameters;
  170. if (!Utils::isUnset($globalParams->queries)) {
  171. $globalQueries = $globalParams->queries;
  172. }
  173. if (!Utils::isUnset($globalParams->headers)) {
  174. $globalHeaders = $globalParams->headers;
  175. }
  176. }
  177. $_request->query = Tea::merge([
  178. "Action" => $action,
  179. "Format" => "json",
  180. "Version" => $version,
  181. "Timestamp" => OpenApiUtilClient::getTimestamp(),
  182. "SignatureNonce" => Utils::getNonce()
  183. ], $globalQueries, $request->query);
  184. $headers = $this->getRpcHeaders();
  185. if (Utils::isUnset($headers)) {
  186. // endpoint is setted in product client
  187. $_request->headers = Tea::merge([
  188. "host" => $this->_endpoint,
  189. "x-acs-version" => $version,
  190. "x-acs-action" => $action,
  191. "user-agent" => $this->getUserAgent()
  192. ], $globalHeaders);
  193. } else {
  194. $_request->headers = Tea::merge([
  195. "host" => $this->_endpoint,
  196. "x-acs-version" => $version,
  197. "x-acs-action" => $action,
  198. "user-agent" => $this->getUserAgent()
  199. ], $globalHeaders, $headers);
  200. }
  201. if (!Utils::isUnset($request->body)) {
  202. $m = Utils::assertAsMap($request->body);
  203. $tmp = Utils::anyifyMapValue(OpenApiUtilClient::query($m));
  204. $_request->body = Utils::toFormString($tmp);
  205. $_request->headers["content-type"] = "application/x-www-form-urlencoded";
  206. }
  207. if (!Utils::equalString($authType, "Anonymous")) {
  208. $accessKeyId = $this->getAccessKeyId();
  209. $accessKeySecret = $this->getAccessKeySecret();
  210. $securityToken = $this->getSecurityToken();
  211. if (!Utils::empty_($securityToken)) {
  212. $_request->query["SecurityToken"] = $securityToken;
  213. }
  214. $_request->query["SignatureMethod"] = "HMAC-SHA1";
  215. $_request->query["SignatureVersion"] = "1.0";
  216. $_request->query["AccessKeyId"] = $accessKeyId;
  217. $t = null;
  218. if (!Utils::isUnset($request->body)) {
  219. $t = Utils::assertAsMap($request->body);
  220. }
  221. $signedParam = Tea::merge($_request->query, OpenApiUtilClient::query($t));
  222. $_request->query["Signature"] = OpenApiUtilClient::getRPCSignature($signedParam, $_request->method, $accessKeySecret);
  223. }
  224. $_lastRequest = $_request;
  225. $_response = Tea::send($_request, $_runtime);
  226. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  227. $_res = Utils::readAsJSON($_response->body);
  228. $err = Utils::assertAsMap($_res);
  229. $requestId = self::defaultAny(@$err["RequestId"], @$err["requestId"]);
  230. @$err["statusCode"] = $_response->statusCode;
  231. throw new TeaError([
  232. "code" => "" . (string) (self::defaultAny(@$err["Code"], @$err["code"])) . "",
  233. "message" => "code: " . (string) ($_response->statusCode) . ", " . (string) (self::defaultAny(@$err["Message"], @$err["message"])) . " request id: " . (string) ($requestId) . "",
  234. "data" => $err,
  235. "description" => "" . (string) (self::defaultAny(@$err["Description"], @$err["description"])) . "",
  236. "accessDeniedDetail" => self::defaultAny(@$err["AccessDeniedDetail"], @$err["accessDeniedDetail"])
  237. ]);
  238. }
  239. if (Utils::equalString($bodyType, "binary")) {
  240. $resp = [
  241. "body" => $_response->body,
  242. "headers" => $_response->headers,
  243. "statusCode" => $_response->statusCode
  244. ];
  245. return $resp;
  246. } else if (Utils::equalString($bodyType, "byte")) {
  247. $byt = Utils::readAsBytes($_response->body);
  248. return [
  249. "body" => $byt,
  250. "headers" => $_response->headers,
  251. "statusCode" => $_response->statusCode
  252. ];
  253. } else if (Utils::equalString($bodyType, "string")) {
  254. $str = Utils::readAsString($_response->body);
  255. return [
  256. "body" => $str,
  257. "headers" => $_response->headers,
  258. "statusCode" => $_response->statusCode
  259. ];
  260. } else if (Utils::equalString($bodyType, "json")) {
  261. $obj = Utils::readAsJSON($_response->body);
  262. $res = Utils::assertAsMap($obj);
  263. return [
  264. "body" => $res,
  265. "headers" => $_response->headers,
  266. "statusCode" => $_response->statusCode
  267. ];
  268. } else if (Utils::equalString($bodyType, "array")) {
  269. $arr = Utils::readAsJSON($_response->body);
  270. return [
  271. "body" => $arr,
  272. "headers" => $_response->headers,
  273. "statusCode" => $_response->statusCode
  274. ];
  275. } else {
  276. return [
  277. "headers" => $_response->headers,
  278. "statusCode" => $_response->statusCode
  279. ];
  280. }
  281. } catch (Exception $e) {
  282. if (!($e instanceof TeaError)) {
  283. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  284. }
  285. if (Tea::isRetryable($e)) {
  286. $_lastException = $e;
  287. continue;
  288. }
  289. throw $e;
  290. }
  291. }
  292. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  293. }
  294. /**
  295. * Encapsulate the request and invoke the network
  296. * @param string $action api name
  297. * @param string $version product version
  298. * @param string $protocol http or https
  299. * @param string $method e.g. GET
  300. * @param string $authType authorization type e.g. AK
  301. * @param string $pathname pathname of every api
  302. * @param string $bodyType response body type e.g. String
  303. * @param OpenApiRequest $request object of OpenApiRequest
  304. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  305. * @return array the response
  306. * @throws TeaError
  307. * @throws Exception
  308. * @throws TeaUnableRetryError
  309. */
  310. public function doROARequest($action, $version, $protocol, $method, $authType, $pathname, $bodyType, $request, $runtime)
  311. {
  312. $request->validate();
  313. $runtime->validate();
  314. $_runtime = [
  315. "timeouted" => "retry",
  316. "key" => Utils::defaultString($runtime->key, $this->_key),
  317. "cert" => Utils::defaultString($runtime->cert, $this->_cert),
  318. "ca" => Utils::defaultString($runtime->ca, $this->_ca),
  319. "readTimeout" => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  320. "connectTimeout" => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  321. "httpProxy" => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  322. "httpsProxy" => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  323. "noProxy" => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  324. "socks5Proxy" => Utils::defaultString($runtime->socks5Proxy, $this->_socks5Proxy),
  325. "socks5NetWork" => Utils::defaultString($runtime->socks5NetWork, $this->_socks5NetWork),
  326. "maxIdleConns" => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  327. "retry" => [
  328. "retryable" => $runtime->autoretry,
  329. "maxAttempts" => Utils::defaultNumber($runtime->maxAttempts, 3)
  330. ],
  331. "backoff" => [
  332. "policy" => Utils::defaultString($runtime->backoffPolicy, "no"),
  333. "period" => Utils::defaultNumber($runtime->backoffPeriod, 1)
  334. ],
  335. "ignoreSSL" => $runtime->ignoreSSL
  336. ];
  337. $_lastRequest = null;
  338. $_lastException = null;
  339. $_now = time();
  340. $_retryTimes = 0;
  341. while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
  342. if ($_retryTimes > 0) {
  343. $_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
  344. if ($_backoffTime > 0) {
  345. Tea::sleep($_backoffTime);
  346. }
  347. }
  348. $_retryTimes = $_retryTimes + 1;
  349. try {
  350. $_request = new Request();
  351. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  352. $_request->method = $method;
  353. $_request->pathname = $pathname;
  354. $globalQueries = [];
  355. $globalHeaders = [];
  356. if (!Utils::isUnset($this->_globalParameters)) {
  357. $globalParams = $this->_globalParameters;
  358. if (!Utils::isUnset($globalParams->queries)) {
  359. $globalQueries = $globalParams->queries;
  360. }
  361. if (!Utils::isUnset($globalParams->headers)) {
  362. $globalHeaders = $globalParams->headers;
  363. }
  364. }
  365. $_request->headers = Tea::merge([
  366. "date" => Utils::getDateUTCString(),
  367. "host" => $this->_endpoint,
  368. "accept" => "application/json",
  369. "x-acs-signature-nonce" => Utils::getNonce(),
  370. "x-acs-signature-method" => "HMAC-SHA1",
  371. "x-acs-signature-version" => "1.0",
  372. "x-acs-version" => $version,
  373. "x-acs-action" => $action,
  374. "user-agent" => Utils::getUserAgent($this->_userAgent)
  375. ], $globalHeaders, $request->headers);
  376. if (!Utils::isUnset($request->body)) {
  377. $_request->body = Utils::toJSONString($request->body);
  378. $_request->headers["content-type"] = "application/json; charset=utf-8";
  379. }
  380. $_request->query = $globalQueries;
  381. if (!Utils::isUnset($request->query)) {
  382. $_request->query = Tea::merge($_request->query, $request->query);
  383. }
  384. if (!Utils::equalString($authType, "Anonymous")) {
  385. $accessKeyId = $this->getAccessKeyId();
  386. $accessKeySecret = $this->getAccessKeySecret();
  387. $securityToken = $this->getSecurityToken();
  388. if (!Utils::empty_($securityToken)) {
  389. $_request->headers["x-acs-accesskey-id"] = $accessKeyId;
  390. $_request->headers["x-acs-security-token"] = $securityToken;
  391. }
  392. $stringToSign = OpenApiUtilClient::getStringToSign($_request);
  393. $_request->headers["authorization"] = "acs " . $accessKeyId . ":" . OpenApiUtilClient::getROASignature($stringToSign, $accessKeySecret) . "";
  394. }
  395. $_lastRequest = $_request;
  396. $_response = Tea::send($_request, $_runtime);
  397. if (Utils::equalNumber($_response->statusCode, 204)) {
  398. return [
  399. "headers" => $_response->headers
  400. ];
  401. }
  402. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  403. $_res = Utils::readAsJSON($_response->body);
  404. $err = Utils::assertAsMap($_res);
  405. $requestId = self::defaultAny(@$err["RequestId"], @$err["requestId"]);
  406. $requestId = self::defaultAny($requestId, @$err["requestid"]);
  407. @$err["statusCode"] = $_response->statusCode;
  408. throw new TeaError([
  409. "code" => "" . (string) (self::defaultAny(@$err["Code"], @$err["code"])) . "",
  410. "message" => "code: " . (string) ($_response->statusCode) . ", " . (string) (self::defaultAny(@$err["Message"], @$err["message"])) . " request id: " . (string) ($requestId) . "",
  411. "data" => $err,
  412. "description" => "" . (string) (self::defaultAny(@$err["Description"], @$err["description"])) . "",
  413. "accessDeniedDetail" => self::defaultAny(@$err["AccessDeniedDetail"], @$err["accessDeniedDetail"])
  414. ]);
  415. }
  416. if (Utils::equalString($bodyType, "binary")) {
  417. $resp = [
  418. "body" => $_response->body,
  419. "headers" => $_response->headers,
  420. "statusCode" => $_response->statusCode
  421. ];
  422. return $resp;
  423. } else if (Utils::equalString($bodyType, "byte")) {
  424. $byt = Utils::readAsBytes($_response->body);
  425. return [
  426. "body" => $byt,
  427. "headers" => $_response->headers,
  428. "statusCode" => $_response->statusCode
  429. ];
  430. } else if (Utils::equalString($bodyType, "string")) {
  431. $str = Utils::readAsString($_response->body);
  432. return [
  433. "body" => $str,
  434. "headers" => $_response->headers,
  435. "statusCode" => $_response->statusCode
  436. ];
  437. } else if (Utils::equalString($bodyType, "json")) {
  438. $obj = Utils::readAsJSON($_response->body);
  439. $res = Utils::assertAsMap($obj);
  440. return [
  441. "body" => $res,
  442. "headers" => $_response->headers,
  443. "statusCode" => $_response->statusCode
  444. ];
  445. } else if (Utils::equalString($bodyType, "array")) {
  446. $arr = Utils::readAsJSON($_response->body);
  447. return [
  448. "body" => $arr,
  449. "headers" => $_response->headers,
  450. "statusCode" => $_response->statusCode
  451. ];
  452. } else {
  453. return [
  454. "headers" => $_response->headers,
  455. "statusCode" => $_response->statusCode
  456. ];
  457. }
  458. } catch (Exception $e) {
  459. if (!($e instanceof TeaError)) {
  460. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  461. }
  462. if (Tea::isRetryable($e)) {
  463. $_lastException = $e;
  464. continue;
  465. }
  466. throw $e;
  467. }
  468. }
  469. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  470. }
  471. /**
  472. * Encapsulate the request and invoke the network with form body
  473. * @param string $action api name
  474. * @param string $version product version
  475. * @param string $protocol http or https
  476. * @param string $method e.g. GET
  477. * @param string $authType authorization type e.g. AK
  478. * @param string $pathname pathname of every api
  479. * @param string $bodyType response body type e.g. String
  480. * @param OpenApiRequest $request object of OpenApiRequest
  481. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  482. * @return array the response
  483. * @throws TeaError
  484. * @throws Exception
  485. * @throws TeaUnableRetryError
  486. */
  487. public function doROARequestWithForm($action, $version, $protocol, $method, $authType, $pathname, $bodyType, $request, $runtime)
  488. {
  489. $request->validate();
  490. $runtime->validate();
  491. $_runtime = [
  492. "timeouted" => "retry",
  493. "key" => Utils::defaultString($runtime->key, $this->_key),
  494. "cert" => Utils::defaultString($runtime->cert, $this->_cert),
  495. "ca" => Utils::defaultString($runtime->ca, $this->_ca),
  496. "readTimeout" => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  497. "connectTimeout" => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  498. "httpProxy" => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  499. "httpsProxy" => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  500. "noProxy" => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  501. "socks5Proxy" => Utils::defaultString($runtime->socks5Proxy, $this->_socks5Proxy),
  502. "socks5NetWork" => Utils::defaultString($runtime->socks5NetWork, $this->_socks5NetWork),
  503. "maxIdleConns" => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  504. "retry" => [
  505. "retryable" => $runtime->autoretry,
  506. "maxAttempts" => Utils::defaultNumber($runtime->maxAttempts, 3)
  507. ],
  508. "backoff" => [
  509. "policy" => Utils::defaultString($runtime->backoffPolicy, "no"),
  510. "period" => Utils::defaultNumber($runtime->backoffPeriod, 1)
  511. ],
  512. "ignoreSSL" => $runtime->ignoreSSL
  513. ];
  514. $_lastRequest = null;
  515. $_lastException = null;
  516. $_now = time();
  517. $_retryTimes = 0;
  518. while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
  519. if ($_retryTimes > 0) {
  520. $_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
  521. if ($_backoffTime > 0) {
  522. Tea::sleep($_backoffTime);
  523. }
  524. }
  525. $_retryTimes = $_retryTimes + 1;
  526. try {
  527. $_request = new Request();
  528. $_request->protocol = Utils::defaultString($this->_protocol, $protocol);
  529. $_request->method = $method;
  530. $_request->pathname = $pathname;
  531. $globalQueries = [];
  532. $globalHeaders = [];
  533. if (!Utils::isUnset($this->_globalParameters)) {
  534. $globalParams = $this->_globalParameters;
  535. if (!Utils::isUnset($globalParams->queries)) {
  536. $globalQueries = $globalParams->queries;
  537. }
  538. if (!Utils::isUnset($globalParams->headers)) {
  539. $globalHeaders = $globalParams->headers;
  540. }
  541. }
  542. $_request->headers = Tea::merge([
  543. "date" => Utils::getDateUTCString(),
  544. "host" => $this->_endpoint,
  545. "accept" => "application/json",
  546. "x-acs-signature-nonce" => Utils::getNonce(),
  547. "x-acs-signature-method" => "HMAC-SHA1",
  548. "x-acs-signature-version" => "1.0",
  549. "x-acs-version" => $version,
  550. "x-acs-action" => $action,
  551. "user-agent" => Utils::getUserAgent($this->_userAgent)
  552. ], $globalHeaders, $request->headers);
  553. if (!Utils::isUnset($request->body)) {
  554. $m = Utils::assertAsMap($request->body);
  555. $_request->body = OpenApiUtilClient::toForm($m);
  556. $_request->headers["content-type"] = "application/x-www-form-urlencoded";
  557. }
  558. $_request->query = $globalQueries;
  559. if (!Utils::isUnset($request->query)) {
  560. $_request->query = Tea::merge($_request->query, $request->query);
  561. }
  562. if (!Utils::equalString($authType, "Anonymous")) {
  563. $accessKeyId = $this->getAccessKeyId();
  564. $accessKeySecret = $this->getAccessKeySecret();
  565. $securityToken = $this->getSecurityToken();
  566. if (!Utils::empty_($securityToken)) {
  567. $_request->headers["x-acs-accesskey-id"] = $accessKeyId;
  568. $_request->headers["x-acs-security-token"] = $securityToken;
  569. }
  570. $stringToSign = OpenApiUtilClient::getStringToSign($_request);
  571. $_request->headers["authorization"] = "acs " . $accessKeyId . ":" . OpenApiUtilClient::getROASignature($stringToSign, $accessKeySecret) . "";
  572. }
  573. $_lastRequest = $_request;
  574. $_response = Tea::send($_request, $_runtime);
  575. if (Utils::equalNumber($_response->statusCode, 204)) {
  576. return [
  577. "headers" => $_response->headers
  578. ];
  579. }
  580. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  581. $_res = Utils::readAsJSON($_response->body);
  582. $err = Utils::assertAsMap($_res);
  583. @$err["statusCode"] = $_response->statusCode;
  584. throw new TeaError([
  585. "code" => "" . (string) (self::defaultAny(@$err["Code"], @$err["code"])) . "",
  586. "message" => "code: " . (string) ($_response->statusCode) . ", " . (string) (self::defaultAny(@$err["Message"], @$err["message"])) . " request id: " . (string) (self::defaultAny(@$err["RequestId"], @$err["requestId"])) . "",
  587. "data" => $err,
  588. "description" => "" . (string) (self::defaultAny(@$err["Description"], @$err["description"])) . "",
  589. "accessDeniedDetail" => self::defaultAny(@$err["AccessDeniedDetail"], @$err["accessDeniedDetail"])
  590. ]);
  591. }
  592. if (Utils::equalString($bodyType, "binary")) {
  593. $resp = [
  594. "body" => $_response->body,
  595. "headers" => $_response->headers,
  596. "statusCode" => $_response->statusCode
  597. ];
  598. return $resp;
  599. } else if (Utils::equalString($bodyType, "byte")) {
  600. $byt = Utils::readAsBytes($_response->body);
  601. return [
  602. "body" => $byt,
  603. "headers" => $_response->headers,
  604. "statusCode" => $_response->statusCode
  605. ];
  606. } else if (Utils::equalString($bodyType, "string")) {
  607. $str = Utils::readAsString($_response->body);
  608. return [
  609. "body" => $str,
  610. "headers" => $_response->headers,
  611. "statusCode" => $_response->statusCode
  612. ];
  613. } else if (Utils::equalString($bodyType, "json")) {
  614. $obj = Utils::readAsJSON($_response->body);
  615. $res = Utils::assertAsMap($obj);
  616. return [
  617. "body" => $res,
  618. "headers" => $_response->headers,
  619. "statusCode" => $_response->statusCode
  620. ];
  621. } else if (Utils::equalString($bodyType, "array")) {
  622. $arr = Utils::readAsJSON($_response->body);
  623. return [
  624. "body" => $arr,
  625. "headers" => $_response->headers,
  626. "statusCode" => $_response->statusCode
  627. ];
  628. } else {
  629. return [
  630. "headers" => $_response->headers,
  631. "statusCode" => $_response->statusCode
  632. ];
  633. }
  634. } catch (Exception $e) {
  635. if (!($e instanceof TeaError)) {
  636. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  637. }
  638. if (Tea::isRetryable($e)) {
  639. $_lastException = $e;
  640. continue;
  641. }
  642. throw $e;
  643. }
  644. }
  645. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  646. }
  647. /**
  648. * Encapsulate the request and invoke the network
  649. * @param Params $params
  650. * @param OpenApiRequest $request object of OpenApiRequest
  651. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  652. * @return array the response
  653. * @throws TeaError
  654. * @throws Exception
  655. * @throws TeaUnableRetryError
  656. */
  657. public function doRequest($params, $request, $runtime)
  658. {
  659. $params->validate();
  660. $request->validate();
  661. $runtime->validate();
  662. $_runtime = [
  663. "timeouted" => "retry",
  664. "key" => Utils::defaultString($runtime->key, $this->_key),
  665. "cert" => Utils::defaultString($runtime->cert, $this->_cert),
  666. "ca" => Utils::defaultString($runtime->ca, $this->_ca),
  667. "readTimeout" => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  668. "connectTimeout" => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  669. "httpProxy" => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  670. "httpsProxy" => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  671. "noProxy" => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  672. "socks5Proxy" => Utils::defaultString($runtime->socks5Proxy, $this->_socks5Proxy),
  673. "socks5NetWork" => Utils::defaultString($runtime->socks5NetWork, $this->_socks5NetWork),
  674. "maxIdleConns" => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  675. "retry" => [
  676. "retryable" => $runtime->autoretry,
  677. "maxAttempts" => Utils::defaultNumber($runtime->maxAttempts, 3)
  678. ],
  679. "backoff" => [
  680. "policy" => Utils::defaultString($runtime->backoffPolicy, "no"),
  681. "period" => Utils::defaultNumber($runtime->backoffPeriod, 1)
  682. ],
  683. "ignoreSSL" => $runtime->ignoreSSL
  684. ];
  685. $_lastRequest = null;
  686. $_lastException = null;
  687. $_now = time();
  688. $_retryTimes = 0;
  689. while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
  690. if ($_retryTimes > 0) {
  691. $_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
  692. if ($_backoffTime > 0) {
  693. Tea::sleep($_backoffTime);
  694. }
  695. }
  696. $_retryTimes = $_retryTimes + 1;
  697. try {
  698. $_request = new Request();
  699. $_request->protocol = Utils::defaultString($this->_protocol, $params->protocol);
  700. $_request->method = $params->method;
  701. $_request->pathname = $params->pathname;
  702. $globalQueries = [];
  703. $globalHeaders = [];
  704. if (!Utils::isUnset($this->_globalParameters)) {
  705. $globalParams = $this->_globalParameters;
  706. if (!Utils::isUnset($globalParams->queries)) {
  707. $globalQueries = $globalParams->queries;
  708. }
  709. if (!Utils::isUnset($globalParams->headers)) {
  710. $globalHeaders = $globalParams->headers;
  711. }
  712. }
  713. $_request->query = Tea::merge($globalQueries, $request->query);
  714. // endpoint is setted in product client
  715. $_request->headers = Tea::merge([
  716. "host" => $this->_endpoint,
  717. "x-acs-version" => $params->version,
  718. "x-acs-action" => $params->action,
  719. "user-agent" => $this->getUserAgent(),
  720. "x-acs-date" => OpenApiUtilClient::getTimestamp(),
  721. "x-acs-signature-nonce" => Utils::getNonce(),
  722. "accept" => "application/json"
  723. ], $globalHeaders, $request->headers);
  724. if (Utils::equalString($params->style, "RPC")) {
  725. $headers = $this->getRpcHeaders();
  726. if (!Utils::isUnset($headers)) {
  727. $_request->headers = Tea::merge($_request->headers, $headers);
  728. }
  729. }
  730. $signatureAlgorithm = Utils::defaultString($this->_signatureAlgorithm, "ACS3-HMAC-SHA256");
  731. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes(""), $signatureAlgorithm));
  732. if (!Utils::isUnset($request->stream)) {
  733. $tmp = Utils::readAsBytes($request->stream);
  734. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash($tmp, $signatureAlgorithm));
  735. $_request->body = $tmp;
  736. $_request->headers["content-type"] = "application/octet-stream";
  737. } else {
  738. if (!Utils::isUnset($request->body)) {
  739. if (Utils::equalString($params->reqBodyType, "json")) {
  740. $jsonObj = Utils::toJSONString($request->body);
  741. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes($jsonObj), $signatureAlgorithm));
  742. $_request->body = $jsonObj;
  743. $_request->headers["content-type"] = "application/json; charset=utf-8";
  744. } else {
  745. $m = Utils::assertAsMap($request->body);
  746. $formObj = OpenApiUtilClient::toForm($m);
  747. $hashedRequestPayload = OpenApiUtilClient::hexEncode(OpenApiUtilClient::hash(Utils::toBytes($formObj), $signatureAlgorithm));
  748. $_request->body = $formObj;
  749. $_request->headers["content-type"] = "application/x-www-form-urlencoded";
  750. }
  751. }
  752. }
  753. $_request->headers["x-acs-content-sha256"] = $hashedRequestPayload;
  754. if (!Utils::equalString($params->authType, "Anonymous")) {
  755. $authType = $this->getType();
  756. if (Utils::equalString($authType, "bearer")) {
  757. $bearerToken = $this->getBearerToken();
  758. $_request->headers["x-acs-bearer-token"] = $bearerToken;
  759. } else {
  760. $accessKeyId = $this->getAccessKeyId();
  761. $accessKeySecret = $this->getAccessKeySecret();
  762. $securityToken = $this->getSecurityToken();
  763. if (!Utils::empty_($securityToken)) {
  764. $_request->headers["x-acs-accesskey-id"] = $accessKeyId;
  765. $_request->headers["x-acs-security-token"] = $securityToken;
  766. }
  767. $_request->headers["Authorization"] = OpenApiUtilClient::getAuthorization($_request, $signatureAlgorithm, $hashedRequestPayload, $accessKeyId, $accessKeySecret);
  768. }
  769. }
  770. $_lastRequest = $_request;
  771. $_response = Tea::send($_request, $_runtime);
  772. if (Utils::is4xx($_response->statusCode) || Utils::is5xx($_response->statusCode)) {
  773. $_res = Utils::readAsJSON($_response->body);
  774. $err = Utils::assertAsMap($_res);
  775. @$err["statusCode"] = $_response->statusCode;
  776. throw new TeaError([
  777. "code" => "" . (string) (self::defaultAny(@$err["Code"], @$err["code"])) . "",
  778. "message" => "code: " . (string) ($_response->statusCode) . ", " . (string) (self::defaultAny(@$err["Message"], @$err["message"])) . " request id: " . (string) (self::defaultAny(@$err["RequestId"], @$err["requestId"])) . "",
  779. "data" => $err,
  780. "description" => "" . (string) (self::defaultAny(@$err["Description"], @$err["description"])) . "",
  781. "accessDeniedDetail" => self::defaultAny(@$err["AccessDeniedDetail"], @$err["accessDeniedDetail"])
  782. ]);
  783. }
  784. if (Utils::equalString($params->bodyType, "binary")) {
  785. $resp = [
  786. "body" => $_response->body,
  787. "headers" => $_response->headers,
  788. "statusCode" => $_response->statusCode
  789. ];
  790. return $resp;
  791. } else if (Utils::equalString($params->bodyType, "byte")) {
  792. $byt = Utils::readAsBytes($_response->body);
  793. return [
  794. "body" => $byt,
  795. "headers" => $_response->headers,
  796. "statusCode" => $_response->statusCode
  797. ];
  798. } else if (Utils::equalString($params->bodyType, "string")) {
  799. $str = Utils::readAsString($_response->body);
  800. return [
  801. "body" => $str,
  802. "headers" => $_response->headers,
  803. "statusCode" => $_response->statusCode
  804. ];
  805. } else if (Utils::equalString($params->bodyType, "json")) {
  806. $obj = Utils::readAsJSON($_response->body);
  807. $res = Utils::assertAsMap($obj);
  808. return [
  809. "body" => $res,
  810. "headers" => $_response->headers,
  811. "statusCode" => $_response->statusCode
  812. ];
  813. } else if (Utils::equalString($params->bodyType, "array")) {
  814. $arr = Utils::readAsJSON($_response->body);
  815. return [
  816. "body" => $arr,
  817. "headers" => $_response->headers,
  818. "statusCode" => $_response->statusCode
  819. ];
  820. } else {
  821. return [
  822. "headers" => $_response->headers,
  823. "statusCode" => $_response->statusCode
  824. ];
  825. }
  826. } catch (Exception $e) {
  827. if (!($e instanceof TeaError)) {
  828. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  829. }
  830. if (Tea::isRetryable($e)) {
  831. $_lastException = $e;
  832. continue;
  833. }
  834. throw $e;
  835. }
  836. }
  837. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  838. }
  839. /**
  840. * Encapsulate the request and invoke the network
  841. * @param Params $params
  842. * @param OpenApiRequest $request object of OpenApiRequest
  843. * @param RuntimeOptions $runtime which controls some details of call api, such as retry times
  844. * @return array the response
  845. * @throws TeaError
  846. * @throws Exception
  847. * @throws TeaUnableRetryError
  848. */
  849. public function execute($params, $request, $runtime)
  850. {
  851. $params->validate();
  852. $request->validate();
  853. $runtime->validate();
  854. $_runtime = [
  855. "timeouted" => "retry",
  856. "key" => Utils::defaultString($runtime->key, $this->_key),
  857. "cert" => Utils::defaultString($runtime->cert, $this->_cert),
  858. "ca" => Utils::defaultString($runtime->ca, $this->_ca),
  859. "readTimeout" => Utils::defaultNumber($runtime->readTimeout, $this->_readTimeout),
  860. "connectTimeout" => Utils::defaultNumber($runtime->connectTimeout, $this->_connectTimeout),
  861. "httpProxy" => Utils::defaultString($runtime->httpProxy, $this->_httpProxy),
  862. "httpsProxy" => Utils::defaultString($runtime->httpsProxy, $this->_httpsProxy),
  863. "noProxy" => Utils::defaultString($runtime->noProxy, $this->_noProxy),
  864. "socks5Proxy" => Utils::defaultString($runtime->socks5Proxy, $this->_socks5Proxy),
  865. "socks5NetWork" => Utils::defaultString($runtime->socks5NetWork, $this->_socks5NetWork),
  866. "maxIdleConns" => Utils::defaultNumber($runtime->maxIdleConns, $this->_maxIdleConns),
  867. "retry" => [
  868. "retryable" => $runtime->autoretry,
  869. "maxAttempts" => Utils::defaultNumber($runtime->maxAttempts, 3)
  870. ],
  871. "backoff" => [
  872. "policy" => Utils::defaultString($runtime->backoffPolicy, "no"),
  873. "period" => Utils::defaultNumber($runtime->backoffPeriod, 1)
  874. ],
  875. "ignoreSSL" => $runtime->ignoreSSL
  876. ];
  877. $_lastRequest = null;
  878. $_lastException = null;
  879. $_now = time();
  880. $_retryTimes = 0;
  881. while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
  882. if ($_retryTimes > 0) {
  883. $_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
  884. if ($_backoffTime > 0) {
  885. Tea::sleep($_backoffTime);
  886. }
  887. }
  888. $_retryTimes = $_retryTimes + 1;
  889. try {
  890. $_request = new Request();
  891. // spi = new Gateway();//Gateway implements SPI,这一步在产品 SDK 中实例化
  892. $headers = $this->getRpcHeaders();
  893. $globalQueries = [];
  894. $globalHeaders = [];
  895. if (!Utils::isUnset($this->_globalParameters)) {
  896. $globalParams = $this->_globalParameters;
  897. if (!Utils::isUnset($globalParams->queries)) {
  898. $globalQueries = $globalParams->queries;
  899. }
  900. if (!Utils::isUnset($globalParams->headers)) {
  901. $globalHeaders = $globalParams->headers;
  902. }
  903. }
  904. $requestContext = new \Darabonba\GatewaySpi\Models\InterceptorContext\request([
  905. "headers" => Tea::merge($globalHeaders, $request->headers, $headers),
  906. "query" => Tea::merge($globalQueries, $request->query),
  907. "body" => $request->body,
  908. "stream" => $request->stream,
  909. "hostMap" => $request->hostMap,
  910. "pathname" => $params->pathname,
  911. "productId" => $this->_productId,
  912. "action" => $params->action,
  913. "version" => $params->version,
  914. "protocol" => Utils::defaultString($this->_protocol, $params->protocol),
  915. "method" => Utils::defaultString($this->_method, $params->method),
  916. "authType" => $params->authType,
  917. "bodyType" => $params->bodyType,
  918. "reqBodyType" => $params->reqBodyType,
  919. "style" => $params->style,
  920. "credential" => $this->_credential,
  921. "signatureVersion" => $this->_signatureVersion,
  922. "signatureAlgorithm" => $this->_signatureAlgorithm,
  923. "userAgent" => $this->getUserAgent()
  924. ]);
  925. $configurationContext = new configuration([
  926. "regionId" => $this->_regionId,
  927. "endpoint" => Utils::defaultString($request->endpointOverride, $this->_endpoint),
  928. "endpointRule" => $this->_endpointRule,
  929. "endpointMap" => $this->_endpointMap,
  930. "endpointType" => $this->_endpointType,
  931. "network" => $this->_network,
  932. "suffix" => $this->_suffix
  933. ]);
  934. $interceptorContext = new InterceptorContext([
  935. "request" => $requestContext,
  936. "configuration" => $configurationContext
  937. ]);
  938. $attributeMap = new AttributeMap([]);
  939. // 1. spi.modifyConfiguration(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap);
  940. $this->_spi->modifyConfiguration($interceptorContext, $attributeMap);
  941. // 2. spi.modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap);
  942. $this->_spi->modifyRequest($interceptorContext, $attributeMap);
  943. $_request->protocol = $interceptorContext->request->protocol;
  944. $_request->method = $interceptorContext->request->method;
  945. $_request->pathname = $interceptorContext->request->pathname;
  946. $_request->query = $interceptorContext->request->query;
  947. $_request->body = $interceptorContext->request->stream;
  948. $_request->headers = $interceptorContext->request->headers;
  949. $_lastRequest = $_request;
  950. $_response = Tea::send($_request, $_runtime);
  951. $responseContext = new response([
  952. "statusCode" => $_response->statusCode,
  953. "headers" => $_response->headers,
  954. "body" => $_response->body
  955. ]);
  956. $interceptorContext->response = $responseContext;
  957. // 3. spi.modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap);
  958. $this->_spi->modifyResponse($interceptorContext, $attributeMap);
  959. return [
  960. "headers" => $interceptorContext->response->headers,
  961. "statusCode" => $interceptorContext->response->statusCode,
  962. "body" => $interceptorContext->response->deserializedBody
  963. ];
  964. } catch (Exception $e) {
  965. if (!($e instanceof TeaError)) {
  966. $e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
  967. }
  968. if (Tea::isRetryable($e)) {
  969. $_lastException = $e;
  970. continue;
  971. }
  972. throw $e;
  973. }
  974. }
  975. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  976. }
  977. /**
  978. * @param Params $params
  979. * @param OpenApiRequest $request
  980. * @param RuntimeOptions $runtime
  981. * @return array
  982. * @throws TeaError
  983. */
  984. public function callApi($params, $request, $runtime)
  985. {
  986. if (Utils::isUnset($params)) {
  987. throw new TeaError([
  988. "code" => "ParameterMissing",
  989. "message" => "'params' can not be unset"
  990. ]);
  991. }
  992. if (Utils::isUnset($this->_signatureAlgorithm) || !Utils::equalString($this->_signatureAlgorithm, "v2")) {
  993. return $this->doRequest($params, $request, $runtime);
  994. } else if (Utils::equalString($params->style, "ROA") && Utils::equalString($params->reqBodyType, "json")) {
  995. return $this->doROARequest($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->pathname, $params->bodyType, $request, $runtime);
  996. } else if (Utils::equalString($params->style, "ROA")) {
  997. return $this->doROARequestWithForm($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->pathname, $params->bodyType, $request, $runtime);
  998. } else {
  999. return $this->doRPCRequest($params->action, $params->version, $params->protocol, $params->method, $params->authType, $params->bodyType, $request, $runtime);
  1000. }
  1001. }
  1002. /**
  1003. * Get user agent
  1004. * @return string user agent
  1005. */
  1006. public function getUserAgent()
  1007. {
  1008. $userAgent = Utils::getUserAgent($this->_userAgent);
  1009. return $userAgent;
  1010. }
  1011. /**
  1012. * Get accesskey id by using credential
  1013. * @return string accesskey id
  1014. */
  1015. public function getAccessKeyId()
  1016. {
  1017. if (Utils::isUnset($this->_credential)) {
  1018. return '';
  1019. }
  1020. $accessKeyId = $this->_credential->getAccessKeyId();
  1021. return $accessKeyId;
  1022. }
  1023. /**
  1024. * Get accesskey secret by using credential
  1025. * @return string accesskey secret
  1026. */
  1027. public function getAccessKeySecret()
  1028. {
  1029. if (Utils::isUnset($this->_credential)) {
  1030. return '';
  1031. }
  1032. $secret = $this->_credential->getAccessKeySecret();
  1033. return $secret;
  1034. }
  1035. /**
  1036. * Get security token by using credential
  1037. * @return string security token
  1038. */
  1039. public function getSecurityToken()
  1040. {
  1041. if (Utils::isUnset($this->_credential)) {
  1042. return '';
  1043. }
  1044. $token = $this->_credential->getSecurityToken();
  1045. return $token;
  1046. }
  1047. /**
  1048. * Get bearer token by credential
  1049. * @return string bearer token
  1050. */
  1051. public function getBearerToken()
  1052. {
  1053. if (Utils::isUnset($this->_credential)) {
  1054. return '';
  1055. }
  1056. $token = $this->_credential->getBearerToken();
  1057. return $token;
  1058. }
  1059. /**
  1060. * Get credential type by credential
  1061. * @return string credential type e.g. access_key
  1062. */
  1063. public function getType()
  1064. {
  1065. if (Utils::isUnset($this->_credential)) {
  1066. return '';
  1067. }
  1068. $authType = $this->_credential->getType();
  1069. return $authType;
  1070. }
  1071. /**
  1072. * If inputValue is not null, return it or return defaultValue
  1073. * @param mixed $inputValue users input value
  1074. * @param mixed $defaultValue default value
  1075. * @return any the final result
  1076. */
  1077. public static function defaultAny($inputValue, $defaultValue)
  1078. {
  1079. if (Utils::isUnset($inputValue)) {
  1080. return $defaultValue;
  1081. }
  1082. return $inputValue;
  1083. }
  1084. /**
  1085. * If the endpointRule and config.endpoint are empty, throw error
  1086. * @param \Darabonba\OpenApi\Models\Config $config config contains the necessary information to create a client
  1087. * @return void
  1088. * @throws TeaError
  1089. */
  1090. public function checkConfig($config)
  1091. {
  1092. if (Utils::empty_($this->_endpointRule) && Utils::empty_($config->endpoint)) {
  1093. throw new TeaError([
  1094. "code" => "ParameterMissing",
  1095. "message" => "'config.endpoint' can not be empty"
  1096. ]);
  1097. }
  1098. }
  1099. /**
  1100. * set gateway client
  1101. * @param Client $spi
  1102. * @return void
  1103. */
  1104. public function setGatewayClient($spi)
  1105. {
  1106. $this->_spi = $spi;
  1107. }
  1108. /**
  1109. * set RPC header for debug
  1110. * @param string[] $headers headers for debug, this header can be used only once.
  1111. * @return void
  1112. */
  1113. public function setRpcHeaders($headers)
  1114. {
  1115. $this->_headers = $headers;
  1116. }
  1117. /**
  1118. * get RPC header for debug
  1119. * @return array
  1120. */
  1121. public function getRpcHeaders()
  1122. {
  1123. $headers = $this->_headers;
  1124. $this->_headers = null;
  1125. return $headers;
  1126. }
  1127. }