Skip to content

Commit

Permalink
refactoring return values of Sites client methods and adding better e…
Browse files Browse the repository at this point in the history
…rror handling for guzzle requests.
  • Loading branch information
padams committed Nov 9, 2021
1 parent 2546929 commit b4d7f0f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
90 changes: 88 additions & 2 deletions src/OwaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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 ) {
Expand Down
11 changes: 8 additions & 3 deletions src/Sites/SitesClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -52,15 +56,16 @@ public function addSite( $params ) {

$params = self::setDefaultParams( $defaults, $params );


$request = [

'http_method' => 'POST',
'uri' => '/base/v1/sites',
'form_params' => $params
];

return $this->makeRequest( $request );
$res = $this->makeRequest( $request );

return $res;
}

}
Expand Down

0 comments on commit b4d7f0f

Please sign in to comment.