Ei kuvausta

Client.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace AliyunMNS;
  3. use AliyunMNS\Queue;
  4. use AliyunMNS\Config;
  5. use AliyunMNS\Http\HttpClient;
  6. use AliyunMNS\AsyncCallback;
  7. use AliyunMNS\Model\AccountAttributes;
  8. use AliyunMNS\Requests\CreateQueueRequest;
  9. use AliyunMNS\Responses\CreateQueueResponse;
  10. use AliyunMNS\Requests\ListQueueRequest;
  11. use AliyunMNS\Responses\ListQueueResponse;
  12. use AliyunMNS\Requests\DeleteQueueRequest;
  13. use AliyunMNS\Responses\DeleteQueueResponse;
  14. use AliyunMNS\Requests\CreateTopicRequest;
  15. use AliyunMNS\Responses\CreateTopicResponse;
  16. use AliyunMNS\Requests\DeleteTopicRequest;
  17. use AliyunMNS\Responses\DeleteTopicResponse;
  18. use AliyunMNS\Requests\ListTopicRequest;
  19. use AliyunMNS\Responses\ListTopicResponse;
  20. use AliyunMNS\Requests\GetAccountAttributesRequest;
  21. use AliyunMNS\Responses\GetAccountAttributesResponse;
  22. use AliyunMNS\Requests\SetAccountAttributesRequest;
  23. use AliyunMNS\Responses\SetAccountAttributesResponse;
  24. /**
  25. * Please refer to
  26. * https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&queue_operation
  27. * for more details
  28. */
  29. class Client
  30. {
  31. private $client;
  32. /**
  33. * Please refer to http://www.aliyun.com/product/mns for more details
  34. *
  35. * @param endPoint: the host url
  36. * could be "http://$accountId.mns.cn-hangzhou.aliyuncs.com"
  37. * accountId could be found in aliyun.com
  38. * @param accessId: accessId from aliyun.com
  39. * @param accessKey: accessKey from aliyun.com
  40. * @param securityToken: securityToken from aliyun.com
  41. * @param config: necessary configs
  42. */
  43. public function __construct($endPoint, $accessId,
  44. $accessKey, $securityToken = NULL, Config $config = NULL)
  45. {
  46. $this->client = new HttpClient($endPoint, $accessId,
  47. $accessKey, $securityToken, $config);
  48. }
  49. /**
  50. * Returns a queue reference for operating on the queue
  51. * this function does not create the queue automatically.
  52. *
  53. * @param string $queueName: the queue name
  54. * @param bool $base64: whether the message in queue will be base64 encoded
  55. *
  56. * @return Queue $queue: the Queue instance
  57. */
  58. public function getQueueRef($queueName, $base64 = TRUE)
  59. {
  60. return new Queue($this->client, $queueName, $base64);
  61. }
  62. /**
  63. * Create Queue and Returns the Queue reference
  64. *
  65. * @param CreateQueueRequest $request: the QueueName and QueueAttributes
  66. *
  67. * @return CreateQueueResponse $response: the CreateQueueResponse
  68. *
  69. * @throws QueueAlreadyExistException if queue already exists
  70. * @throws InvalidArgumentException if any argument value is invalid
  71. * @throws MnsException if any other exception happends
  72. */
  73. public function createQueue(CreateQueueRequest $request)
  74. {
  75. $response = new CreateQueueResponse($request->getQueueName());
  76. return $this->client->sendRequest($request, $response);
  77. }
  78. /**
  79. * Create Queue and Returns the Queue reference
  80. * The request will not be sent until calling MnsPromise->wait();
  81. *
  82. * @param CreateQueueRequest $request: the QueueName and QueueAttributes
  83. * @param AsyncCallback $callback: the Callback when the request finishes
  84. *
  85. * @return MnsPromise $promise: the MnsPromise instance
  86. *
  87. * @throws MnsException if any exception happends
  88. */
  89. public function createQueueAsync(CreateQueueRequest $request,
  90. AsyncCallback $callback = NULL)
  91. {
  92. $response = new CreateQueueResponse($request->getQueueName());
  93. return $this->client->sendRequestAsync($request, $response, $callback);
  94. }
  95. /**
  96. * Query the queues created by current account
  97. *
  98. * @param ListQueueRequest $request: define filters for quering queues
  99. *
  100. * @return ListQueueResponse: the response containing queueNames
  101. */
  102. public function listQueue(ListQueueRequest $request)
  103. {
  104. $response = new ListQueueResponse();
  105. return $this->client->sendRequest($request, $response);
  106. }
  107. public function listQueueAsync(ListQueueRequest $request,
  108. AsyncCallback $callback = NULL)
  109. {
  110. $response = new ListQueueResponse();
  111. return $this->client->sendRequestAsync($request, $response, $callback);
  112. }
  113. /**
  114. * Delete the specified queue
  115. * the request will succeed even when the queue does not exist
  116. *
  117. * @param $queueName: the queueName
  118. *
  119. * @return DeleteQueueResponse
  120. */
  121. public function deleteQueue($queueName)
  122. {
  123. $request = new DeleteQueueRequest($queueName);
  124. $response = new DeleteQueueResponse();
  125. return $this->client->sendRequest($request, $response);
  126. }
  127. public function deleteQueueAsync($queueName,
  128. AsyncCallback $callback = NULL)
  129. {
  130. $request = new DeleteQueueRequest($queueName);
  131. $response = new DeleteQueueResponse();
  132. return $this->client->sendRequestAsync($request, $response, $callback);
  133. }
  134. // API for Topic
  135. /**
  136. * Returns a topic reference for operating on the topic
  137. * this function does not create the topic automatically.
  138. *
  139. * @param string $topicName: the topic name
  140. *
  141. * @return Topic $topic: the Topic instance
  142. */
  143. public function getTopicRef($topicName)
  144. {
  145. return new Topic($this->client, $topicName);
  146. }
  147. /**
  148. * Create Topic and Returns the Topic reference
  149. *
  150. * @param CreateTopicRequest $request: the TopicName and TopicAttributes
  151. *
  152. * @return CreateTopicResponse $response: the CreateTopicResponse
  153. *
  154. * @throws TopicAlreadyExistException if topic already exists
  155. * @throws InvalidArgumentException if any argument value is invalid
  156. * @throws MnsException if any other exception happends
  157. */
  158. public function createTopic(CreateTopicRequest $request)
  159. {
  160. $response = new CreateTopicResponse($request->getTopicName());
  161. return $this->client->sendRequest($request, $response);
  162. }
  163. /**
  164. * Delete the specified topic
  165. * the request will succeed even when the topic does not exist
  166. *
  167. * @param $topicName: the topicName
  168. *
  169. * @return DeleteTopicResponse
  170. */
  171. public function deleteTopic($topicName)
  172. {
  173. $request = new DeleteTopicRequest($topicName);
  174. $response = new DeleteTopicResponse();
  175. return $this->client->sendRequest($request, $response);
  176. }
  177. /**
  178. * Query the topics created by current account
  179. *
  180. * @param ListTopicRequest $request: define filters for quering topics
  181. *
  182. * @return ListTopicResponse: the response containing topicNames
  183. */
  184. public function listTopic(ListTopicRequest $request)
  185. {
  186. $response = new ListTopicResponse();
  187. return $this->client->sendRequest($request, $response);
  188. }
  189. /**
  190. * Query the AccountAttributes
  191. *
  192. * @return GetAccountAttributesResponse: the response containing topicNames
  193. * @throws MnsException if any exception happends
  194. */
  195. public function getAccountAttributes()
  196. {
  197. $request = new GetAccountAttributesRequest();
  198. $response = new GetAccountAttributesResponse();
  199. return $this->client->sendRequest($request, $response);
  200. }
  201. public function getAccountAttributesAsync(AsyncCallback $callback = NULL)
  202. {
  203. $request = new GetAccountAttributesRequest();
  204. $response = new GetAccountAttributesResponse();
  205. return $this->client->sendRequestAsync($request, $response, $callback);
  206. }
  207. /**
  208. * Set the AccountAttributes
  209. *
  210. * @param AccountAttributes $attributes: the AccountAttributes to set
  211. *
  212. * @return SetAccountAttributesResponse: the response
  213. *
  214. * @throws MnsException if any exception happends
  215. */
  216. public function setAccountAttributes(AccountAttributes $attributes)
  217. {
  218. $request = new SetAccountAttributesRequest($attributes);
  219. $response = new SetAccountAttributesResponse();
  220. return $this->client->sendRequest($request, $response);
  221. }
  222. public function setAccountAttributesAsync(AccountAttributes $attributes,
  223. AsyncCallback $callback = NULL)
  224. {
  225. $request = new SetAccountAttributesRequest($attributes);
  226. $response = new SetAccountAttributesResponse();
  227. return $this->client->sendRequestAsync($request, $response, $callback);
  228. }
  229. }
  230. ?>