From 4559702e65b370b6d306543cfe4aa9b5ec4c96d0 Mon Sep 17 00:00:00 2001 From: Maulidan Nashuha <34979909+maulidannashuha@users.noreply.github.com> Date: Tue, 17 Oct 2023 10:45:11 +0700 Subject: [PATCH] set new feature for set timeout --- src/ESignBsre.php | 31 ++++++++++++++++++++++++------- src/ESignBsreResponse.php | 11 +++++++---- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/ESignBsre.php b/src/ESignBsre.php index 380a1e3..4bf85ef 100644 --- a/src/ESignBsre.php +++ b/src/ESignBsre.php @@ -14,13 +14,15 @@ class ESignBsre private $file; private $fileName; private $view = 'invisible'; + private $timeout; - public function __construct($baseUrl, $username, $password){ + public function __construct($baseUrl, $username, $password, $timeout=1){ $this->baseUrl = $baseUrl; $this->http = new GuzzleClient(); $this->username = $username; $this->password = $password; + $this->timeout = $timeout; } public function setFile($file, $fileName){ @@ -30,11 +32,18 @@ public function setFile($file, $fileName){ return $this; } + public function setTimeout($timeout){ + $this->timeout = $timeout; + + return $this; + } + public function sign($nik, $passphrase) { try { $response = $this->http->request('POST', "{$this->getBaseUrl()}/api/sign/pdf", [ 'auth' => $this->getAuth(), + 'timeout' => $this->timeout, 'multipart' => [ [ 'name' => 'file', @@ -55,11 +64,13 @@ public function sign($nik, $passphrase) ], ], ]); - }catch (\Exception $e) { + }catch(\GuzzleHttp\Exception\ConnectException $e){ + return new ESignBsreResponse($e); + } catch (\Exception $e) { $response = $e->getResponse(); } - return new ESignBsreResponse($response); + return (new ESignBsreResponse())->setFromResponse($response); } public function verification() @@ -67,6 +78,7 @@ public function verification() try { $response = $this->http->request('POST', "{$this->getBaseUrl()}/api/sign/verify", [ 'auth' => $this->getAuth(), + 'timeout' => $this->timeout, 'multipart' => [ [ 'name' => 'signed_file', @@ -75,23 +87,28 @@ public function verification() ], ] ]); - }catch (\Exception $e) { + }catch(\GuzzleHttp\Exception\ConnectException $e){ + return new ESignBsreResponse($e); + } catch (\Exception $e) { $response = $e->getResponse(); } - return new ESignBsreResponse($response); + return (new ESignBsreResponse())->setFromResponse($response); } public function statusUser($nik) { try { $response = $this->http->request('GET', "{$this->getBaseUrl()}/api/user/status/$nik", [ - 'auth' => $this->getAuth() + 'auth' => $this->getAuth(), + 'timeout' => $this->timeout, ]); + } catch(\GuzzleHttp\Exception\ConnectException $e){ + return (new ESignBsreResponse())->setFromExeption($e, ESignBsreResponse::STATUS_TIMEOUT); } catch (\Exception $e) { $response = $e->getResponse(); } - return new ESignBsreResponse($response); + return (new ESignBsreResponse())->setFromResponse($response); } private function getAuth(){ diff --git a/src/ESignBsreResponse.php b/src/ESignBsreResponse.php index 9eba08d..887b86c 100644 --- a/src/ESignBsreResponse.php +++ b/src/ESignBsreResponse.php @@ -8,11 +8,14 @@ class ESignBsreResponse private $errors; private $data; private $response; - private const STATUS_OK = 200; + public const STATUS_OK = 200; + public const STATUS_TIMEOUT = 408; - public function __construct($response) - { - $this->setFromResponse($response); + public function setFromExeption($error, $status){ + $this->status = $status; + $this->errors = $error->getMessage(); + + return $this; } public function setFromResponse($response){