From b4d7f0fb376c4e792eef160ecbd25cad035200e6 Mon Sep 17 00:00:00 2001 From: padams Date: Tue, 9 Nov 2021 00:56:02 +0000 Subject: [PATCH] refactoring return values of Sites client methods and adding better error handling for guzzle requests. --- src/OwaClient.php | 90 ++++++++++++++++++++++++++++++++++++++- src/Sites/SitesClient.php | 11 +++-- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/OwaClient.php b/src/OwaClient.php index dfca32e..665853b 100644 --- a/src/OwaClient.php +++ b/src/OwaClient.php @@ -2,10 +2,14 @@ namespace OwaSdk; use GuzzleHttp\Client; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\ClientException; class OwaClient { var $config = []; + var $last_response = null; public function __construct( $config ) { @@ -183,9 +187,91 @@ public function makeRequest( $params ) { ]; - $res = $http->request( $params['http_method'], $uri, $request_options ); + $res = null; - return $res; + try{ + + $res = $http->request( $params['http_method'], $uri, $request_options ); + + + } + + catch( \GuzzleHttp\Exception\RequestException $e ) { + + $r = $e->getRequest(); + $res = null; + + error_log( print_r( $r, true ) ); + + if ( $e->hasResponse() ) { + + $res = $e->getResponse(); + + error_log( print_r( $res, true ) ); + } + + if ( $this->getSetting( 'debug' ) ) { + + print_r($r); + print_r($res); + } + } + + catch( \GuzzleHttp\Exception\ConnectException $e ) { + + $r = $e->getRequest(); + $res = null; + + error_log( print_r( $r, true ) ); + + if ( $e->hasResponse() ) { + + $res = $e->getResponse(); + + error_log( print_r( $res, true ) ); + } + + if ( $this->getSetting( 'debug' ) ) { + + print_r($r); + print_r($res); + } + } + + catch( \GuzzleHttp\Exception\ClientException $e ) { + + $r = $e->getRequest(); + $res = null; + + error_log( print_r( $r, true ) ); + + if ( $e->hasResponse() ) { + + $res = $e->getResponse(); + + error_log( print_r( $res, true ) ); + } + + if ( $this->getSetting( 'debug' ) ) { + + print_r($r); + print_r($res); + } + } + + + if ( $res ) { + + $this->last_response = $res ; + + $b = $res->getBody(); + $b = json_decode( $b, true); + + if ( array_key_exists( 'data', $b ) ) { + + return $b['data']; + } + } } public static function setDefaultParams( $defaults, $params ) { diff --git a/src/Sites/SitesClient.php b/src/Sites/SitesClient.php index d4b35b7..308f6bb 100644 --- a/src/Sites/SitesClient.php +++ b/src/Sites/SitesClient.php @@ -36,7 +36,11 @@ public function listSites() { 'uri' => '/base/v1/sites' ]; - return $this->makeRequest( $request ); + $res = $this->makeRequest( $request ); + + return $res; + + } public function addSite( $params ) { @@ -52,7 +56,6 @@ public function addSite( $params ) { $params = self::setDefaultParams( $defaults, $params ); - $request = [ 'http_method' => 'POST', @@ -60,7 +63,9 @@ public function addSite( $params ) { 'form_params' => $params ]; - return $this->makeRequest( $request ); + $res = $this->makeRequest( $request ); + + return $res; } }