Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele.corsini@corisnvest.it committed Dec 19, 2024
2 parents ea267c4 + a39e193 commit dc612cb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The result is class **Result** and contain methods:
* **getReasonPhrase()** (string): The reason phrase which typically is sent by servers together with the status code.
* **isSuccessStatusCode()** (bool) : Gets a value that indicates if the HTTP response was successful.
* **getError()** (string) : Get error.
* **getResponseHeaders()** (string) : Gets the raw HTTP headers associated with this response.

## Main features

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
"Corsinvest\\ProxmoxVE\\Api\\": "src/"
}
}
}
}
50 changes: 42 additions & 8 deletions src/PveClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class PveClientBase
*/
private $validateCertificate = false;


/** @var callable[]
*
*/
public $onActionExecuted = [];

/**
* Client constructor.
* @param string $hostname Host Proxmox VE
Expand Down Expand Up @@ -332,7 +338,7 @@ public function delete($resource, $parameters = [])
/**
* @ignore
*/
private function executeAction($resource, $method, $parameters = [])
protected function executeAction($resource, $method, $parameters = [])
{
//url resource
$url = "{$this->getApiUrl()}{$resource}";
Expand All @@ -357,6 +363,7 @@ private function executeAction($resource, $method, $parameters = [])

$headers = [];
$methodType = "";
$data = ""; // default POSTFIELDS value as defined in https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html
$prox_ch = curl_init();
switch ($method) {
case "GET":
Expand All @@ -368,18 +375,28 @@ private function executeAction($resource, $method, $parameters = [])

case "PUT":
curl_setopt($prox_ch, CURLOPT_CUSTOMREQUEST, "PUT");
$data = json_encode($params);
array_push($headers, 'Content-Type: application/json');
array_push($headers, 'Content-Length: ' . strlen($data));

// data from params only if there are any
if (count($params)) {
$data = json_encode($params);
array_push($headers, 'Content-Type: application/json');
array_push($headers, 'Content-Length: ' . strlen($data));
}

curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data);
$methodType = "SET";
break;

case "POST":
curl_setopt($prox_ch, CURLOPT_POST, true);
$data = json_encode($params);
array_push($headers, 'Content-Type: application/json');
array_push($headers, 'Content-Length: ' . strlen($data));

// data from params only if there are any
if (count($params)) {
$data = json_encode($params);
array_push($headers, 'Content-Type: application/json');
array_push($headers, 'Content-Length: ' . strlen($data));
}

curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data);
$methodType = "CREATE";
break;
Expand Down Expand Up @@ -422,6 +439,7 @@ private function executeAction($resource, $method, $parameters = [])
unset($prox_ch);

$body = substr($response, $curlInfo["header_size"]);
$responseHeaders = substr($response, 0, $curlInfo["header_size"]);
unset($response);
unset($curlInfo);

Expand All @@ -448,9 +466,25 @@ private function executeAction($resource, $method, $parameters = [])
$resource,
$parameters,
$methodType,
$this->responseType
$this->responseType,
$responseHeaders,
);

if (is_array($this->onActionExecuted) && count($this->onActionExecuted)) {
foreach ($this->onActionExecuted as $call) {
if (is_callable($call)) {
call_user_func_array($call, [
$this->lastResult, [
'url' => $url,
'method' => $method,
'parameters' => $parameters,
'headers' => $headers
]
]);
}
}
}

if ($this->getDebugLevel() >= 2) {
if (is_array($obj)) {
echo '<pre>';
Expand Down
18 changes: 17 additions & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Result
*/
private $responseType;

/**
* @ignore
*/
private $responseHeaders;

/**
* @ignore
*/
Expand All @@ -64,7 +69,8 @@ public function __construct(
$requestResource,
$requestParameters,
$methodType,
$responseType
$responseType,
$responseHeaders
) {
$this->statusCode = $statusCode;
$this->reasonPhrase = $reasonPhrase;
Expand All @@ -74,6 +80,7 @@ public function __construct(
$this->requestParameters = $requestParameters;
$this->methodType = $methodType;
$this->responseType = $responseType;
$this->responseHeaders = $responseHeaders;
}

/**
Expand Down Expand Up @@ -139,6 +146,15 @@ public function getReasonPhrase()
return $this->reasonPhrase;
}

/**
* Gets the raw HTTP headers associated with this response.
* @return string
*/
public function getResponseHeaders()
{
return $this->responseHeaders;
}

/**
* Gets a value that indicates if the HTTP response was successful.
* @return bool
Expand Down

0 comments on commit dc612cb

Please sign in to comment.