Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add request event, fix client props checking #17

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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