AcsRequest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. abstract class AcsRequest
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $version;
  26. /**
  27. * @var string
  28. */
  29. protected $product;
  30. /**
  31. * @var string
  32. */
  33. protected $actionName;
  34. /**
  35. * @var string
  36. */
  37. protected $regionId;
  38. /**
  39. * @var string
  40. */
  41. protected $acceptFormat;
  42. /**
  43. * @var string
  44. */
  45. protected $method;
  46. /**
  47. * @var string
  48. */
  49. protected $requestScheme = 'http';
  50. /**
  51. * @var string
  52. */
  53. protected $content;
  54. /**
  55. * @var array
  56. */
  57. protected $queryParameters = array();
  58. /**
  59. * @var array
  60. */
  61. protected $headers = array();
  62. /**
  63. * @var null|string
  64. */
  65. protected $locationServiceCode;
  66. /**
  67. * @var null|string
  68. */
  69. protected $locationEndpointType;
  70. /**
  71. * @var array The original parameters of the request object.
  72. */
  73. protected $requestParameters = array();
  74. /**
  75. * @var string
  76. */
  77. protected $stringToBeSigned = '';
  78. /**
  79. * AcsRequest constructor.
  80. *
  81. * @param string $product
  82. * @param string $version
  83. * @param string $actionName
  84. * @param string|null $locationServiceCode
  85. * @param string $locationEndpointType
  86. */
  87. public function __construct($product,
  88. $version,
  89. $actionName,
  90. $locationServiceCode = null,
  91. $locationEndpointType = 'openAPI')
  92. {
  93. $this->headers['x-sdk-client'] = 'php/2.0.0';
  94. $this->product = $product;
  95. $this->version = $version;
  96. $this->actionName = $actionName;
  97. $this->locationServiceCode = $locationServiceCode;
  98. $this->locationEndpointType = $locationEndpointType;
  99. }
  100. /**
  101. * @param $iSigner
  102. * @param $credential
  103. * @param $domain
  104. *
  105. * @return mixed
  106. */
  107. abstract public function composeUrl($iSigner, $credential, $domain);
  108. /**
  109. * @return string
  110. */
  111. public function getVersion()
  112. {
  113. return $this->version;
  114. }
  115. /**
  116. * @param $version
  117. */
  118. public function setVersion($version)
  119. {
  120. $this->version = $version;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getProduct()
  126. {
  127. return $this->product;
  128. }
  129. /**
  130. * @param string $product
  131. */
  132. public function setProduct($product)
  133. {
  134. $this->product = $product;
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getActionName()
  140. {
  141. return $this->actionName;
  142. }
  143. /**
  144. * @param string $actionName
  145. */
  146. public function setActionName($actionName)
  147. {
  148. $this->actionName = $actionName;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getAcceptFormat()
  154. {
  155. return $this->acceptFormat;
  156. }
  157. /**
  158. * @param string $acceptFormat
  159. */
  160. public function setAcceptFormat($acceptFormat)
  161. {
  162. $this->acceptFormat = $acceptFormat;
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function getQueryParameters()
  168. {
  169. return $this->queryParameters;
  170. }
  171. /**
  172. * @return array
  173. */
  174. public function getHeaders()
  175. {
  176. return $this->headers;
  177. }
  178. /**
  179. * @return string
  180. */
  181. public function getMethod()
  182. {
  183. return $this->method;
  184. }
  185. /**
  186. * @param string $method
  187. */
  188. public function setMethod($method)
  189. {
  190. $this->method = $method;
  191. }
  192. /**
  193. * @return string
  194. */
  195. public function getProtocol()
  196. {
  197. return $this->requestScheme;
  198. }
  199. /**
  200. * @param string $protocol
  201. */
  202. public function setProtocol($protocol)
  203. {
  204. $this->requestScheme = $protocol;
  205. }
  206. /**
  207. * @return string
  208. */
  209. public function getRegionId()
  210. {
  211. return $this->regionId;
  212. }
  213. /**
  214. * @param string $region
  215. */
  216. public function setRegionId($region)
  217. {
  218. $this->regionId = $region;
  219. }
  220. /**
  221. * @return string
  222. */
  223. public function getContent()
  224. {
  225. return $this->content;
  226. }
  227. /**
  228. * @param string $content
  229. */
  230. public function setContent($content)
  231. {
  232. $this->content = $content;
  233. }
  234. /**
  235. * @param string $headerKey
  236. * @param mixed $headerValue
  237. */
  238. public function addHeader($headerKey, $headerValue)
  239. {
  240. $this->headers[$headerKey] = $headerValue;
  241. }
  242. /**
  243. * @return null|string
  244. */
  245. public function getLocationServiceCode()
  246. {
  247. return $this->locationServiceCode;
  248. }
  249. /**
  250. * @return null|string
  251. */
  252. public function getLocationEndpointType()
  253. {
  254. return $this->locationEndpointType;
  255. }
  256. /**
  257. * Magic method for get parameters.
  258. *
  259. * @param string $name
  260. * @param mixed $arguments
  261. *
  262. * @return $this
  263. */
  264. public function __call($name, $arguments)
  265. {
  266. if (\strpos($name, 'get', 0) !== false) {
  267. $parameterName = $this->propertyNameByMethodName($name);
  268. return isset($this->requestParameters[$parameterName])
  269. ? $this->requestParameters[$parameterName]
  270. : null;
  271. }
  272. return $this;
  273. }
  274. /**
  275. * @param string $methodName
  276. *
  277. * @return string
  278. */
  279. protected function propertyNameByMethodName($methodName)
  280. {
  281. return \mb_strcut($methodName, 3);
  282. }
  283. /**
  284. * @return string
  285. */
  286. public function stringToBeSigned()
  287. {
  288. return $this->stringToBeSigned;
  289. }
  290. }