public function sendRequest($method, $url, $data = array(), $endpoint, AuthenticationInterface $credential, $is_file = false, $debug = false)
{
if (!$credential instanceof Basic && !$credential instanceof Anonymous) {
throw new \InvalidArgumentException(sprintf('PHPClient does not support %s authentication.', get_class($credential)));
}
$header = array();
if (!$credential instanceof Anonymous) {
$header[] = 'Authorization: Basic ' . $credential->getCredential();
}
if (!$is_file) {
$header[] = 'Content-Type: application/json;charset=UTF-8';
}
$context = array('http' => array('method' => $method, 'header' => implode("\r\n", $header), 'ignore_errors' => true));
if ($method == 'POST' || $method == 'PUT' || $method == 'DELETE') {
if ($is_file) {
$filename = preg_replace('/^@/', '', $data['file']);
$name = $data['name'] !== null ? $data['name'] : basename($filename);
$boundary = '--------------------------' . microtime(true);
$header[] = 'X-Atlassian-Token: nocheck';
$header[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
$__data = '--' . $boundary . "\r\n" . 'Content-Disposition: form-data; name="file"; filename="' . $name . "\"\r\n" . "Content-Type: application/octet-stream\r\n\r\n" . file_get_contents($filename) . "\r\n";
$__data .= '--' . $boundary . "--\r\n";
} else {
$__data = json_encode($data);
}
$header[] = sprintf('Content-Length: %d', strlen($__data));
$context['http']['header'] = implode("\r\n", $header);
$context['http']['content'] = $__data;
} elseif ($method == 'GET') {
if (!is_array($data)) {
throw new \InvalidArgumentException('Data must be an array.');
}
$url .= '?' . http_build_query($data);
}
// @codeCoverageIgnoreStart
if (strpos($endpoint, 'https://') === 0 && !$this->isHttpsSupported()) {
throw new \InvalidArgumentException('does not support https wrapper. please enable openssl extension');
}
// @codeCoverageIgnoreEnd
list($http_code, $response, $error_message) = $this->doSendRequest($endpoint . $url, $context);
// Check for 401 code before "$error_message" checking, because it's considered as an error.
if ($http_code == 401) {
throw new UnauthorizedException('Unauthorized');
}
if (!empty($error_message)) {
throw new Exception(sprintf('Jira request failed: "%s"', $error_message));
}
if ($response === '' && !in_array($http_code, array(201, 204))) {
throw new Exception('JIRA Rest server returns unexpected result.');
}
// @codeCoverageIgnoreStart
if (is_null($response)) {
throw new Exception('JIRA Rest server returns unexpected result.');
}
// @codeCoverageIgnoreEnd
return $response;
}