Skip to content

Commit

Permalink
Merge pull request #17 from Xelon-AG/feat/add-request-event
Browse files Browse the repository at this point in the history
add request event, fix client props checking
  • Loading branch information
gazhur94 authored Jun 20, 2023
2 parents 08723a3 + 5763c35 commit 3cc5cb4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Events/RequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Xelon\VmWareClient\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RequestEvent
{
use Dispatchable;
use SerializesModels;

public string $requestBody;

public string $responseBody;

public string $status;

public function __construct($requestBody, $responseBody, $status)
{
$this->requestBody = $requestBody;
$this->responseBody = $responseBody;
$this->status = $status ? 'Success' : 'Error';
}
}
15 changes: 14 additions & 1 deletion src/Requests/SoapRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use stdClass;
use Xelon\VmWareClient\Events\RequestEvent;
use Xelon\VmWareClient\Transform\SoapTransform;

trait SoapRequest
Expand All @@ -23,6 +24,12 @@ public function request(string $method, array $requestBody, bool $convertToSoap
try {
$response = $this->soapClient->$method($convertToSoap ? $this->arrayToSoapVar($requestBody) : $requestBody);

RequestEvent::dispatch(
property_exists($this->soapClient, '__last_request') ? $this->soapClient->__last_request : '',
property_exists($this->soapClient, '__last_response') ? $this->soapClient->__last_response : '',
true
);

if (config('vmware-php-client.enable_logs')) {
Log::info(
'SOAP REQUEST SUCCESS:'.
Expand All @@ -35,6 +42,12 @@ public function request(string $method, array $requestBody, bool $convertToSoap

return $response;
} catch (\Exception $exception) {
RequestEvent::dispatch(
property_exists($this->soapClient, '__last_request') ? $this->soapClient->__last_request : '',
property_exists($this->soapClient, '__last_response') ? $this->soapClient->__last_response : '',
false
);

if ($exception->getMessage() === 'The session is not authenticated.' && $this->tries < $this->maxTries) {
$this->tries++;
$sessionInfo = Cache::get("vcenter-soap-session-$this->ip");
Expand Down Expand Up @@ -64,7 +77,7 @@ public function request(string $method, array $requestBody, bool $convertToSoap
? "\nSOAP request start***".$this->soapClient->__last_request.'***SOAP request end'
: ''
).(
property_exists($this->soapClient, '__last_request')
property_exists($this->soapClient, '__last_response')
? "\nSOAP response start***: ".$this->soapClient->__last_response.'***SOAP response end'
: ''
);
Expand Down
4 changes: 2 additions & 2 deletions src/Transform/SoapTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function arrayToSoapVar(array $array): array
unset($value['@type']);
}

if (array_key_exists('type', $value)) {
if (array_key_exists('_', $value) && array_key_exists('type', $value)) {
$data[$key] = new SoapVar($value['_'], null, $value['type'], '', $key, '');

continue;
Expand All @@ -52,7 +52,7 @@ public function arrayToSoapVar(array $array): array
unset($childItem['@type']);
}

if (array_key_exists('type', $childItem)) {
if (array_key_exists('_', $childItem) && array_key_exists('type', $childItem)) {
$data[$key] = new SoapVar($childItem['_'], null, $childItem['type'], '', $key, '');

continue;
Expand Down

0 comments on commit 3cc5cb4

Please sign in to comment.