示例#1
0
 /**
  * Check credentials
  *
  * @param string                  $endpoint
  * @param AuthenticationInterface $authentication
  * @param ClientInterface         $client
  * @throws Api\Exception
  */
 public function __construct($endpoint, AuthenticationInterface $authentication, ClientInterface $client = null)
 {
     if (!$authentication->getId() || !$authentication->getPassword()) {
         throw new Api\Exception('Username or password is empty.', self::ERROR_EMPTY_CREDENTIALS);
     }
     parent::__construct($endpoint, $authentication, $client);
 }
示例#2
0
 /**
  * send request to the api server
  *
  * @param string $method
  * @param string $url
  * @param array $data
  * @param string $endpoint
  * @param AuthenticationInterface $credential
  * @param boolean $isFile
  * @param boolean $debug
  * @return array|string
  * @throws \Exception
  * @throws UnauthorizedException
  */
 public function sendRequest($method, $url, $data = array(), $endpoint, AuthenticationInterface $credential, $isFile = false, $debug = false)
 {
     $curl = curl_init();
     if ($method == "GET") {
         $url .= "?" . http_build_query($data);
     }
     curl_setopt($curl, CURLOPT_URL, $endpoint . $url);
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     if ($credential instanceof Basic) {
         curl_setopt($curl, CURLOPT_USERPWD, sprintf("%s:%s", $credential->getId(), $credential->getPassword()));
     }
     curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($curl, CURLOPT_VERBOSE, $debug);
     if ($isFile) {
         if (defined('CURLOPT_SAFE_UPLOAD')) {
             curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
         }
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Atlassian-Token: nocheck'));
     } else {
         curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json;charset=UTF-8"));
     }
     if ($method == "POST") {
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($isFile) {
             $data['file'] = $this->getCurlValue($data['file']);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         } else {
             curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
         }
     } elseif ($method == "DELETE") {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
     } elseif ($method == "PUT") {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
     }
     $data = curl_exec($curl);
     $errorNumber = curl_errno($curl);
     if ($errorNumber > 0) {
         throw new Exception(sprintf('Jira request failed: code = %s, "%s"', $errorNumber, curl_error($curl)));
     }
     // if empty result and status != "204 No Content"
     if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 401) {
         throw new UnauthorizedException("Unauthorized");
     }
     if ($data === '' && !in_array(curl_getinfo($curl, CURLINFO_HTTP_CODE), array(201, 204))) {
         throw new Exception("JIRA Rest server returns unexpected result.");
     }
     if (is_null($data)) {
         throw new Exception("JIRA Rest server returns unexpected result.");
     }
     return $data;
 }
示例#3
0
 public function sendRequest($method, $url, $data = array(), $endpoint, AuthenticationInterface $credential, $isFile = false, $debug = false)
 {
     if (!$this->isSupportedMethod($method)) {
         throw new Exception('Unsupported HTTP method: ' . $method);
     }
     if ($credential instanceof Basic) {
         $this->client->setDefaultOption('auth', array($credential->getId(), $credential->getPassword()));
     }
     $request = $this->createRequest($method, $url, $data, $isFile);
     try {
         $response = $this->client->send($request);
         $content = $response->getBody()->getContents();
         if (strlen($content) <= 0) {
             return $this->statusToJSON($response);
         }
         return $content;
     } catch (ClientException $e) {
         return $this->statusToJSON($e->getResponse());
     }
 }