mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-22 00:32:16 +02:00
Reintegrating the REST web services in the 2.0 branch
SVN:2.0[2602]
This commit is contained in:
166
webservices/itoprest.examples.php
Normal file
166
webservices/itoprest.examples.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
// Copyright (C) 2010-2012 Combodo SARL
|
||||
//
|
||||
// This file is part of iTop.
|
||||
//
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// iTop is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with iTop. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
|
||||
/**
|
||||
* Shows a usage of the SOAP queries
|
||||
*
|
||||
* @copyright Copyright (C) 2010-2012 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper to execute an HTTP POST request
|
||||
* Source: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
|
||||
* originaly named after do_post_request
|
||||
*/
|
||||
function DoPostRequest($sUrl, $aData, $sOptionnalHeaders = null)
|
||||
{
|
||||
// $sOptionnalHeaders is a string containing additional HTTP headers that you would like to send in your request.
|
||||
|
||||
$sData = http_build_query($aData);
|
||||
|
||||
$aParams = array('http' => array(
|
||||
'method' => 'POST',
|
||||
'content' => $sData,
|
||||
'header'=> "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($sData)."\r\n",
|
||||
));
|
||||
if ($sOptionnalHeaders !== null)
|
||||
{
|
||||
$aParams['http']['header'] .= $sOptionnalHeaders;
|
||||
}
|
||||
$ctx = stream_context_create($aParams);
|
||||
|
||||
$fp = @fopen($sUrl, 'rb', false, $ctx);
|
||||
if (!$fp)
|
||||
{
|
||||
global $php_errormsg;
|
||||
if (isset($php_errormsg))
|
||||
{
|
||||
throw new Exception("Problem with $sUrl, $php_errormsg");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Problem with $sUrl");
|
||||
}
|
||||
}
|
||||
$response = @stream_get_contents($fp);
|
||||
if ($response === false)
|
||||
{
|
||||
throw new Exception("Problem reading data from $sUrl, $php_errormsg");
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
// If the library curl is installed.... use this function
|
||||
//
|
||||
function DoPostRequest_curl($sUrl, $aData)
|
||||
{
|
||||
$curl = curl_init($sUrl);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $aData);
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Main program
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Define the operations to perform (one operation per call the rest service)
|
||||
//
|
||||
|
||||
$aOperations = array(
|
||||
array(
|
||||
'operation' => 'list_operations', // operation code
|
||||
),
|
||||
array(
|
||||
'operation' => 'core/create', // operation code
|
||||
'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
|
||||
'class' => 'UserRequest',
|
||||
'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
|
||||
// Values for the object to create
|
||||
'fields' => array(
|
||||
'org_id' => "SELECT Organization WHERE name = 'Demo'",
|
||||
'caller_id' => array('name' => 'monet', 'first_name' => 'claude'),
|
||||
'title' => 'issue blah',
|
||||
'description' => 'something happened'
|
||||
),
|
||||
),
|
||||
array(
|
||||
'operation' => 'core/update', // operation code
|
||||
'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
|
||||
'class' => 'UserRequest',
|
||||
'key' => 'SELECT UserRequest WHERE id=1',
|
||||
'output_fields' => 'id, friendlyname, title', // list of fields to show in the results (* or a,b,c)
|
||||
// Values for the object to create
|
||||
'fields' => array(
|
||||
'title' => 'Issue #'.rand(0, 100),
|
||||
'contacts_list' => array(
|
||||
array(
|
||||
'role' => 'fireman #'.rand(0, 100),
|
||||
'contact_id' => array('finalclass' => 'Person', 'name' => 'monet', 'first_name' => 'claude'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'operation' => 'core/get', // operation code
|
||||
'class' => 'UserRequest',
|
||||
'key' => 'SELECT UserRequest',
|
||||
'output_fields' => 'id, friendlyname, title, contacts_list', // list of fields to show in the results (* or a,b,c)
|
||||
),
|
||||
);
|
||||
|
||||
$sUrl = "http://localhost/rest-services/webservices/rest.php?version=1.0";
|
||||
|
||||
$aData = array();
|
||||
$aData['auth_user'] = 'admin';
|
||||
$aData['auth_pwd'] = 'admin';
|
||||
|
||||
foreach ($aOperations as $iOp => $aOperation)
|
||||
{
|
||||
echo "======================================\n";
|
||||
echo "Operation #$iOp: ".$aOperation['operation']."\n";
|
||||
$aData['json_data'] = json_encode($aOperation);
|
||||
|
||||
echo "--------------------------------------\n";
|
||||
echo "Input:\n";
|
||||
print_r($aOperation);
|
||||
|
||||
$response = DoPostRequest($sUrl, $aData);
|
||||
$aResults = json_decode($response);
|
||||
if ($aResults)
|
||||
{
|
||||
echo "--------------------------------------\n";
|
||||
echo "Reply:\n";
|
||||
print_r($aResults);
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "ERROR rest.php replied:\n";
|
||||
echo $response;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
209
webservices/rest.php
Normal file
209
webservices/rest.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
// Copyright (C) 2010-2013 Combodo SARL
|
||||
//
|
||||
// This file is part of iTop.
|
||||
//
|
||||
// iTop is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// iTop is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with iTop. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
/**
|
||||
* Entry point for all the REST services
|
||||
*
|
||||
* --------------------------------------------------
|
||||
* Create an object
|
||||
* --------------------------------------------------
|
||||
* POST itop/webservices/rest.php
|
||||
* {
|
||||
* operation: 'object_create',
|
||||
* comment: 'Synchronization from blah...',
|
||||
* class: 'UserRequest',
|
||||
* results: 'id, friendlyname',
|
||||
* fields:
|
||||
* {
|
||||
* org_id: 'SELECT Organization WHERE name = "Demo"',
|
||||
* caller_id:
|
||||
* {
|
||||
* name: 'monet',
|
||||
* first_name: 'claude',
|
||||
* }
|
||||
* title: 'Houston, got a problem!',
|
||||
* description: 'The fridge is empty'
|
||||
* contacts_list:
|
||||
* [
|
||||
* {
|
||||
* role: 'pizza delivery',
|
||||
* contact_id:
|
||||
* {
|
||||
* finalclass: 'Person',
|
||||
* name: 'monet',
|
||||
* first_name: 'claude'
|
||||
* }
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
* }
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (C) 2010-2013 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
|
||||
require_once(__DIR__.'/../approot.inc.php');
|
||||
require_once(APPROOT.'/application/application.inc.php');
|
||||
require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
|
||||
require_once(APPROOT.'core/restservices.class.inc.php');
|
||||
|
||||
|
||||
/**
|
||||
* Result structure that is specific to the hardcoded verb 'list_operations'
|
||||
*/
|
||||
class RestResultListOperations extends RestResult
|
||||
{
|
||||
public $version;
|
||||
public $operations;
|
||||
|
||||
public function AddOperation($sVerb, $sDescription, $sServiceProviderClass)
|
||||
{
|
||||
$this->operations[] = array(
|
||||
'verb' => $sVerb,
|
||||
'description' => $sDescription,
|
||||
'extension' => $sServiceProviderClass
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Main
|
||||
//
|
||||
$oP = new ajax_page('rest');
|
||||
|
||||
try
|
||||
{
|
||||
utils::UseParamFile();
|
||||
|
||||
$sAuthUser = utils::ReadParam('auth_user', null, false, 'raw_data');
|
||||
$sAuthPwd = utils::ReadParam('auth_pwd', null, false, 'raw_data');
|
||||
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
|
||||
{
|
||||
UserRights::Login($sAuthUser); // Login & set the user's language
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid login '$sAuthUser'", RestResult::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$sVersion = utils::ReadParam('version', null, false, 'raw_data');
|
||||
if ($sVersion == null)
|
||||
{
|
||||
throw new Exception("Missing parameter 'version' (e.g. '1.0')", RestResult::MISSING_VERSION);
|
||||
}
|
||||
|
||||
$sJsonString = utils::ReadParam('json_data', null, false, 'raw_data');
|
||||
if ($sJsonString == null)
|
||||
{
|
||||
throw new Exception("Missing parameter 'json_data", RestResult::MISSING_JSON);
|
||||
}
|
||||
$aJsonData = json_decode($sJsonString);
|
||||
if ($aJsonData == null)
|
||||
{
|
||||
throw new Exception("Parameter json_data is not a valid JSON structure", RestResult::INVALID_JSON);
|
||||
}
|
||||
|
||||
|
||||
$aProviders = array();
|
||||
foreach(get_declared_classes() as $sPHPClass)
|
||||
{
|
||||
$oRefClass = new ReflectionClass($sPHPClass);
|
||||
if ($oRefClass->implementsInterface('iRestServiceProvider'))
|
||||
{
|
||||
$aProviders[] = new $sPHPClass;
|
||||
}
|
||||
}
|
||||
|
||||
$aOpToRestService = array(); // verb => $oRestServiceProvider
|
||||
foreach ($aProviders as $oRestSP)
|
||||
{
|
||||
$aOperations = $oRestSP->ListOperations($sVersion);
|
||||
foreach ($aOperations as $aOpData)
|
||||
{
|
||||
$aOpToRestService[$aOpData['verb']] = array
|
||||
(
|
||||
'service_provider' => $oRestSP,
|
||||
'description' => $aOpData['description'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($aOpToRestService) == 0)
|
||||
{
|
||||
throw new Exception("There is no service available for version '$sVersion'", RestResult::UNSUPPORTED_VERSION);
|
||||
}
|
||||
|
||||
|
||||
$sOperation = RestUtils::GetMandatoryParam($aJsonData, 'operation');
|
||||
if ($sOperation == 'list_operations')
|
||||
{
|
||||
$oResult = new RestResultListOperations();
|
||||
$oResult->message = "Operations: ".count($aOpToRestService);
|
||||
$oResult->version = $sVersion;
|
||||
foreach ($aOpToRestService as $sVerb => $aOpData)
|
||||
{
|
||||
$oResult->AddOperation($sVerb, $aOpData['description'], get_class($aOpData['service_provider']));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!array_key_exists($sOperation, $aOpToRestService))
|
||||
{
|
||||
throw new Exception("Unknown verb '$sOperation' in version '$sVersion'", RestResult::UNKNOWN_OPERATION);
|
||||
}
|
||||
$oRS = $aOpToRestService[$sOperation]['service_provider'];
|
||||
|
||||
$oResult = $oRS->ExecOperation($sVersion, $sOperation, $aJsonData);
|
||||
}
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
$oResult = new RestResult();
|
||||
if ($e->GetCode() == 0)
|
||||
{
|
||||
$oResult->code = RestResult::INTERNAL_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
$oResult->code = $e->GetCode();
|
||||
}
|
||||
$oResult->message = "Error: ".$e->GetMessage();
|
||||
}
|
||||
|
||||
// Output the results
|
||||
//
|
||||
$oP->add_header('Access-Control-Allow-Origin: *');
|
||||
|
||||
$sCallback = utils::ReadParam('callback', null);
|
||||
if ($sCallback == null)
|
||||
{
|
||||
$oP->SetContentType('application/json');
|
||||
$oP->add(json_encode($oResult));
|
||||
}
|
||||
else
|
||||
{
|
||||
$oP->SetContentType('application/javascript');
|
||||
$oP->add($sCallback.'('.json_encode($oResult).')');
|
||||
}
|
||||
$oP->Output();
|
||||
?>
|
||||
Reference in New Issue
Block a user