Version.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Twilio;
  3. use Twilio\Exceptions\RestException;
  4. use Twilio\Exceptions\TwilioException;
  5. use Twilio\Http\Response;
  6. abstract class Version {
  7. /**
  8. * @const int MAX_PAGE_SIZE largest page the Twilio API will return
  9. */
  10. const MAX_PAGE_SIZE = 1000;
  11. /**
  12. * @var \Twilio\Domain $domain
  13. */
  14. protected $domain;
  15. /**
  16. * @var string $version
  17. */
  18. protected $version;
  19. /**
  20. * @param \Twilio\Domain $domain
  21. */
  22. public function __construct(Domain $domain) {
  23. $this->domain = $domain;
  24. $this->version = null;
  25. }
  26. /**
  27. * Generate an absolute URL from a version relative uri
  28. * @param string $uri Version relative uri
  29. * @return string Absolute URL
  30. */
  31. public function absoluteUrl($uri) {
  32. return $this->getDomain()->absoluteUrl($this->relativeUri($uri));
  33. }
  34. /**
  35. * Generate a domain relative uri from a version relative uri
  36. * @param string $uri Version relative uri
  37. * @return string Domain relative uri
  38. */
  39. public function relativeUri($uri) {
  40. return trim($this->version, '/') . '/' . trim($uri, '/');
  41. }
  42. public function request($method, $uri, $params = array(), $data = array(),
  43. $headers = array(), $username = null,
  44. $password = null, $timeout = null) {
  45. $uri = $this->relativeUri($uri);
  46. return $this->getDomain()->request(
  47. $method,
  48. $uri,
  49. $params,
  50. $data,
  51. $headers,
  52. $username,
  53. $password,
  54. $timeout
  55. );
  56. }
  57. /**
  58. * Create the best possible exception for the response.
  59. *
  60. * Attempts to parse the response for Twilio Standard error messages and use
  61. * those to populate the exception, falls back to generic error message and
  62. * HTTP status code.
  63. *
  64. * @param Response $response Error response
  65. * @param string $header Header for exception message
  66. * @return TwilioException
  67. */
  68. protected function exception($response, $header) {
  69. $message = '[HTTP ' . $response->getStatusCode() . '] ' . $header;
  70. $content = $response->getContent();
  71. if (is_array($content)) {
  72. $message .= isset($content['message']) ? ': ' . $content['message'] : '';
  73. $code = isset($content['code']) ? $content['code'] : $response->getStatusCode();
  74. return new RestException($message, $code, $response->getStatusCode());
  75. } else {
  76. return new RestException($message, $response->getStatusCode(), $response->getStatusCode());
  77. }
  78. }
  79. /**
  80. * @throws TwilioException
  81. */
  82. public function fetch($method, $uri, $params = array(), $data = array(),
  83. $headers = array(), $username = null,
  84. $password = null, $timeout = null) {
  85. $response = $this->request(
  86. $method,
  87. $uri,
  88. $params,
  89. $data,
  90. $headers,
  91. $username,
  92. $password,
  93. $timeout
  94. );
  95. if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
  96. throw $this->exception($response, 'Unable to fetch record');
  97. }
  98. return $response->getContent();
  99. }
  100. /**
  101. * @throws TwilioException
  102. */
  103. public function update($method, $uri, $params = array(), $data = array(),
  104. $headers = array(), $username = null,
  105. $password = null, $timeout = null) {
  106. $response = $this->request(
  107. $method,
  108. $uri,
  109. $params,
  110. $data,
  111. $headers,
  112. $username,
  113. $password,
  114. $timeout
  115. );
  116. if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
  117. throw $this->exception($response, 'Unable to update record');
  118. }
  119. return $response->getContent();
  120. }
  121. /**
  122. * @throws TwilioException
  123. */
  124. public function delete($method, $uri, $params = array(), $data = array(),
  125. $headers = array(), $username = null,
  126. $password = null, $timeout = null) {
  127. $response = $this->request(
  128. $method,
  129. $uri,
  130. $params,
  131. $data,
  132. $headers,
  133. $username,
  134. $password,
  135. $timeout
  136. );
  137. if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
  138. throw $this->exception($response, 'Unable to delete record');
  139. }
  140. return $response->getStatusCode() == 204;
  141. }
  142. public function readLimits($limit = null, $pageSize = null) {
  143. $pageLimit = Values::NONE;
  144. if ($limit) {
  145. if (is_null($pageSize)) {
  146. $pageSize = min($limit, self::MAX_PAGE_SIZE);
  147. }
  148. $pageLimit = (int)(ceil($limit / (float)$pageSize));
  149. }
  150. $pageSize = min($pageSize, self::MAX_PAGE_SIZE);
  151. return array(
  152. 'limit' => $limit ?: Values::NONE,
  153. 'pageSize' => $pageSize ?: Values::NONE,
  154. 'pageLimit' => $pageLimit,
  155. );
  156. }
  157. public function page($method, $uri, $params = array(), $data = array(),
  158. $headers = array(), $username = null,
  159. $password = null, $timeout = null) {
  160. return $this->request(
  161. $method,
  162. $uri,
  163. $params,
  164. $data,
  165. $headers,
  166. $username,
  167. $password,
  168. $timeout
  169. );
  170. }
  171. public function stream($page, $limit = null, $pageLimit = null) {
  172. return new Stream($page, $limit, $pageLimit);
  173. }
  174. /**
  175. * @throws TwilioException
  176. */
  177. public function create($method, $uri, $params = array(), $data = array(),
  178. $headers = array(), $username = null,
  179. $password = null, $timeout = null) {
  180. $response = $this->request(
  181. $method,
  182. $uri,
  183. $params,
  184. $data,
  185. $headers,
  186. $username,
  187. $password,
  188. $timeout
  189. );
  190. if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
  191. throw $this->exception($response, 'Unable to create record');
  192. }
  193. return $response->getContent();
  194. }
  195. /**
  196. * @return \Twilio\Domain $domain
  197. */
  198. public function getDomain() {
  199. return $this->domain;
  200. }
  201. public function __toString() {
  202. return '[Version]';
  203. }
  204. }