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

JsonCollector: Introduce GetOptionalHeaders() and GetCurlOptions() methods #44

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
48 changes: 45 additions & 3 deletions core/jsoncollector.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,13 @@ public function Prepare()
} else {
$aDataGet = [];
}
$iSynchroTimeout = (int)Utils::GetConfigurationValue('itop_synchro_timeout', 600); // timeout in seconds, for a synchro to run
$aCurlOptions = Utils::GetCurlOptions($iSynchroTimeout);

$aCurlOptions = static::GetCurlOptions();
jbostoen marked this conversation as resolved.
Show resolved Hide resolved


//logs
Utils::Log(LOG_DEBUG, 'Source aDataGet: '.json_encode($aDataGet));
$this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, '', $aResponseHeaders, $aCurlOptions);
$this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, static::GetOptionalHeaders(), $aResponseHeaders, $aCurlOptions);
jbostoen marked this conversation as resolved.
Show resolved Hide resolved
Utils::Log(LOG_DEBUG, 'Source sFileJson: '.$this->sFileJson);
Utils::Log(LOG_INFO, 'Synchro URL (target): '.Utils::GetConfigurationValue('itop_url', array()));
} else {
Expand Down Expand Up @@ -258,6 +259,47 @@ public function Fetch()

return false;
}

/**
* @return Optional HTTP headers which can be sent to the specified URL.
Molkobain marked this conversation as resolved.
Show resolved Hide resolved
* For example, a subclass can be override this method to send an Authorization: header.
jbostoen marked this conversation as resolved.
Show resolved Hide resolved
*/
public function GetOptionalHeaders() : string
{
return '';
}

/**
* @return cURL options to use.
* For example, a subclass can be override this method to add the CURLOPT_USERPWD and a value.
*/
public function GetCurlOptions() : array
{
$iSynchroTimeout = (int)Utils::GetConfigurationValue('itop_synchro_timeout', 600); // timeout in seconds, for a synchro to run
$aParamsSourceJson = $this->aCollectorConfig;

// Step 1:
// Use cURL options from "config" XML.
$aConfigCurlOptions = Utils::GetCurlOptions($iSynchroTimeout);

// Step 2:
// If available: Add or update options defined in the "collector" XML.
// Apply the same mechanism to convert constants. The flexibility in place is to allow adding and overriding values, so do not pass on synchro timeout.
$aCollectorCurlOptions = array();

if(isset($aParamsSourceJson['curl_options'])) {
$aCollectorCurlOptions = $aParamsSourceJson['curl_options'];
}
if(isset($aParamsSourceJson['CURL_OPTIONS'])) {
$aCollectorCurlOptions = $aParamsSourceJson['CURL_OPTIONS'];
}
Molkobain marked this conversation as resolved.
Show resolved Hide resolved

if(count($aCollectorCurlOptions) > 0) {
$aCollectorCurlOptions = utils::GetCurlOptions($aCollectorCurlOptions, -1);
jbostoen marked this conversation as resolved.
Show resolved Hide resolved
}

return array_replace($aConfigCurlOptions, $aCollectorCurlOptions);
}

/**
* @param array $aData
Expand Down