/** * Curl Jenkins JSON API * * NOTE: This method is not meant to be used on its own. It is used by other classes, * e.g. \Bart\Jenkins\Job, to make API calls against Jenkins. * * @param string $apiPath The full API path to curl against. For example, to do a * simple GET against the Jenkins Job 'Example', the full path, 'job/Example/api/json' * must be passed in. * @param array $postData if null, then curl uses GET, otherwise POSTs data * @return array JSON data decoded as PHP array * @throws JenkinsApiException */ public function curlJenkinsApi($apiPath, array $postData = null) { if (!Strings::startsWith($apiPath, '/')) { $apiPath = "/{$apiPath}"; } $fullUrl = "{$this->baseUrl}{$apiPath}"; $isPost = $postData !== null; $this->logger->debug('Curling ' . ($isPost ? 'POST ' : 'GET ') . $fullUrl); /** @var \Bart\Curl $curl */ $curl = Diesel::create('\\Bart\\Curl', $fullUrl, $this->port); if ($this->curlOptions !== []) { $curl->setPhpCurlOpts($this->curlOptions); } $response = $isPost ? $curl->post('', [], $postData) : $curl->get('', []); $httpCode = $response['info']['http_code']; $content = $response['content']; if ($httpCode !== 200 && $httpCode !== 201 && $httpCode !== 202) { throw new JenkinsApiException("The Jenkins API call returned a {$httpCode}, " . "with the following content: {$content}"); } return JSON::decode($content); }
/** * @param string $remoteQuery Gerrity query * @param boolean $expectResponse If a JSON response is expected * @return null|ApiResult * @throws GerritException */ private function send($remoteQuery, $expectResponse = true) { try { $this->logger->debug("Sending remote command to gerrit: {$remoteQuery}"); $gerritResponseArray = $this->ssh->exec($remoteQuery); if (!$expectResponse) { return; } $stats = JSON::decode(array_pop($gerritResponseArray)); if ($stats['type'] == 'error') { throw new GerritException($stats['message']); } $records = array(); foreach ($gerritResponseArray as $json) { $records[] = JSON::decode($json); } return new ApiResult($stats, $records); } catch (CommandException $e) { $this->logger->warn('Gerrit query failed', $e); throw new GerritException('Query to gerrit failed', 0, $e); } catch (JSONParseException $e) { $this->logger->warn('Gerrit query returned bad json', $e); throw new GerritException('Gerrit query returned bad json', 0, $e); } }