DefaultAcsClient.php 5.0 KB

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