Skip to content

Commit

Permalink
Merge pull request #31 from noxomix/master
Browse files Browse the repository at this point in the history
Add Timeout for cURL
  • Loading branch information
franklupo authored Feb 28, 2024
2 parents c42f8ff + b890fc8 commit 181f32a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The result is class **Result** and contain methods:
* ClientBase lite function
* Form Proxmox VE 6.2 support Api Token for user
* Login with One-time password for Two-factor authentication.
* Set Timeout for the Connection.

## Api token

Expand Down Expand Up @@ -157,6 +158,9 @@ if($client->login('root','password','pam')){
//result json result
$client->setResponseType('json');
var_dump($client->get('/version')->getResponse());

//set connection timeout (by default no timeout)
$client->setTimeout(2)->get('/version')->getResponse();
}

```
Expand Down
32 changes: 32 additions & 0 deletions src/PveClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
class PveClientBase
{

/**
* Set default to 0 ... which means no timeout limit at all
* @ignore
*/
private $timeout = 0;

/**
* @ignore
*/
Expand Down Expand Up @@ -72,6 +78,24 @@ function __construct($hostname, $port = 8006)
$this->port = $port;
}

/**
* Set timeout in seconds
* @return $this
*/
public function setTimeout($timeout) {
$this->timeout = $timeout;
return $this;
}

/**
* Get timeout in seconds.
* @return int
*/
public function getTimeout() {
return $this->timeout;
}


/**
* Return if result is object
* @return bool
Expand Down Expand Up @@ -341,6 +365,14 @@ private function executeAction($resource, $method, $parameters = [])
curl_setopt($prox_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($prox_ch, CURLOPT_SSL_VERIFYHOST, false);

/**
* Set the Timeout for the Request in curl | CURLOPT_TIMEOUT sets the maximum execution time of the cUrl function.
*/
$timeout = $this->timeout;
if($timeout != 0) {
curl_setopt($prox_ch, CURLOPT_TIMEOUT, $timeout);
}

if (isset($this->ticketPVEAuthCookie)) {
array_push($headers, "CSRFPreventionToken: {$this->ticketCSRFPreventionToken}");
}
Expand Down
3 changes: 3 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
echo "\n" . $node->id;
}

/* Timeout test 2 seconds */
var_dump($client->setTimeout(2)->getVersion()->version()->getResponse());

$client->getNodes()->get("cc01")->getQemu()->get(1006)->getAgent()->getExec()->exec(array("powershell", "-command", "echo", "test"));

// foreach ($client->getNodes()->get("cv-pve01")->getQemu()->vmlist()->getResponse()->data as $vm) {
Expand Down

0 comments on commit 181f32a

Please sign in to comment.