From 41eb14de570f1ace95d49c9b996bad919c621b54 Mon Sep 17 00:00:00 2001 From: Przemek Date: Mon, 11 Jan 2016 15:13:04 +0100 Subject: [PATCH 1/4] Updated endpoints for v2 api --- .gitignore | 1 + class.php | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 57872d0..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +.idea diff --git a/class.php b/class.php index 7800a20..c3e57b4 100644 --- a/class.php +++ b/class.php @@ -37,71 +37,71 @@ private function query($action,$postdata=array()) { } public function ticket($ticket_reference) { - return $this->query('/api/v1/tickets/ticket',array('reference' => $ticket_reference)); + return $this->query('/api/v2/tickets/ticket',array('ticket' => $ticket_reference)); } public function create_ticket($params = array()) { - return $this->query('/api/v1/tickets/submit',$params); + return $this->query('/api/v2/tickets/submit',$params); } public function post_update($params = array()) { - return $this->query('/api/v1/tickets/post_update',$params); + return $this->query('/api/v2/tickets/post_update',$params); } public function tickets($page = '1') { - return $this->query('/api/v1/tickets/all',array('page' => $page)); + return $this->query('/api/v2/tickets/all',array('page' => $page)); } public function update_ticket($params = array()) { - return $this->query('/api/v1/tickets/update',$params); + return $this->query('/api/v2/tickets/update',$params); } public function run_macro($params = array()) { - return $this->query('/api/v1/tickets/macro',$params); + return $this->query('/api/v2/tickets/macro',$params); } public function add_follow_up($params = array()) { - return $this->query('/api/v1/tickets/followup',$params); + return $this->query('/api/v2/tickets/add_followup',$params); } public function create_user($params = array()) { - return $this->query('/api/v1/users/create',$params); + return $this->query('/api/v2/users/create',$params); } public function statuses() { - return $this->query('/api/v1/objects/statuses'); + return $this->query('/api/v2/objects/statuses'); } public function priorities() { - return $this->query('/api/v1/objects/priorities'); + return $this->query('/api/v2/objects/priorities'); } public function teams() { - return $this->query('/api/v1/objects/teams'); + return $this->query('/api/v2/objects/teams'); } public function brands() { - return $this->query('/api/v1/objects/brands'); + return $this->query('/api/v2/objects/brands'); } public function departments() { - return $this->query('/api/v1/objects/departments'); + return $this->query('/api/v2/objects/departments'); } public function escalation_paths() { - return $this->query('/api/v1/objects/escalation_paths'); + return $this->query('/api/v2/objects/escalation_paths'); } public function slas() { - return $this->query('/api/v1/objects/slas'); + return $this->query('/api/v2/objects/slas'); } public function filters() { - return $this->query('/api/v1/objects/filters'); + return $this->query('/api/v2/objects/filters'); } public function spql($params = array()) { - return $this->query('/api/v1/tickets/spql',$params); + return $this->query('/api/v2/tickets/spql',$params); } /** @@ -109,7 +109,7 @@ public function spql($params = array()) { * @return array list of knowledgebases in an array format. */ public function kb_list() { - return $this->query('/api/v1/knowledge/list'); + return $this->query('/api/v2/knowledge/list'); } /** @@ -118,7 +118,7 @@ public function kb_list() { * @return array The knowledgebase as an array. */ public function kb($kb_id) { - return $this->query('/api/v1/knowledge/tree', array('kb' => $kb_id)); + return $this->query('/api/v2/knowledge/tree', array('kb' => $kb_id)); } /** From acce860df5e026b6be574e7b0552bd62e0401df1 Mon Sep 17 00:00:00 2001 From: Przemek Date: Mon, 11 Jan 2016 15:25:04 +0100 Subject: [PATCH 2/4] Converting to psr4 --- README.md | 2 +- composer.json | 4 +++- class.php => src/Sirportly.php | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) rename class.php => src/Sirportly.php (99%) diff --git a/README.md b/README.md index 53df251..41298d7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This library allows you to interact with your Sirportly data using PHP. ## Setting up a Sirportly Client ```php -$sirportly = new Sirportly('the-token','the-secret'); +$sirportly = new \Sirportly\Sirportly('the-token','the-secret'); ``` ## Creating a ticket diff --git a/composer.json b/composer.json index 32a0b4e..1c3f07a 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ ], "minimum-stability": "stable", "autoload": { - "classmap": ["class.php"] + "psr-4": { + "Sirportly\\": "src" + } }, "require": { diff --git a/class.php b/src/Sirportly.php similarity index 99% rename from class.php rename to src/Sirportly.php index c3e57b4..359d1ab 100644 --- a/class.php +++ b/src/Sirportly.php @@ -1,4 +1,7 @@ Date: Mon, 19 Mar 2018 14:13:00 +0000 Subject: [PATCH 3/4] Support for cURL errors --- src/Exceptions/SirportlyDefaultException.php | 10 ++++++++++ src/Sirportly.php | 21 +++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/Exceptions/SirportlyDefaultException.php diff --git a/src/Exceptions/SirportlyDefaultException.php b/src/Exceptions/SirportlyDefaultException.php new file mode 100644 index 0000000..0a5eac3 --- /dev/null +++ b/src/Exceptions/SirportlyDefaultException.php @@ -0,0 +1,10 @@ +url = $url; } + /** + * @param $action + * @param array $postdata + * @return mixed + * @throws SirportlyDefaultException + */ private function query($action,$postdata=array()) { $curl = curl_init(); $query_string = ""; @@ -29,16 +37,27 @@ private function query($action,$postdata=array()) { curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $query_string); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_POST, 1); $result = curl_exec($curl); + $info = curl_getinfo($curl); + + if ($result === false || !in_array($info['http_code'], [200, 201])) { + $error = "No cURL data returned for $action [". $info['http_code']. "]"; + if (curl_error($curl)) { + $error .= "\n" . curl_error($curl); + } + throw new SirportlyDefaultException($error); + } + curl_close($curl); $decode = json_decode($result,true); return $decode; } public function ticket($ticket_reference) { - return $this->query('/api/v2/tickets/ticket',array('ticket' => $ticket_reference)); + return $this->query('/api/v2/tickets/ticket', array('ticket' => $ticket_reference)); } public function create_ticket($params = array()) { From d6bd31f39650497d6bd744832f4fd06c127cf1a4 Mon Sep 17 00:00:00 2001 From: Przemek Peron Date: Mon, 19 Mar 2018 18:21:18 +0000 Subject: [PATCH 4/4] Better error reporting --- src/Sirportly.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Sirportly.php b/src/Sirportly.php index 9224f4d..526c1bc 100644 --- a/src/Sirportly.php +++ b/src/Sirportly.php @@ -30,10 +30,11 @@ public function __construct($token,$secret,$url='https://api.sirportly.com') { private function query($action,$postdata=array()) { $curl = curl_init(); $query_string = ""; + $url = $this->url . $action; foreach ($postdata AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; $header = array('X-Auth-Token: '.$this->token, 'X-Auth-Secret: '.$this->secret); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_URL, $this->url.$action); + curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $query_string); @@ -44,7 +45,7 @@ private function query($action,$postdata=array()) { $info = curl_getinfo($curl); if ($result === false || !in_array($info['http_code'], [200, 201])) { - $error = "No cURL data returned for $action [". $info['http_code']. "]"; + $error = "No cURL data returned for $url [http error: ". $info['http_code']. "]"; if (curl_error($curl)) { $error .= "\n" . curl_error($curl); }