DefaultAcsClient.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. function __construct($iClientProfile)
  24. {
  25. $this->iClientProfile = $iClientProfile;
  26. }
  27. public function getAcsResponse($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  28. {
  29. $httpResponse = $this->doAction($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  30. $respObject = $this->parseAcsResponse($httpResponse->getBody(), $request->getAcceptFormat());
  31. if(false == $httpResponse->isSuccess())
  32. {
  33. $this->buildApiException($respObject, $httpResponse->getStatus());
  34. }
  35. return $respObject;
  36. }
  37. public function doAction($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  38. {
  39. if(null == $this->iClientProfile && (null == $iSigner || null == $credential
  40. || null == $request->getRegionId() || null == $request->getAcceptFormat()))
  41. {
  42. throw new ClientException("No active profile found.", "SDK.InvalidProfile");
  43. }
  44. if(null == $iSigner)
  45. {
  46. $iSigner = $this->iClientProfile->getSigner();
  47. }
  48. if(null == $credential)
  49. {
  50. $credential = $this->iClientProfile->getCredential();
  51. }
  52. $request = $this->prepareRequest($request);
  53. $domain = EndpointProvider::findProductDomain($request->getRegionId(), $request->getProduct());
  54. if(null == $domain)
  55. {
  56. throw new ClientException("Can not find endpoint to access.", "SDK.InvalidRegionId");
  57. }
  58. $requestUrl = $request->composeUrl($iSigner, $credential, $domain);
  59. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), null, $request->getHeaders());
  60. $retryTimes = 1;
  61. while (500 <= $httpResponse->getStatus() && $autoRetry && $retryTimes < $maxRetryNumber) {
  62. $requestUrl = $request->composeUrl($iSigner, $credential);
  63. $httpResponse = HttpHelper::curl($requestUrl, null, $request->getHeaders());
  64. $retryTimes ++;
  65. }
  66. return $httpResponse;
  67. }
  68. private function prepareRequest($request)
  69. {
  70. if(null == $request->getRegionId())
  71. {
  72. $request->setRegionId($this->iClientProfile->getRegionId());
  73. }
  74. if(null == $request->getAcceptFormat())
  75. {
  76. $request->setAcceptFormat($this->iClientProfile->getFormat());
  77. }
  78. if(null == $request->getMethod())
  79. {
  80. $request->setMethod("GET");
  81. }
  82. return $request;
  83. }
  84. private function buildApiException($respObject, $httpStatus)
  85. {
  86. if(500 <= $httpStatus)
  87. {
  88. throw new ServerException($respObject->Message, $respObject->Code);
  89. }
  90. else
  91. {
  92. throw new ClientException($respObject->Message, $respObject->Code);
  93. }
  94. }
  95. private function parseAcsResponse($body, $format)
  96. {
  97. if ("JSON" == $format)
  98. {
  99. $respObject = json_decode($body);
  100. }
  101. else if("XML" == $format)
  102. {
  103. $respObject = @simplexml_load_string($body);
  104. }
  105. else if("RAW" == $format)
  106. {
  107. $respObject = $body;
  108. }
  109. return $respObject;
  110. }
  111. }