No Description

HttpClient.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace AliyunMNS\Http;
  3. use AliyunMNS\Config;
  4. use AliyunMNS\Constants;
  5. use AliyunMNS\Exception\MnsException;
  6. use AliyunMNS\Requests\BaseRequest;
  7. use AliyunMNS\Responses\BaseResponse;
  8. use AliyunMNS\Signature\Signature;
  9. use AliyunMNS\AsyncCallback;
  10. use Psr\Http\Message\ResponseInterface;
  11. use GuzzleHttp\Psr7\Request;
  12. use GuzzleHttp\Exception\TransferException;
  13. use AliyunMNS\Responses\MnsPromise;
  14. class HttpClient
  15. {
  16. private $client;
  17. private $region;
  18. private $accountId;
  19. private $accessId;
  20. private $accessKey;
  21. private $securityToken;
  22. private $requestTimeout;
  23. private $connectTimeout;
  24. public function __construct($endPoint, $accessId,
  25. $accessKey, $securityToken = NULL, Config $config = NULL)
  26. {
  27. if ($config == NULL)
  28. {
  29. $config = new Config;
  30. }
  31. $this->accessId = $accessId;
  32. $this->accessKey = $accessKey;
  33. $this->client = new \GuzzleHttp\Client([
  34. 'base_uri' => $endPoint,
  35. 'defaults' => [
  36. 'headers' => [
  37. 'Host' => $endPoint
  38. ],
  39. 'proxy' => $config->getProxy(),
  40. 'expect' => $config->getExpectContinue()
  41. ]
  42. ]);
  43. $this->requestTimeout = $config->getRequestTimeout();
  44. $this->connectTimeout = $config->getConnectTimeout();
  45. $this->securityToken = $securityToken;
  46. $this->endpoint = $endPoint;
  47. $this->parseEndpoint();
  48. }
  49. public function getRegion()
  50. {
  51. return $this->region;
  52. }
  53. public function getAccountId()
  54. {
  55. return $this->accountId;
  56. }
  57. // This function is for SDK internal use
  58. private function parseEndpoint()
  59. {
  60. $pieces = explode("//", $this->endpoint);
  61. $host = end($pieces);
  62. $host_pieces = explode(".", $host);
  63. $this->accountId = $host_pieces[0];
  64. $region_pieces = explode("-internal", $host_pieces[2]);
  65. $this->region = $region_pieces[0];
  66. }
  67. private function addRequiredHeaders(BaseRequest &$request)
  68. {
  69. $body = $request->generateBody();
  70. $queryString = $request->generateQueryString();
  71. $request->setBody($body);
  72. $request->setQueryString($queryString);
  73. if ($body != NULL)
  74. {
  75. $request->setHeader(Constants::CONTENT_LENGTH, strlen($body));
  76. }
  77. $request->setHeader('Date', gmdate(Constants::GMT_DATE_FORMAT));
  78. if (!$request->isHeaderSet(Constants::CONTENT_TYPE))
  79. {
  80. $request->setHeader(Constants::CONTENT_TYPE, 'text/xml');
  81. }
  82. $request->setHeader(Constants::MNS_VERSION_HEADER, Constants::MNS_VERSION);
  83. if ($this->securityToken != NULL)
  84. {
  85. $request->setHeader(Constants::SECURITY_TOKEN, $this->securityToken);
  86. }
  87. $sign = Signature::SignRequest($this->accessKey, $request);
  88. $request->setHeader(Constants::AUTHORIZATION,
  89. Constants::MNS . " " . $this->accessId . ":" . $sign);
  90. }
  91. public function sendRequestAsync(BaseRequest $request,
  92. BaseResponse &$response, AsyncCallback $callback = NULL)
  93. {
  94. $promise = $this->sendRequestAsyncInternal($request, $response, $callback);
  95. return new MnsPromise($promise, $response);
  96. }
  97. public function sendRequest(BaseRequest $request, BaseResponse &$response)
  98. {
  99. $promise = $this->sendRequestAsync($request, $response);
  100. return $promise->wait();
  101. }
  102. private function sendRequestAsyncInternal(BaseRequest &$request, BaseResponse &$response, AsyncCallback $callback = NULL)
  103. {
  104. $this->addRequiredHeaders($request);
  105. $parameters = array('exceptions' => false, 'http_errors' => false);
  106. $queryString = $request->getQueryString();
  107. $body = $request->getBody();
  108. if ($queryString != NULL) {
  109. $parameters['query'] = $queryString;
  110. }
  111. if ($body != NULL) {
  112. $parameters['body'] = $body;
  113. }
  114. $parameters['timeout'] = $this->requestTimeout;
  115. $parameters['connect_timeout'] = $this->connectTimeout;
  116. $request = new Request(strtoupper($request->getMethod()),
  117. $request->getResourcePath(), $request->getHeaders());
  118. try
  119. {
  120. if ($callback != NULL)
  121. {
  122. return $this->client->sendAsync($request, $parameters)->then(
  123. function ($res) use (&$response, $callback) {
  124. try {
  125. $response->parseResponse($res->getStatusCode(), $res->getBody());
  126. $callback->onSucceed($response);
  127. } catch (MnsException $e) {
  128. $callback->onFailed($e);
  129. }
  130. }
  131. );
  132. }
  133. else
  134. {
  135. return $this->client->sendAsync($request, $parameters);
  136. }
  137. }
  138. catch (TransferException $e)
  139. {
  140. $message = $e->getMessage();
  141. if ($e->hasResponse()) {
  142. $message = $e->getResponse()->getBody();
  143. }
  144. throw new MnsException($e->getCode(), $message, $e);
  145. }
  146. }
  147. }
  148. ?>