diff --git a/core/restclient.class.inc.php b/core/restclient.class.inc.php index 5b641177..b2257410 100644 --- a/core/restclient.class.inc.php +++ b/core/restclient.class.inc.php @@ -129,21 +129,29 @@ protected static function ExecOperation($aOperation, $sVersion = '1.0') return $aResults; } - public static function GetNewestKnownVersion() - { - $sNewestVersion = '1.0'; + + /** + * Gets the iTop API version in use. It tries all the known versions, starting from the newest one. + * + * @return void + * @throws Exception If no supported version is found. + */ + public static function GetNewestKnownVersion() { + $oC = new RestClient(); - $aKnownVersions = array('1.0', '1.1', '1.2', '2.0'); + // Order: Put the newest versions first. + $aKnownVersions = array('1.4', '1.3', '1.2', '1.1', '1.0'); foreach ($aKnownVersions as $sVersion) { $oC->SetVersion($sVersion); $aRet = $oC->ListOperations(); if ($aRet['code'] == 0) { - // Supported version - $sNewestVersion = $sVersion; + // A non-error code (0) means the version is supported. + return $sVersion; } } - return $sNewestVersion; + throw new Exception("No supported iTop API version found (tried: ".implode(', ', $aKnownVersions).")"); + } /**