|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TestingBot; |
| 4 | + |
| 5 | +class TestingBotAPI |
| 6 | +{ |
| 7 | + private $_key; |
| 8 | + private $_secret; |
| 9 | + private $_options = array(); |
| 10 | + |
| 11 | + public function __construct($key, $secret, array $options = array()) |
| 12 | + { |
| 13 | + $this->_key = $key; |
| 14 | + $this->_secret = $secret; |
| 15 | + |
| 16 | + if (empty($key)) |
| 17 | + { |
| 18 | + throw new \Exception("Key is required to use the TestingBot API"); |
| 19 | + } |
| 20 | + |
| 21 | + if (empty($secret)) |
| 22 | + { |
| 23 | + throw new \Exception("Secret is required to use the TestingBot API"); |
| 24 | + } |
| 25 | + |
| 26 | + $this->_options = $options; |
| 27 | + } |
| 28 | + |
| 29 | + public function updateJob($sessionID, array $details) |
| 30 | + { |
| 31 | + if (empty($sessionID)) |
| 32 | + { |
| 33 | + throw new \Exception("Please use a valid WebDriver sessionID"); |
| 34 | + } |
| 35 | + |
| 36 | + $data = array(); |
| 37 | + |
| 38 | + foreach ($details as $k => $v) |
| 39 | + { |
| 40 | + $data['test[' . $k . ']'] = $v; |
| 41 | + } |
| 42 | + |
| 43 | + return $this->_doRequest("tests/" . $sessionID, "PUT", $data); |
| 44 | + } |
| 45 | + |
| 46 | + public function getJob($sessionID, array $details = array()) |
| 47 | + { |
| 48 | + if (empty($sessionID)) |
| 49 | + { |
| 50 | + throw new \Exception("Please use a valid WebDriver sessionID"); |
| 51 | + } |
| 52 | + |
| 53 | + $extra = ""; |
| 54 | + if (!empty($details)) |
| 55 | + { |
| 56 | + $extra = "?" . http_build_query($details); |
| 57 | + } |
| 58 | + return $this->_doRequest("tests/" . $sessionID . $extra, "GET"); |
| 59 | + } |
| 60 | + |
| 61 | + public function getJobs($offset = 0, $count = 10, array $extraOptions = array()) |
| 62 | + { |
| 63 | + $queryData = array( |
| 64 | + 'offset' => $offset, |
| 65 | + 'count' => $count |
| 66 | + ); |
| 67 | + |
| 68 | + $queryData = array_merge($queryData, $extraOptions); |
| 69 | + |
| 70 | + return $this->_doRequest("tests/?" . http_build_query($queryData), "GET"); |
| 71 | + } |
| 72 | + |
| 73 | + public function deleteJob($sessionID) |
| 74 | + { |
| 75 | + if (empty($sessionID)) |
| 76 | + { |
| 77 | + throw new \Exception("Please use a valid WebDriver sessionID"); |
| 78 | + } |
| 79 | + |
| 80 | + return $this->_doRequest("tests/" . $sessionID, "DELETE"); |
| 81 | + } |
| 82 | + |
| 83 | + public function stopJob($sessionID) |
| 84 | + { |
| 85 | + if (empty($sessionID)) |
| 86 | + { |
| 87 | + throw new \Exception("Please use a valid WebDriver sessionID"); |
| 88 | + } |
| 89 | + |
| 90 | + return $this->_doRequest("tests/" . $sessionID . "/stop", "PUT"); |
| 91 | + } |
| 92 | + |
| 93 | + public function getBuilds($offset = 0, $count = 10) |
| 94 | + { |
| 95 | + $queryData = array( |
| 96 | + 'offset' => $offset, |
| 97 | + 'count' => $count |
| 98 | + ); |
| 99 | + |
| 100 | + return $this->_doRequest("builds/?" . http_build_query($queryData), "GET"); |
| 101 | + } |
| 102 | + |
| 103 | + public function getBuild($buildID) |
| 104 | + { |
| 105 | + if (empty($buildID)) |
| 106 | + { |
| 107 | + throw new \Exception("Please use a valid buildID"); |
| 108 | + } |
| 109 | + |
| 110 | + return $this->_doRequest("builds/" . $buildID, "GET"); |
| 111 | + } |
| 112 | + |
| 113 | + public function getTunnels() |
| 114 | + { |
| 115 | + return $this->_doRequest("tunnels/", "GET"); |
| 116 | + } |
| 117 | + |
| 118 | + public function getUserInfo() |
| 119 | + { |
| 120 | + return $this->_doRequest("user", "GET"); |
| 121 | + } |
| 122 | + |
| 123 | + public function updateUserInfo(array $details) |
| 124 | + { |
| 125 | + $data = array(); |
| 126 | + |
| 127 | + foreach ($details as $k => $v) |
| 128 | + { |
| 129 | + $data['user[' . $k . ']'] = $v; |
| 130 | + } |
| 131 | + |
| 132 | + return $this->_doRequest("user", "PUT", $data); |
| 133 | + } |
| 134 | + |
| 135 | + private function _doRequest($path, $method = 'POST', array $data = array()) |
| 136 | + { |
| 137 | + $curl = curl_init(); |
| 138 | + curl_setopt($curl, CURLOPT_URL, "https://api.testingbot.com/v1/" . $path); |
| 139 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
| 140 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 141 | + curl_setopt($curl, CURLOPT_USERPWD, $this->_key . ":" . $this->_secret); |
| 142 | + if (!empty($data)) |
| 143 | + { |
| 144 | + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 145 | + } |
| 146 | + |
| 147 | + $response = curl_exec($curl); |
| 148 | + curl_close($curl); |
| 149 | + |
| 150 | + return $this->_parseResult($response); |
| 151 | + } |
| 152 | + |
| 153 | + private function _parseResult($response) |
| 154 | + { |
| 155 | + $result = json_decode($response, true); |
| 156 | + if (empty($result)) |
| 157 | + { |
| 158 | + throw new \Exception("An API error occurred: " . print_r($response, true)); |
| 159 | + } |
| 160 | + |
| 161 | + return $result; |
| 162 | + } |
| 163 | +} |
0 commit comments