From 3c5c7155c27e66df7664838671a673513f7f0eb5 Mon Sep 17 00:00:00 2001 From: kristuff Date: Sun, 17 Jan 2021 19:09:15 +0100 Subject: [PATCH] v0.9.9 **Added** - better exception handling with new class `SilentApiHandler`: to not raise any exception during API request but instead return an `ApiResponse` with errors - version constant - `ApiReponse` has new `errors()` and `hasError()` methods --- README.md | 2 +- lib/ApiBase.php | 2 +- lib/ApiHandler.php | 11 +- lib/ApiResponse.php | 36 ++++++- lib/CurlTrait.php | 2 +- lib/InvalidPermissionException.php | 2 +- lib/SilentApiHandler.php | 164 +++++++++++++++++++++++++++++ 7 files changed, 210 insertions(+), 9 deletions(-) create mode 100644 lib/SilentApiHandler.php diff --git a/README.md b/README.md index f300358..23ee560 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Deploy with composer: ```json ... "require": { - "kristuff/abuseipdb": ">=0.9.8-stable" + "kristuff/abuseipdb": ">=0.9.9-stable" }, ``` diff --git a/lib/ApiBase.php b/lib/ApiBase.php index 0a4edd2..e35a348 100644 --- a/lib/ApiBase.php +++ b/lib/ApiBase.php @@ -14,7 +14,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @version 0.9.8 + * @version 0.9.9 * @copyright 2020-2021 Kristuff */ diff --git a/lib/ApiHandler.php b/lib/ApiHandler.php index b8eadd2..549c985 100644 --- a/lib/ApiHandler.php +++ b/lib/ApiHandler.php @@ -14,7 +14,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @version 0.9.8 + * @version 0.9.9 * @copyright 2020-2021 Kristuff */ @@ -32,6 +32,11 @@ class ApiHandler extends ApiBase */ use CurlTrait; + /** + * @var string $version + */ + const VERSION = 'v0.9.9'; + /** * The ips to remove from report messages * Generally you will add to this list yours ipv4 and ipv6, hostname, domain names @@ -214,8 +219,8 @@ class ApiHandler extends ApiBase public function check(string $ip, int $maxAgeInDays = 30, bool $verbose = false): ApiResponse { // max age must be less or equal to 365 - if ($maxAgeInDays > 365 || $maxAgeInDays < 1){ - throw new \InvalidArgumentException('maxAgeInDays must be between 1 and 365 (' . $maxAgeInDays . ' was given)'); + if ( $maxAgeInDays > 365 || $maxAgeInDays < 1 ){ + throw new \InvalidArgumentException('maxAgeInDays must be between 1 and 365.'); } // ip must be set diff --git a/lib/ApiResponse.php b/lib/ApiResponse.php index 1b9a232..1231c00 100644 --- a/lib/ApiResponse.php +++ b/lib/ApiResponse.php @@ -14,7 +14,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @version 0.9.8 + * @version 0.9.9 * @copyright 2020-2021 Kristuff */ @@ -33,6 +33,13 @@ class ApiResponse */ protected $curlResponse; + /** + * + * @access protected + * @var object + */ + protected $decodedResponse; + /** * Constructor * @@ -43,6 +50,7 @@ class ApiResponse public function __construct(?string $plaintext = null) { $this->curlResponse = $plaintext; + $this->decodedResponse = json_decode($plaintext, false); } /** @@ -66,7 +74,7 @@ class ApiResponse */ public function getObject(): ?object { - return json_decode($this->curlResponse, false); + return $this->decodedResponse; } /** @@ -80,4 +88,28 @@ class ApiResponse { return $this->curlResponse; } + + /** + * Get whether the response contains error(s) + * + * @access public + * + * @return bool + */ + public function hasError(): bool + { + return count($this->errors()) > 0; + } + + /** + * Get an array of errors (object) contained is response + * + * @access public + * + * @return array + */ + public function errors(): array + { + return ($this->decodedResponse && $this->decodedResponse->errors) ? $this->decodedResponse->errors : []; + } } \ No newline at end of file diff --git a/lib/CurlTrait.php b/lib/CurlTrait.php index 53758d6..bf73b1e 100644 --- a/lib/CurlTrait.php +++ b/lib/CurlTrait.php @@ -14,7 +14,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @version 0.9.8 + * @version 0.9.9 * @copyright 2020-2021 Kristuff */ diff --git a/lib/InvalidPermissionException.php b/lib/InvalidPermissionException.php index 7290792..bdf6ead 100644 --- a/lib/InvalidPermissionException.php +++ b/lib/InvalidPermissionException.php @@ -14,7 +14,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @version 0.9.8 + * @version 0.9.9 * @copyright 2020-2021 Kristuff */ diff --git a/lib/SilentApiHandler.php b/lib/SilentApiHandler.php new file mode 100644 index 0000000..dd45bc2 --- /dev/null +++ b/lib/SilentApiHandler.php @@ -0,0 +1,164 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @version 0.9.9 + * @copyright 2020-2021 Kristuff + */ + +namespace Kristuff\AbuseIPDB; + +/** + * Class SilentApiHandler + * + * Overwrite ApiHandler with Exception handling + * Instead of Exception, all method return an ApiResponse that may + * contains errors from the AbuseIPDB API, or internal errors + */ +class SilentApiHandler extends ApiHandler +{ + /** + * Get an internal error message in an ApiResponse object + * + * @access public + * @param string $message The error message + * + * @return ApiResponse + */ + public function getErrorResponse(string $message): ApiResponse + { + $response = [ + "errors" => [ + [ + "title" => "Internal Error", + "detail" => $message + ] + ] + ]; + + return new ApiResponse(json_encode($response)); + } + + /** + * Performs a 'report' api request, with Exception handling + * + * @access public + * @param string $ip The ip to report + * @param string $categories The report category(es) + * @param string $message The report message + * + * @return ApiResponse + */ + public function report(string $ip, string $categories, string $message): ApiResponse + { + try { + return parent::report($ip,$categories,$message); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } + + /** + * Performs a 'bulk-report' api request, with Exception handling + * + * @access public + * @param string $filePath The CSV file path. Could be an absolute or relative path. + * + * @return ApiResponse + */ + public function bulkReport(string $filePath): ApiResponse + { + try { + return parent::bulkReport($filePath); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } + + /** + * Perform a 'clear-address' api request, with Exception handling + * + * @access public + * @param string $ip The IP to clear reports + * + * @return ApiResponse + */ + public function clearAddress(string $ip): ApiResponse + { + try { + return parent::clearAddress($ip); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } + + /** + * Perform a 'check' api request, with Exception handling + * + * @access public + * @param string $ip The ip to check + * @param int $maxAgeInDays Max age in days. Default is 30. + * @param bool $verbose True to get the full response (last reports and countryName). Default is false + * + * @return ApiResponse + */ + public function check(string $ip, int $maxAgeInDays = 30, bool $verbose = false): ApiResponse + { + try { + return parent::check($ip, $maxAgeInDays, $verbose); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } + + /** + * Perform a 'check-block' api request, with Exception handling + * + * @access public + * @param string $network The network to check + * @param int $maxAgeInDays The Max age in days, must + * + * @return ApiResponse + */ + public function checkBlock(string $network, int $maxAgeInDays = 30): ApiResponse + { + try { + return parent::checkBlock($network, $maxAgeInDays); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } + + /** + * Perform a 'blacklist' api request, with Exception handling + * + * @access public + * @param int $limit The blacklist limit. Default is 10000 (the api default limit) + * @param bool $plainText True to get the response in plaintext list. Default is false + * @param int $confidenceMinimum The abuse confidence score minimum (subscribers feature). Default is 100. + * The confidence minimum must be between 25 and 100. + * This parameter is subscriber feature (not honored otherwise). + * + * @return ApiResponse + */ + public function blacklist(int $limit = 10000, bool $plainText = false, int $confidenceMinimum = 100): ApiResponse + { + try { + return parent::blacklist($limit, $plainText, $confidenceMinimum); + } catch (\Exception $e) { + return $this->getErrorResponse($e->getMessage()); + } + } +} \ No newline at end of file