DefaultAcsClient.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace common\services\mall\aliyunsts\StsCode;
  3. use common\services\mall\aliyunsts\StsCode\Exception\ClientException;
  4. use common\services\mall\aliyunsts\StsCode\Exception\ServerException;
  5. use common\services\mall\aliyunsts\StsCode\Http\HttpHelper;
  6. use common\services\mall\aliyunsts\StsCode\Regions\EndpointProvider;
  7. /*
  8. * Licensed to the Apache Software Foundation (ASF) under one
  9. * or more contributor license agreements. See the NOTICE file
  10. * distributed with this work for additional information
  11. * regarding copyright ownership. The ASF licenses this file
  12. * to you under the Apache License, Version 2.0 (the
  13. * "License"); you may not use this file except in compliance
  14. * with the License. You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing,
  19. * software distributed under the License is distributed on an
  20. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  21. * KIND, either express or implied. See the License for the
  22. * specific language governing permissions and limitations
  23. * under the License.
  24. */
  25. class DefaultAcsClient implements IAcsClient
  26. {
  27. public $iClientProfile;
  28. function __construct($iClientProfile)
  29. {
  30. $this->iClientProfile = $iClientProfile;
  31. }
  32. public function getAcsResponse($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  33. {
  34. $httpResponse = $this->doAction($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  35. $respObject = $this->parseAcsResponse($httpResponse->getBody(), $request->getAcceptFormat());
  36. if(false == $httpResponse->isSuccess())
  37. {
  38. $this->buildApiException($respObject, $httpResponse->getStatus());
  39. }
  40. return $respObject;
  41. }
  42. public function doAction($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  43. {
  44. if(null == $this->iClientProfile && (null == $iSigner || null == $credential
  45. || null == $request->getRegionId() || null == $request->getAcceptFormat()))
  46. {
  47. throw new ClientException("No active profile found.", "SDK.InvalidProfile");
  48. }
  49. if(null == $iSigner)
  50. {
  51. $iSigner = $this->iClientProfile->getSigner();
  52. }
  53. if(null == $credential)
  54. {
  55. $credential = $this->iClientProfile->getCredential();
  56. }
  57. $request = $this->prepareRequest($request);
  58. $domain = EndpointProvider::findProductDomain($request->getRegionId(), $request->getProduct());
  59. if(null == $domain)
  60. {
  61. throw new ClientException("Can not find endpoint to access.", "SDK.InvalidRegionId");
  62. }
  63. $requestUrl = $request->composeUrl($iSigner, $credential, $domain);
  64. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), null, $request->getHeaders());
  65. $retryTimes = 1;
  66. while (500 <= $httpResponse->getStatus() && $autoRetry && $retryTimes < $maxRetryNumber) {
  67. $requestUrl = $request->composeUrl($iSigner, $credential);
  68. $httpResponse = HttpHelper::curl($requestUrl, null, $request->getHeaders());
  69. $retryTimes ++;
  70. }
  71. return $httpResponse;
  72. }
  73. private function prepareRequest($request)
  74. {
  75. if(null == $request->getRegionId())
  76. {
  77. $request->setRegionId($this->iClientProfile->getRegionId());
  78. }
  79. if(null == $request->getAcceptFormat())
  80. {
  81. $request->setAcceptFormat($this->iClientProfile->getFormat());
  82. }
  83. if(null == $request->getMethod())
  84. {
  85. $request->setMethod("GET");
  86. }
  87. return $request;
  88. }
  89. private function buildApiException($respObject, $httpStatus)
  90. {
  91. if(500 <= $httpStatus)
  92. {
  93. throw new ServerException($respObject->Message, $respObject->Code);
  94. }
  95. else
  96. {
  97. throw new ClientException($respObject->Message, $respObject->Code);
  98. }
  99. }
  100. private function parseAcsResponse($body, $format)
  101. {
  102. if ("JSON" == $format)
  103. {
  104. $respObject = json_decode($body);
  105. }
  106. else if("XML" == $format)
  107. {
  108. $respObject = @simplexml_load_string($body);
  109. }
  110. else if("RAW" == $format)
  111. {
  112. $respObject = $body;
  113. }
  114. return $respObject;
  115. }
  116. }