From 95c99c65133a254816f8bf614a26e1e2de36e3c5 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 23 Mar 2019 09:43:53 +0000 Subject: [PATCH 1/4] Add Pushover Glances API --- src/LeonardoTeixeira/Pushover/Client.php | 150 +++++++++++++++++++++-- 1 file changed, 142 insertions(+), 8 deletions(-) diff --git a/src/LeonardoTeixeira/Pushover/Client.php b/src/LeonardoTeixeira/Pushover/Client.php index 97ebb39..f19cbb7 100644 --- a/src/LeonardoTeixeira/Pushover/Client.php +++ b/src/LeonardoTeixeira/Pushover/Client.php @@ -12,6 +12,7 @@ class Client private $token; const API_MESSAGE_URL = 'https://api.pushover.net/1/messages.json'; + const API_GLANCES_URL = 'https://api.pushover.net/1/glances.json'; const API_RECEIPTS_URL = 'https://api.pushover.net/1/receipts'; public function __construct($user = null, $token = null) @@ -150,15 +151,15 @@ public function poll(Receipt $receipt) if (! $receipt instanceof Receipt ) { throw new PushoverException('The parameter \'$receipt\' must be a Receipt instance.'); } - + if(is_null($receipt->getReceipt())) { - throw new PushoverException('The receipt content was not set.'); + throw new PushoverException('The receipt content was not set.'); } try { $request = Requests::get(self::API_RECEIPTS_URL.'/'.$receipt->getReceipt().'.json?token='.$this->token, []); $responseJson = json_decode($request->body, true); - + if (!isset($responseJson['status']) || $responseJson['status'] != 1) { if (isset($responseJson['errors'])) { throw new PushoverException($responseJson['errors'][0]); @@ -177,15 +178,15 @@ public function cancel(Receipt $receipt) if (! $receipt instanceof Receipt ) { throw new PushoverException('The parameter \'$receipt\' must be a Receipt instance.'); } - + if(is_null($receipt->getReceipt())) { - throw new PushoverException('The receipt content was not set.'); + throw new PushoverException('The receipt content was not set.'); } try { $request = Requests::post(self::API_RECEIPTS_URL.'/'.$receipt->getReceipt().'/cancel.json', [], ['token' => $this->token]); $responseJson = json_decode($request->body, true); - + if (!isset($responseJson['status']) || $responseJson['status'] != 1) { if (isset($responseJson['errors'])) { throw new PushoverException($responseJson['errors'][0]); @@ -195,6 +196,139 @@ public function cancel(Receipt $receipt) } } catch (\Exception $e) { throw new PushoverException($e->getMessage()); - } - } + } + } + + public function updateGlances(Glances $glances, $device = null) + { + if (!$glances->hasTitle() && !$glances->hasText() && !$glances->hasSubtext() && !$glances->hasCount() && !$glances->hasPercent()) { + throw new PushoverException('The parameter \'$glances\' must have at least one parameter'); + } + + $postData = [ + 'user' => $this->user, + 'token' => $this->token + ]; + + if ($device != null) { + $postData['device'] = $device; + } + + if ($glances->hasTitle()) { + $postData['title'] = $glances->getTitle(); + } + + if ($glances->hasText()) { + $postData['text'] = $glances->getText(); + } + + if ($glances->hasSubtext()) { + $postData['subtext'] = $glances->getSubtext(); + } + + if ($glances->hasCount()) { + $postData['count'] = $glances->getCount(); + } + + if ($glances->hasPercent()) { + $postData['percent'] = $glances->getPercent(); + } + + try { + // Using hooks is an ugly hack since we bypass the fancy request API + // Up to no, Requests doesn't support multi-part headers :-/ + // + // Should we switch to guzzle/guzzle ? + $hooks = new Requests_Hooks(); + $hooks->register('curl.before_send', function($fp) use ($postData) { + curl_setopt($fp, CURLOPT_POSTFIELDS, $postData); + $postData = []; + }); + $hooks = ['hooks' => $hooks]; + + $request = Requests::post(self::API_GLANCES_URL, [], $postData, $hooks); + $responseJson = json_decode($request->body); + + if (!isset($responseJson->status) || $responseJson->status != 1) { + if (isset($responseJson->errors)) { + throw new PushoverException($responseJson->errors[0]); + } else { + throw new PushoverException('Unable to access the Pushover API.'); + } + } + if(isset($responseJson->receipt)) { + return new Receipt($responseJson->receipt); + } + return new Receipt(); + + } catch (\Exception $e) { + throw new PushoverException($e->getMessage()); + } + } + + public function updateGlances(Glances $glances, $device = null) + { + if (!$glances->hasTitle() && !$glances->hasText() && !$glances->hasSubtext() && !$glances->hasCount() && !$glances->hasPercent()) { + throw new PushoverException('The parameter \'$glances\' must have at least one parameter'); + } + + $postData = [ + 'user' => $this->user, + 'token' => $this->token + ]; + + if ($device != null) { + $postData['device'] = $device; + } + + if ($glances->hasTitle()) { + $postData['title'] = $glances->getTitle(); + } + + if ($glances->hasText()) { + $postData['text'] = $glances->getText(); + } + + if ($glances->hasSubtext()) { + $postData['subtext'] = $glances->getSubtext(); + } + + if ($glances->hasCount()) { + $postData['count'] = $glances->getCount(); + } + + if ($glances->hasPercent()) { + $postData['percent'] = $glances->getPercent(); + } + + try { + // Using hooks is an ugly hack since we bypass the fancy request API + // Up to no, Requests doesn't support multi-part headers :-/ + // + // Should we switch to guzzle/guzzle ? + $hooks = new Requests_Hooks(); + $hooks->register('curl.before_send', function($fp) use ($postData) { + curl_setopt($fp, CURLOPT_POSTFIELDS, $postData); + $postData = []; + }); + $hooks = ['hooks' => $hooks]; + $request = Requests::post(self::API_GLANCES_URL, [], $postData, $hooks); + $responseJson = json_decode($request->body); + + if (!isset($responseJson->status) || $responseJson->status != 1) { + if (isset($responseJson->errors)) { + throw new PushoverException($responseJson->errors[0]); + } else { + throw new PushoverException('Unable to access the Pushover API.'); + } + } + if(isset($responseJson->receipt)) { + return new Receipt($responseJson->receipt); + } + return new Receipt(); + + } catch (\Exception $e) { + throw new PushoverException($e->getMessage()); + } + } } From 4943e1ec7e460bfc5fe79b174c9baea0b23d26b1 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 23 Mar 2019 09:48:19 +0000 Subject: [PATCH 2/4] Add Glances API --- src/LeonardoTeixeira/Pushover/Glances.php | 101 ++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/LeonardoTeixeira/Pushover/Glances.php diff --git a/src/LeonardoTeixeira/Pushover/Glances.php b/src/LeonardoTeixeira/Pushover/Glances.php new file mode 100644 index 0000000..88952e6 --- /dev/null +++ b/src/LeonardoTeixeira/Pushover/Glances.php @@ -0,0 +1,101 @@ +title = null; + $this->text = null; + $this->subtext = null; + $this->count = null; + $this->percent = null; + } + + public function getTitle() + { + return $this->title; + } + + public function getText() + { + return $this->text; + } + + public function getSubtext() + { + return $this->subtext; + } + + public function getCount() + { + return $this->count; + } + + public function getPercent() + { + return $this->percent; + } + + public function setTitle($title) + { + $this->title = $title; + } + + public function setText($text) + { + $this->text = $text; + } + + public function setSubtext($subtext) + { + $this->subtext = $subtext; + } + + public function setCount($count) + { + $this->count = $count; + } + + public function setPercent($percent) + { + if ($percent < 0 || $percent > 100) { + throw new InvalidArgumentException('The percent value \'' . $percent . '\' is out of range.'); + } + $this->percent = $percent; + } + + public function hasTitle() + { + return !is_null($this->title); + } + + public function hasText() + { + return !is_null($this->text); + } + + public function hasSubtext() + { + return !is_null($this->subtext); + } + + public function hasCount() + { + return !is_null($this->count); + } + + public function hasPercent() + { + return !is_null($this->percent); + } +} From 98e701ff1095be8c82f4ea6e290d21b566bba986 Mon Sep 17 00:00:00 2001 From: Feilner Date: Sat, 23 Mar 2019 10:57:13 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index baee42f..de39226 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ require 'vendor/autoload.php'; use LeonardoTeixeira\Pushover\Client; use LeonardoTeixeira\Pushover\Message; +use LeonardoTeixeira\Pushover\Glances; use LeonardoTeixeira\Pushover\Exceptions\PushoverException; $client = new Client('YOUR_USER_CODE_HERE', 'YOUR_TOKEN_HERE'); @@ -52,6 +53,17 @@ try { } catch (PushoverException $e) { echo 'ERROR: ', $e->getMessage(), PHP_EOL; } + +$glances = new Glances(); + +$glances.setPercent(50); // Update percent field + +try { + $client->updateGlances($glances); + echo 'Glances has been updated!', PHP_EOL; +} catch (PushoverException $e) { + echo 'ERROR: ', $e->getMessage(), PHP_EOL; +} ``` You also can pass a title and the priority on constructor: @@ -69,6 +81,7 @@ date_default_timezone_set('UTC'); use LeonardoTeixeira\Pushover\Client; use LeonardoTeixeira\Pushover\Message; +use LeonardoTeixeira\Pushover\Glances; use LeonardoTeixeira\Pushover\Priority; use LeonardoTeixeira\Pushover\Sound; use LeonardoTeixeira\Pushover\Exceptions\PushoverException; @@ -95,6 +108,20 @@ try { } catch (PushoverException $e) { echo 'ERROR: ', $e->getMessage(), PHP_EOL; } +$glances = new Glances(); +$glances->setTitle('Title here'); +$glances->setText('Text here'); +$glances->setSubtext('Subtext here'); +$glances->setCount(123); +$glances->setPercent(100); + +try { + $receipt = $client->updateGlances($glances); + echo 'Glances has been updated!', PHP_EOL; + $status = $client->poll($receipt); +} catch (PushoverException $e) { + echo 'ERROR: ', $e->getMessage(), PHP_EOL; +} ``` ### Emergency Priority From ae51a6c2085ab788e68a2159bc5290711ee5c906 Mon Sep 17 00:00:00 2001 From: feilner Date: Sun, 24 Mar 2019 10:56:52 +0000 Subject: [PATCH 4/4] remoe duplicate code --- src/LeonardoTeixeira/Pushover/Client.php | 66 ------------------------ 1 file changed, 66 deletions(-) diff --git a/src/LeonardoTeixeira/Pushover/Client.php b/src/LeonardoTeixeira/Pushover/Client.php index f19cbb7..2cb58d5 100644 --- a/src/LeonardoTeixeira/Pushover/Client.php +++ b/src/LeonardoTeixeira/Pushover/Client.php @@ -265,70 +265,4 @@ public function updateGlances(Glances $glances, $device = null) throw new PushoverException($e->getMessage()); } } - - public function updateGlances(Glances $glances, $device = null) - { - if (!$glances->hasTitle() && !$glances->hasText() && !$glances->hasSubtext() && !$glances->hasCount() && !$glances->hasPercent()) { - throw new PushoverException('The parameter \'$glances\' must have at least one parameter'); - } - - $postData = [ - 'user' => $this->user, - 'token' => $this->token - ]; - - if ($device != null) { - $postData['device'] = $device; - } - - if ($glances->hasTitle()) { - $postData['title'] = $glances->getTitle(); - } - - if ($glances->hasText()) { - $postData['text'] = $glances->getText(); - } - - if ($glances->hasSubtext()) { - $postData['subtext'] = $glances->getSubtext(); - } - - if ($glances->hasCount()) { - $postData['count'] = $glances->getCount(); - } - - if ($glances->hasPercent()) { - $postData['percent'] = $glances->getPercent(); - } - - try { - // Using hooks is an ugly hack since we bypass the fancy request API - // Up to no, Requests doesn't support multi-part headers :-/ - // - // Should we switch to guzzle/guzzle ? - $hooks = new Requests_Hooks(); - $hooks->register('curl.before_send', function($fp) use ($postData) { - curl_setopt($fp, CURLOPT_POSTFIELDS, $postData); - $postData = []; - }); - $hooks = ['hooks' => $hooks]; - $request = Requests::post(self::API_GLANCES_URL, [], $postData, $hooks); - $responseJson = json_decode($request->body); - - if (!isset($responseJson->status) || $responseJson->status != 1) { - if (isset($responseJson->errors)) { - throw new PushoverException($responseJson->errors[0]); - } else { - throw new PushoverException('Unable to access the Pushover API.'); - } - } - if(isset($responseJson->receipt)) { - return new Receipt($responseJson->receipt); - } - return new Receipt(); - - } catch (\Exception $e) { - throw new PushoverException($e->getMessage()); - } - } }