SauceRest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Copyright 2012-2017 Anthon Pang. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * @package WebDriver
  18. *
  19. * @author Anthon Pang <apang@softwaredevelopment.ca>
  20. * @author Fabrizio Branca <mail@fabrizio-branca.de>
  21. */
  22. namespace WebDriver\SauceLabs;
  23. use WebDriver\ServiceFactory;
  24. /**
  25. * WebDriver\SauceLabs\SauceRest class
  26. *
  27. * @package WebDriver
  28. */
  29. class SauceRest
  30. {
  31. /**
  32. * @var string
  33. */
  34. private $userId;
  35. /**
  36. * @var string
  37. */
  38. private $accessKey;
  39. /**
  40. * Constructor
  41. *
  42. * @param string $userId Your Sauce user name
  43. * @param string $accessKey Your Sauce API key
  44. */
  45. public function __construct($userId, $accessKey)
  46. {
  47. $this->userId = $userId;
  48. $this->accessKey = $accessKey;
  49. }
  50. /**
  51. * Execute Sauce Labs REST API command
  52. *
  53. * @param string $requestMethod HTTP request method
  54. * @param string $url URL
  55. * @param mixed $parameters Parameters
  56. *
  57. * @return mixed
  58. *
  59. * @throws \WebDriver\Exception\CurlExec
  60. *
  61. * @see http://saucelabs.com/docs/saucerest
  62. */
  63. protected function execute($requestMethod, $url, $parameters = null)
  64. {
  65. $extraOptions = array(
  66. CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
  67. CURLOPT_USERPWD => $this->userId . ':' . $this->accessKey,
  68. // don't verify SSL certificates
  69. CURLOPT_SSL_VERIFYPEER => false,
  70. CURLOPT_SSL_VERIFYHOST => false,
  71. CURLOPT_HTTPHEADER => array('Expect:'),
  72. CURLOPT_FAILONERROR => true,
  73. );
  74. $url = 'https://saucelabs.com/rest/v1/' . $url;
  75. list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);
  76. return json_decode($rawResult, true);
  77. }
  78. /**
  79. * Get account details: /rest/v1/users/:userId (GET)
  80. *
  81. * @param string $userId
  82. *
  83. * @return array
  84. */
  85. public function getAccountDetails($userId)
  86. {
  87. return $this->execute('GET', 'users/' . $userId);
  88. }
  89. /**
  90. * Check account limits: /rest/v1/limits (GET)
  91. *
  92. * @return array
  93. */
  94. public function getAccountLimits()
  95. {
  96. return $this->execute('GET', 'limits');
  97. }
  98. /**
  99. * Create new sub-account: /rest/v1/users/:userId (POST)
  100. *
  101. * For "partners", $accountInfo also contains 'plan' => (one of 'free', 'small', 'team', 'com', or 'complus')
  102. *
  103. * @param array $accountInfo array('username' => ..., 'password' => ..., 'name' => ..., 'email' => ...)
  104. *
  105. * @return array array('access_key' => ..., 'minutes' => ..., 'id' => ...)
  106. */
  107. public function createSubAccount($accountInfo)
  108. {
  109. return $this->execute('POST', 'users/' . $this->userId, $accountInfo);
  110. }
  111. /**
  112. * Update sub-account service plan: /rest/v1/users/:userId/subscription (POST)
  113. *
  114. * @param string $userId User ID
  115. * @param string $plan Plan
  116. *
  117. * @return array
  118. */
  119. public function updateSubAccount($userId, $plan)
  120. {
  121. return $this->execute('POST', 'users/' . $userId . '/subscription', array('plan' => $plan));
  122. }
  123. /**
  124. * Unsubscribe a sub-account: /rest/v1/users/:userId/subscription (DELETE)
  125. *
  126. * @param string $userId User ID
  127. *
  128. * @return array
  129. */
  130. public function unsubscribeSubAccount($userId)
  131. {
  132. return $this->execute('DELETE', 'users/' . $userId . '/subscription');
  133. }
  134. /**
  135. * Get current account activity: /rest/v1/:userId/activity (GET)
  136. *
  137. * @return array
  138. */
  139. public function getActivity()
  140. {
  141. return $this->execute('GET', $this->userId . '/activity');
  142. }
  143. /**
  144. * Get historical account usage: /rest/v1/:userId/usage (GET)
  145. *
  146. * @param string $start Optional start date YYYY-MM-DD
  147. * @param string $end Optional end date YYYY-MM-DD
  148. *
  149. * @return array
  150. */
  151. public function getUsage($start = null, $end = null)
  152. {
  153. $query = http_build_query(array(
  154. 'start' => $start,
  155. 'end' => $end,
  156. ));
  157. return $this->execute('GET', $this->userId . '/usage' . (strlen($query) ? '?' . $query : ''));
  158. }
  159. /**
  160. * Get jobs: /rest/v1/:userId/jobs (GET)
  161. *
  162. * @param boolean $full
  163. *
  164. * @return array
  165. */
  166. public function getJobs($full = null)
  167. {
  168. $query = http_build_query(array(
  169. 'full' => (isset($full) && $full) ? 'true' : null,
  170. ));
  171. return $this->execute('GET', $this->userId . '/jobs' . (strlen($query) ? '?' . $query : ''));
  172. }
  173. /**
  174. * Get full information for job: /rest/v1/:userId/jobs/:jobId (GET)
  175. *
  176. * @param string $jobId
  177. *
  178. * @return array
  179. */
  180. public function getJob($jobId)
  181. {
  182. return $this->execute('GET', $this->userId . '/jobs/' . $jobId);
  183. }
  184. /**
  185. * Update existing job: /rest/v1/:userId/jobs/:jobId (PUT)
  186. *
  187. * @param string $jobId Job ID
  188. * @param array $jobInfo Job information
  189. *
  190. * @return array
  191. */
  192. public function updateJob($jobId, $jobInfo)
  193. {
  194. return $this->execute('PUT', $this->userId . '/jobs/' . $jobId, $jobInfo);
  195. }
  196. /**
  197. * Stop job: /rest/v1/:userId/jobs/:jobId/stop (PUT)
  198. *
  199. * @param string $jobId
  200. *
  201. * @return array
  202. */
  203. public function stopJob($jobId)
  204. {
  205. return $this->execute('PUT', $this->userId . '/jobs/' . $jobId . '/stop');
  206. }
  207. /**
  208. * Delete job: /rest/v1/:userId/jobs/:jobId (DELETE)
  209. *
  210. * @param string $jobId
  211. *
  212. * @return array
  213. */
  214. public function deleteJob($jobId)
  215. {
  216. return $this->execute('DELETE', $this->userId . '/jobs/' . $jobId);
  217. }
  218. /**
  219. * Get running tunnels for a given user: /rest/v1/:userId/tunnels (GET)
  220. *
  221. * @return array
  222. */
  223. public function getTunnels()
  224. {
  225. return $this->execute('GET', $this->userId . '/tunnels');
  226. }
  227. /**
  228. * Get full information for a tunnel: /rest/v1/:userId/tunnels/:tunnelId (GET)
  229. *
  230. * @param string $tunnelId
  231. *
  232. * @return array
  233. */
  234. public function getTunnel($tunnelId)
  235. {
  236. return $this->execute('GET', $this->userId . '/tunnels/' . $tunnelId);
  237. }
  238. /**
  239. * Shut down a tunnel: /rest/v1/:userId/tunnels/:tunnelId (DELETE)
  240. *
  241. * @param string $tunnelId
  242. *
  243. * @return array
  244. */
  245. public function shutdownTunnel($tunnelId)
  246. {
  247. return $this->execute('DELETE', $this->userId . '/tunnels/' . $tunnelId);
  248. }
  249. /**
  250. * Get current status of Sauce Labs' services: /rest/v1/info/status (GET)
  251. *
  252. * @return array array('wait_time' => ..., 'service_operational' => ..., 'status_message' => ...)
  253. */
  254. public function getStatus()
  255. {
  256. return $this->execute('GET', 'info/status');
  257. }
  258. /**
  259. * Get currently supported browsers: /rest/v1/info/browsers (GET)
  260. *
  261. * @param string $termination Optional termination (one of "all", "selenium-rc", or "webdriver')
  262. *
  263. * @return array
  264. */
  265. public function getBrowsers($termination = false)
  266. {
  267. if ($termination) {
  268. return $this->execute('GET', 'info/browsers/' . $termination);
  269. }
  270. return $this->execute('GET', 'info/browsers');
  271. }
  272. /**
  273. * Get number of tests executed so far on Sauce Labs: /rest/v1/info/counter (GET)
  274. *
  275. * @return array
  276. */
  277. public function getCounter()
  278. {
  279. return $this->execute('GET', 'info/counter');
  280. }
  281. }