Security update for permissions_by_term
[yaffs-website] / vendor / instaclick / php-webdriver / lib / WebDriver / SauceLabs / SauceRest.php
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
23 namespace WebDriver\SauceLabs;
24
25 use WebDriver\ServiceFactory;
26
27 /**
28  * WebDriver\SauceLabs\SauceRest class
29  *
30  * @package WebDriver
31  */
32 class SauceRest
33 {
34     /**
35      * @var string
36      */
37     private $userId;
38
39     /**
40      * @var string
41      */
42     private $accessKey;
43
44     /**
45      * Constructor
46      *
47      * @param string $userId    Your Sauce user name
48      * @param string $accessKey Your Sauce API key
49      */
50     public function __construct($userId, $accessKey)
51     {
52         $this->userId = $userId;
53         $this->accessKey = $accessKey;
54     }
55
56     /**
57      * Execute Sauce Labs REST API command
58      *
59      * @param string $requestMethod HTTP request method
60      * @param string $url           URL
61      * @param mixed  $parameters    Parameters
62      *
63      * @return mixed
64      *
65      * @throws \WebDriver\Exception\CurlExec
66      *
67      * @see http://saucelabs.com/docs/saucerest
68      */
69     protected function execute($requestMethod, $url, $parameters = null)
70     {
71         $extraOptions = array(
72             CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
73             CURLOPT_USERPWD => $this->userId . ':' . $this->accessKey,
74
75             // don't verify SSL certificates
76             CURLOPT_SSL_VERIFYPEER => false,
77             CURLOPT_SSL_VERIFYHOST => false,
78
79             CURLOPT_HTTPHEADER => array('Expect:'),
80             CURLOPT_FAILONERROR => true,
81         );
82
83         $url = 'https://saucelabs.com/rest/v1/' . $url;
84
85         list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);
86
87         return json_decode($rawResult, true);
88     }
89
90     /**
91      * Get account details: /rest/v1/users/:userId (GET)
92      *
93      * @param string $userId
94      *
95      * @return array
96      */
97     public function getAccountDetails($userId)
98     {
99         return $this->execute('GET', 'users/' . $userId);
100     }
101
102     /**
103      * Check account limits: /rest/v1/limits (GET)
104      *
105      * @return array
106      */
107     public function getAccountLimits()
108     {
109         return $this->execute('GET', 'limits');
110     }
111
112     /**
113      * Create new sub-account: /rest/v1/users/:userId (POST)
114      *
115      * For "partners", $accountInfo also contains 'plan' => (one of 'free', 'small', 'team', 'com', or 'complus')
116      *
117      * @param array $accountInfo array('username' => ..., 'password' => ..., 'name' => ..., 'email' => ...)
118      *
119      * @return array array('access_key' => ..., 'minutes' => ..., 'id' => ...)
120      */
121     public function createSubAccount($accountInfo)
122     {
123         return $this->execute('POST', 'users/' . $this->userId, $accountInfo);
124     }
125
126     /**
127      * Update sub-account service plan: /rest/v1/users/:userId/subscription (POST)
128      *
129      * @param string $userId User ID
130      * @param string $plan   Plan
131      *
132      * @return array
133      */
134     public function updateSubAccount($userId, $plan)
135     {
136         return $this->execute('POST', 'users/' . $userId . '/subscription', array('plan' => $plan));
137     }
138
139     /**
140      * Unsubscribe a sub-account: /rest/v1/users/:userId/subscription (DELETE)
141      *
142      * @param string $userId User ID
143      *
144      * @return array
145      */
146     public function unsubscribeSubAccount($userId)
147     {
148         return $this->execute('DELETE', 'users/' . $userId . '/subscription');
149     }
150
151     /**
152      * Get current account activity: /rest/v1/:userId/activity (GET)
153      *
154      * @return array
155      */
156     public function getActivity()
157     {
158         return $this->execute('GET', $this->userId . '/activity');
159     }
160
161     /**
162      * Get historical account usage: /rest/v1/:userId/usage (GET)
163      *
164      * @param string $start Optional start date YYYY-MM-DD
165      * @param string $end   Optional end date YYYY-MM-DD
166      *
167      * @return array
168      */
169     public function getUsage($start = null, $end = null)
170     {
171         $query = http_build_query(array(
172             'start' => $start,
173             'end' => $end,
174         ));
175
176         return $this->execute('GET', $this->userId . '/usage' . (strlen($query) ? '?' . $query : ''));
177     }
178
179     /**
180      * Get jobs: /rest/v1/:userId/jobs (GET)
181      *
182      * @param boolean $full
183      *
184      * @return array
185      */
186     public function getJobs($full = null)
187     {
188         $query = http_build_query(array(
189             'full' => (isset($full) && $full) ? 'true' : null,
190         ));
191
192         return $this->execute('GET', $this->userId . '/jobs' . (strlen($query) ? '?' . $query : ''));
193     }
194
195     /**
196      * Get full information for job: /rest/v1/:userId/jobs/:jobId (GET)
197      *
198      * @param string $jobId
199      *
200      * @return array
201      */
202     public function getJob($jobId)
203     {
204         return $this->execute('GET', $this->userId . '/jobs/' . $jobId);
205     }
206
207     /**
208      * Update existing job: /rest/v1/:userId/jobs/:jobId (PUT)
209      *
210      * @param string $jobId   Job ID
211      * @param array  $jobInfo Job information
212      *
213      * @return array
214      */
215     public function updateJob($jobId, $jobInfo)
216     {
217         return $this->execute('PUT', $this->userId . '/jobs/' . $jobId, $jobInfo);
218     }
219
220     /**
221      * Stop job: /rest/v1/:userId/jobs/:jobId/stop (PUT)
222      *
223      * @param string $jobId
224      *
225      * @return array
226      */
227     public function stopJob($jobId)
228     {
229         return $this->execute('PUT', $this->userId . '/jobs/' . $jobId . '/stop');
230     }
231
232     /**
233      * Delete job: /rest/v1/:userId/jobs/:jobId (DELETE)
234      *
235      * @param string $jobId
236      *
237      * @return array
238      */
239     public function deleteJob($jobId)
240     {
241         return $this->execute('DELETE', $this->userId . '/jobs/' . $jobId);
242     }
243
244     /**
245      * Get running tunnels for a given user: /rest/v1/:userId/tunnels (GET)
246      *
247      * @return array
248      */
249     public function getTunnels()
250     {
251         return $this->execute('GET', $this->userId . '/tunnels');
252     }
253
254     /**
255      * Get full information for a tunnel: /rest/v1/:userId/tunnels/:tunnelId (GET)
256      *
257      * @param string $tunnelId
258      *
259      * @return array
260      */
261     public function getTunnel($tunnelId)
262     {
263         return $this->execute('GET', $this->userId . '/tunnels/' . $tunnelId);
264     }
265
266     /**
267      * Shut down a tunnel: /rest/v1/:userId/tunnels/:tunnelId (DELETE)
268      *
269      * @param string $tunnelId
270      *
271      * @return array
272      */
273     public function shutdownTunnel($tunnelId)
274     {
275         return $this->execute('DELETE', $this->userId . '/tunnels/' . $tunnelId);
276     }
277
278     /**
279      * Get current status of Sauce Labs' services: /rest/v1/info/status (GET)
280      *
281      * @return array array('wait_time' => ..., 'service_operational' => ..., 'status_message' => ...)
282      */
283     public function getStatus()
284     {
285         return $this->execute('GET', 'info/status');
286     }
287
288     /**
289      * Get currently supported browsers: /rest/v1/info/browsers (GET)
290      *
291      * @param string $termination Optional termination (one of "all", "selenium-rc", or "webdriver')
292      *
293      * @return array
294      */
295     public function getBrowsers($termination = false)
296     {
297         if ($termination) {
298             return $this->execute('GET', 'info/browsers/' . $termination);
299         }
300
301         return $this->execute('GET', 'info/browsers');
302     }
303
304     /**
305      * Get number of tests executed so far on Sauce Labs: /rest/v1/info/counter (GET)
306      *
307      * @return array
308      */
309     public function getCounter()
310     {
311         return $this->execute('GET', 'info/counter');
312     }
313 }