piwigo/tools/ws.htm

387 lines
8.6 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PWG web service explorer</title>
<script type="text/javascript" src="prototype.js" ></script>
<script type="text/javascript">
function setElementText(id, text)
{
if (!text) text="";
var elt = document.getElementById(id);
if (!elt) alert('setElementText '+id);
elt.innerHTML = text;
}
function setVisibility(id, vis)
{
document.getElementById(id).style.visibility = vis;
}
function clearError()
{
setElementText("error", "");
}
function dumpError(err)
{
var s= 'Error ';
if ('string' === typeof err )
s += err;
else
{
s += err.name+'<br/>';
s += err.message;
if (err.stack!=null)
{//mozilla only
s += '<br/><small><pre>'+ err.stack + '</pre></small>';
}
}
setElementText("error", s);
}
var gServiceUrl;
var gCurrentMethodParams;
Ajax.Responders.register({
onException: function(req, err) {
try {
document.pwgError = err;
dumpError(err);
}
catch (e)
{
alert (err);
alert (err.message);
}
},
onComplete: function(req, transport) {
if (!req.responseIsSuccess())
{
var s = 'Completion failure\n' + transport.status + ' ' + transport.statusText;
if (transport.status>=300)
{
s += '\n';
s += transport.responseText.substr(0,1000);
}
dumpError(s);
}
}
}
);
function pwgGetJsonResult(transport)
{
var resp;
try {
eval('resp = ' + transport.responseText);
}
catch (e)
{
var s = e.message;
s += '\n' + transport.responseText.substr(0,1000).escapeHTML();
throw new Error( s );
}
if (resp==null || resp.result==null || resp.stat==null || resp.stat!='ok')
{
var s = 'JSON evaluation error';
if (resp)
{
if (resp.stat!=null) s+= '\n'+resp.stat;
if (resp.message!=null) s+= '\n'+ resp.message;
}
throw new Error(s);
}
return resp.result;
}
function pwgChangeUrl()
{
clearError();
setVisibility("methodListWrapper", "hidden");
setElementText("methodList", "");
setVisibility("methodWrapper", "hidden");
gServiceUrl = $F('ws_url');
gCurrentMethodParams = null;
try {
var ajaxReq = new Ajax.Request(
gServiceUrl,
{method:'get', parameters:'format=json&method=reflection.getMethodList',
onSuccess: function (r) { onSuccess_getMethodList(r); }
}
)
}catch (e)
{
dumpError(e);
}
return false;
}
function onSuccess_getMethodList(transport)
{
var result = pwgGetJsonResult(transport);
var ml = '';
for (var i=0; i<result.methods.length; i++)
{
ml += '<li><a href="#" onclick="return pwgSelectMethod(this.innerHTML)">'+ result.methods[i]+'</a></li>';
}
setElementText("methodList", ml);
setVisibility("methodListWrapper", "visible");
}
function pwgSelectMethod(method)
{
clearError();
setElementText("methodName", method);
setVisibility("methodDetailWrapper", "hidden");
setVisibility("methodWrapper", "visible");
gCurrentMethodParams = null;
try {
var ajaxReq = new Ajax.Request(
gServiceUrl,
{method:'get', parameters:'format=json&method=reflection.getMethodDetails&methodName='+method,
onSuccess: function (r) { onSuccess_getMethodDetails(r); }
}
)
}catch (e)
{
dumpError( e );
}
return false;
}
function onSuccess_getMethodDetails(transport)
{
var result = pwgGetJsonResult(transport);
var methodParamsElt = $("methodParams");
while (methodParamsElt.tBodies[0].rows.length)
methodParamsElt.tBodies[0].deleteRow(methodParamsElt.tBodies[0].rows.length-1);
if (result.params)
{
gCurrentMethodParams = result.params;
if (result.params.length>0)
{
for (var i=0; i<result.params.length; i++)
{
var row = methodParamsElt.tBodies[0].insertRow(-1);
var isOptional = result.params[i].optional;
var defaultValue = result.params[i].defaultValue == null ? '' : result.params[i].defaultValue;
row.insertCell(0).innerHTML = result.params[i].name;
row.insertCell(1).innerHTML = (isOptional ? 'optional':'required');
row.insertCell(2).innerHTML = '<input id="methodParameterSend_'+i+'" type="checkbox" '+(isOptional ? '':'checked="checked"')+'/>';
row.insertCell(3).innerHTML = '<input id="methodParameterValue_'+i+'"" value="'+defaultValue+'" style="width:99%" onchange="$(\'methodParameterSend_'+i+'\').checked=true;"/>';
}
}
}
setElementText("methodDescription", result.description);
setVisibility("methodDetailWrapper", "visible");
}
function pwgInvokeMethod( newWindow )
{
var method = document.getElementById('methodName').innerHTML;
var reqUrl = gServiceUrl;
reqUrl += "?format="+$F('responseFormat');
if (document.getElementById('requestFormat').value == 'get')
{
reqUrl += "&method="+method;
for ( var i=0; i<gCurrentMethodParams.length; i++)
{
if (document.getElementById('methodParameterSend_'+i).checked)
reqUrl += '&'+gCurrentMethodParams[i].name+'='+$F('methodParameterValue_'+i);
}
if ( !newWindow )
document.getElementById("invokeFrame").src = reqUrl;
else
window.open(reqUrl);
}
else
{
var form = document.getElementById("invokeForm");
form.action = reqUrl;
var t = '<input type="hidden" name="'+'method'+'" value="'+method+'"/>';
for ( var i=0; i<gCurrentMethodParams.length; i++)
{
if (document.getElementById('methodParameterSend_'+i).checked)
t += '<input type="hidden" name="'+gCurrentMethodParams[i].name+'" value="'+$F('methodParameterValue_'+i)+'"/>';
}
form.innerHTML = t;
if ( !newWindow )
form.target = "invokeFrame";
else
form.target = "_blank";
form.submit();
}
return false;
}
</script>
<style>
#methodListWrapper {
width: 13em;
float: left;
display: inline;
visibility: hidden;
}
#methodList {
padding-left: 10px;
margin-left: 15px;
}
#methodWrapper {
margin-left: 14em;
visibility: hidden;
}
#methodName {
margin-top: 0;
margin-bottom: 3px;
}
#error {
height: 90px;
overflow: scroll;
color: red;
}
#methodParams {
border-collapse: collapse;
font-size: small;
}
#methodParams input {
font-size: 90%;
border: 1px solid black;
text-indent: 2px;
}
a {
color: #02f;
background-color: white;
text-decoration: underline;
}
a:hover {
color: white;
background-color: #02f;
text-decoration: none;
cursor:pointer;
}
</style>
</head>
<body>
<div>
<label>PWG Web service url
<input name="ws_url" id="ws_url" size="64"/>
<script type="text/javascript">
var match = document.location.toString().match(/^(https?.*\/)tools\/ws\.html?$/);
if (match!=null) $('ws_url').value = match[1]+'ws.php';
</script>
</label>
<a href="#" onclick="return pwgChangeUrl();">Go!</a>
</div>
<div id="error">
</div>
<div>
<div id="methodListWrapper"><h2>Methods</h2>
<ul id="methodList">
<li><a href="#" onclick="return pwgSelectMethod(this.innerHTML)">getVersion</a></li>
</ul>
</div>
<div id="methodWrapper">
<h2 id="methodName"></h2>
<div id="methodDetailWrapper">
<table>
<tr style="vertical-align:top">
<td>
<div id="methodDescription"></div>
<table>
<tr>
<td>Request format:</td>
<td>
<select id="requestFormat">
<option value="get" selected="selected">GET</option>
<option value="post">POST</option>
</select>
</td>
</tr>
<tr>
<td>Response format:</td>
<td>
<select id="responseFormat">
<option value="rest" selected="selected">REST (xml)</option>
<option value="json">JSON</option>
<option value="php">PHP serial</option>
<option value="xmlrpc">XML RPC</option>
</select>
</td>
</tr>
</table>
<p>
<a href="#" onclick="return pwgInvokeMethod(false)">Invoke</a>
<a href="#" onclick="return pwgInvokeMethod(true)">Invoke (new Window)</a>
</p>
</td>
<td>
<table id="methodParams" border="1" cellspacing="0" cellpadding="2px">
<thead>
<tr>
<td style="width:150px">Parameter</td>
<td>Optional</td>
<td>Send</td>
<td style="width:160px">Value</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</td>
</tr>
</table>
<div style="display:none;">
<!-- hiddenForm for POST -->
<form method="post" action="" target="invokeFrame" id="invokeForm">
<input type="submit" value="submit"/>
</form>
</div>
<iframe width="100%" height="400px" id="invokeFrame" name="invokeFrame" style="clear:both"></iframe>
</div> <!-- methodDetailWrapper -->
</div> <!-- methodWrapper -->
</div>
</body>
</html>