|
@@ -80,9 +80,28 @@ class HTTPUnexpectedResponse extends \Exception {
|
|
|
598 => 'Network read timeout error', // Unknown
|
|
|
599 => 'Network connect timeout error', // Unknown
|
|
|
);
|
|
|
+
|
|
|
+ private $response;
|
|
|
+
|
|
|
+ public function __construct($message, $code = 0, Exception $previous = null) {
|
|
|
+ $this->response = $message;
|
|
|
+ parent::__construct(HTTPUnexpectedResponse::$ecode[$code], $code, $previous);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function __toString() {
|
|
|
+ return __CLASS__ . ": [{$this->code} | {$this->message}]: {$this->response}\n";
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getResponse() {
|
|
|
+ return $this->response;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-class NotAuthorizedException extends \Exception{}
|
|
|
+class NotAuthorizedException extends HTTPUnexpectedResponse {
|
|
|
+ public function __construct($message, Exception $previous = null) {
|
|
|
+ parent::__contruct($message, 401, $previous);
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
trait Curl {
|
|
|
protected $url;
|
|
@@ -92,24 +111,46 @@ trait Curl {
|
|
|
protected $timeout = 30;
|
|
|
protected $max_redirects = 4;
|
|
|
|
|
|
- protected function method(&$req, $scope, $params, $post, $ret) {
|
|
|
- echo sprintf("%s: %s%s\n", $post ? "POST" : "GET", $this->url, $scope);
|
|
|
+ private function array_2_params(array $params) {
|
|
|
+ return join("&", array_map(function($k, $v) {
|
|
|
+ return sprintf("%s=%s", $k, rawurlencode(is_bool($v) ? ($v ? "true" : "false") : $v ));
|
|
|
+ }, array_keys($params), $params));
|
|
|
+ }
|
|
|
+
|
|
|
+ private function array_2_json(array $params) {
|
|
|
+ return json_encode($params);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function method(string $method, string &$req, string $scope, array $params, bool $ret) {
|
|
|
$c = curl_init();
|
|
|
|
|
|
if (!$c) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($post) {
|
|
|
- curl_setopt($c, CURLOPT_POST, 1);
|
|
|
- curl_setopt($c, CURLOPT_POSTFIELDS, $params);
|
|
|
+ $headers = array(
|
|
|
+ sprintf("Authorization: token %s", $this->token),
|
|
|
+ );
|
|
|
+
|
|
|
+ $url = sprintf("%s%s", $this->url, $scope);
|
|
|
+
|
|
|
+ if (in_array($method, array("DELETE", "PATCH", "POST"))) {
|
|
|
+ $json = $this->array_2_json($params);
|
|
|
+ curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
+ curl_setopt($c, CURLOPT_POSTFIELDS, $json);
|
|
|
+ echo sprintf("JSON: %s", $json);
|
|
|
+ array_unshift($headers, "Content-Type: application/json");
|
|
|
+ array_push($headers, "Content-Length: " . strlen($json));
|
|
|
+ var_dump($headers);
|
|
|
+ } else {
|
|
|
+ $url .= "?" . $this->array_2_params($params);
|
|
|
}
|
|
|
|
|
|
+ echo sprintf("%s: %s\n", $method, $url);
|
|
|
+
|
|
|
curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
|
|
|
- curl_setopt($c, CURLOPT_URL, sprintf("%s%s", $this->url, $scope));
|
|
|
- curl_setopt($c, CURLOPT_HTTPHEADER, array(
|
|
|
- sprintf("Authorization: token %s", $this->token)
|
|
|
- ));
|
|
|
+ curl_setopt($c, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
|
|
|
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
|
|
|
curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
|
|
|
curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
|
|
@@ -126,7 +167,7 @@ trait Curl {
|
|
|
|
|
|
protected function authorized($scope) {
|
|
|
$ret = "";
|
|
|
- if ($this->method($ret, $scope, array(), false, false) == 401) {
|
|
|
+ if ($this->method("GET", $ret, $scope, array(), false) == 401) {
|
|
|
throw new NotAuthorizedException("Not authorized", 401);
|
|
|
}
|
|
|
return true;
|
|
@@ -135,30 +176,47 @@ trait Curl {
|
|
|
private function post($scope = "", $params = array()) {
|
|
|
$req = "";
|
|
|
|
|
|
- $code = $this->method($req, $scope, $params, true, true);
|
|
|
+ $code = $this->method("POST", $req, $scope, $params, true);
|
|
|
+
|
|
|
+ switch ($code) {
|
|
|
+ case 200:
|
|
|
+ case 201:
|
|
|
+ return $req;
|
|
|
+ case 401:
|
|
|
+ throw new NotAuthorizedException($req);
|
|
|
+ default:
|
|
|
+ throw new HTTPUnexpectedResponse($req, $code);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function delete($scope = "") {
|
|
|
+ $req = "";
|
|
|
+
|
|
|
+ $code = $this->method("DELETE", $req, $scope, array(), true);
|
|
|
|
|
|
switch ($code) {
|
|
|
case 200:
|
|
|
+ case 204:
|
|
|
return $req;
|
|
|
case 401:
|
|
|
- throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
|
|
|
+ throw new NotAuthorizedException($req);
|
|
|
default:
|
|
|
- throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
|
|
|
+ throw new HTTPUnexpectedResponse($req, $code);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private function get($scope = "", $params = array()) {
|
|
|
$req = "";
|
|
|
|
|
|
- $code = $this->method($req, $scope, $params, false, true);
|
|
|
+ $code = $this->method("GET", $req, $scope, $params, true);
|
|
|
|
|
|
switch ($code) {
|
|
|
case 200:
|
|
|
return $req;
|
|
|
case 401:
|
|
|
- throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
|
|
|
+ throw new NotAuthorizedException($req);
|
|
|
default:
|
|
|
- throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
|
|
|
+ throw new HTTPUnexpectedResponse($req, $code);
|
|
|
|
|
|
}
|
|
|
}
|