From 95b5b661c3b761782f9c80d10f9cc5a8f73be520 Mon Sep 17 00:00:00 2001 From: Kaan Colman Date: Tue, 25 Jun 2024 14:32:26 +0200 Subject: [PATCH 1/2] Change build function to array_replace_recursive Add optional $returnResponseArray in fire function to return full response --- src/Client.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Client.php b/src/Client.php index 469fe6a..805e402 100644 --- a/src/Client.php +++ b/src/Client.php @@ -39,16 +39,16 @@ public function build(Recipient $recipient, Notification $notification = null, D $isPlayload = false; if (!is_null($notification)) { - $result = array_merge($result, $notification()); + $result = array_replace_recursive($result, $notification()); $isPlayload = true; } if (!is_null($data)) { - $result = array_merge($result, $data()); + $result = array_replace_recursive($result, $data()); $isPlayload = true; } if (!is_null($config)) { - $result = array_merge($result, $config()); + $result = array_replace_recursive($result, $config()); } if (!$isPlayload) { @@ -61,7 +61,7 @@ public function build(Recipient $recipient, Notification $notification = null, D /** * Fires built message */ - public function fire() { + public function fire($returnResponseArray = false) { $options = array( 'headers' => array( 'Authorization' => 'Bearer ' . $this -> credentials -> getAccessToken() @@ -74,13 +74,15 @@ public function fire() { // Class name conflict occurs, when used as "Client" $client = new GuzzleHttp\Client($options); $response = $client -> request('POST', $this -> getURL(), $body); + $statusCode = $response -> getStatusCode(); - if ($response -> getStatusCode() == 200) { - return true; - } else { + if(!$returnResponseArray){ + return ($statusCode == 200); + }else{ $result = json_decode($response -> getBody(), true); - return $result['error']['message']; + return array('status' => $statusCode, 'result' => $result); } + } /** From a57b28bd6abc877ea9c837e136e8180846dff8c3 Mon Sep 17 00:00:00 2001 From: Kaan Colman Date: Fri, 12 Jul 2024 12:25:17 +0200 Subject: [PATCH 2/2] Speed-up fire function for bulks, re-use token and Guzzle instance --- src/Client.php | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Client.php b/src/Client.php index 805e402..f6c5012 100644 --- a/src/Client.php +++ b/src/Client.php @@ -62,27 +62,34 @@ public function build(Recipient $recipient, Notification $notification = null, D * Fires built message */ public function fire($returnResponseArray = false) { - $options = array( - 'headers' => array( - 'Authorization' => 'Bearer ' . $this -> credentials -> getAccessToken() - ) - ); + if(!isset($this->header_options)){ + $this->header_options = array( + 'headers' => array( + 'Authorization' => 'Bearer ' . $this -> credentials -> getAccessToken() + ) + ); + } + + $options = $this->header_options; $body = array( self::CONTENT_TYPE => $this -> getPayload(), self::HTTP_ERRORS_OPTION => false ); - // Class name conflict occurs, when used as "Client" - $client = new GuzzleHttp\Client($options); + + if(!isset($this->guzzle_client)){ + // Class name conflict occurs, when used as "Client" + $this->guzzle_client = new GuzzleHttp\Client($options); + } + + $client = $this->guzzle_client; $response = $client -> request('POST', $this -> getURL(), $body); - $statusCode = $response -> getStatusCode(); - if(!$returnResponseArray){ - return ($statusCode == 200); - }else{ + if ($response -> getStatusCode() == 200) { + return true; + } else { $result = json_decode($response -> getBody(), true); - return array('status' => $statusCode, 'result' => $result); + return $result['error']['message']; } - } /**