source: trunk/tools/ws.htm @ 25077

Last change on this file since 25077 was 25077, checked in by mistic100, 11 years ago

feature:2982 API: add high-level type check
introduces some constants fro bool, int, float, positive and notnull parameters
types are tested in PwgServer::invoke and no in each method
+ some optimizations + update methods descriptions

  • Property svn:eol-style set to LF
File size: 17.9 KB
Line 
1<!DOCTYPE html>
2<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
3<head>
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5  <title>Piwigo web API (web-services) explorer</title>
6 
7  <link rel="stylesheet" type="text/css" href="http://cdn.jsdelivr.net/tiptip/1.3/tipTip.css">
8 
9  <style type="text/css">
10  /* BEGIN CSS RESET
11    http://meyerweb.com/eric/tools/css/reset
12    v2.0 | 20110126 | License: none (public domain) */
13  html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code,
14  del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li,
15  fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, 
16  figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video
17  {margin:0;padding:0;border:0;font-size:100%;vertical-align:baseline;}
18
19  article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {display:block;}
20  body {line-height:1.1;}
21  blockquote, q {quotes:none;}
22  blockquote:before, blockquote:after, q:before, q:after {content:'';content:none;}
23  table {border-collapse:collapse;border-spacing:0;}
24  /* END CSS RESET */
25
26  html {font-family:"Corbel","Lucida Grande","Verdana",sans-serif;color:#222;font-size:13px;}
27
28  a {color:#247EBF;text-decoration:none;}
29  a:hover {color:#EB9C39;border-bottom-width:1px;border-style:dotted;text-shadow:1px 1px 0 #ddd;}
30 
31  blockquote {border:1px solid #cdcdcd;background:#F9F9F9;padding:8px;}
32  hr {margin:10px 30px;color:#fff;}
33  ul {margin-left:25px;}
34  p {margin:8px 0;}
35
36  h1 {color:#fff;font-size:26px;padding:10px 15px;text-shadow:1px 1px 0 #999;
37    background:#45484d;background:linear-gradient(to bottom, #45484d 0%,#333333 100%);
38  }
39  h2 {color:#fff;font-size:20px;padding:5px 10px;text-shadow:1px 1px 0 #555;
40    background:#f2a841;background:linear-gradient(to bottom, #f2a841 0%,#ef6b13 100%);
41  }
42  h2#errorWrapper {color:#F42C00;font-weight:normal;
43    background:#eaeaea;background:linear-gradient(to bottom, #eaeaea 0%, #afafaf 100%);
44  }
45  h3 {display:inline-block;padding:5px 10px;color:#555;font-weight:bold;text-align:center;
46    border-radius:8px 8px 0 0;text-shadow:1px 1px 0 #bbb;
47    background:#f2f2f2;background:linear-gradient(to bottom, #f2f2f2 0%,#cecece 100%);
48  }
49
50  #the_header {border-bottom:1px solid #cdcdcd;margin-bottom:1px;}
51  #the_footer {background:#EAEAEA;border-top:1px solid #cdcdcd;padding:10px;clear:both;}
52
53  #the_methods {width:250px;float:left;border-style:solid;border-color:#cdcdcd;border-width:1px 1px 0 0;
54    background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAH0lEQVQImSXHMQEAMAwCMOrfK0jIjuVL2gLBzyHJtgd7wBdU3Vt/7AAAAABJRU5ErkJggg==);
55  }
56  #methodsList {font-size:1.1em;margin:5px 0 10px 10px;list-style:none;}
57  #methodsList li:before {content:"\203A\00A0";font-weight:bold;color:#EB9C39;font-size:1.1em;}
58  #methodsList li:hover:before {content:"\00A0\203A";}
59
60  #the_page {margin-left:252px;border-style:solid;border-color:#cdcdcd;border-width:1px 0 0 1px;}
61  #the_content {padding:10px;}
62
63  #methodParams {display:inline-block;}
64  #methodParams thead td {background:#DEE3E9;font-weight:bold;padding:2px 5px;}
65  #methodParams td {padding:2px;border:1px solid #cdcdcd;vertical-align:middle;}
66  #methodParams tbody tr:nth-child(even) {background:#f7f7f7;}
67  #methodParams tbody tr td:first-child {font-family:monospace;font-size:0.95em;}
68  #methodParams td.mini {width:0px;text-align:center;}
69  #methodParams tfoot {font-size:0.95em;}
70  #methodParams td.input {text-align:center;}
71  #methodParams td.input input[type="text"] {width:97%;font-size:0.9em;background:#f7f7f7;border:1px solid #ccc;border-radius:2px;}
72  #methodParams td.input input[type="text"]:hover, #methodParams td.input input[type="text"]:focus {border-color:#C7E2F1;border-top-color:#96BCD7;background:#fff;}
73  #methodParams .type {display:inline-block;width:16px;height:16px;font-size:12px;line-height:16px;background:#ddd;border-radius:8px;font-weight:bold;text-align:center;color:#222;}
74  #methodParams .subtype {vertical-align:super;}
75
76  #testForm {display:inline-block;margin-left:15px;}
77  #testForm td {padding:2px 0;}
78  #testForm tr:last-child td {padding:8px 0 5px 0;}
79  #testForm blockquote {width:200px;}
80 
81  #introMessage {font-size:1.1em;}
82  #urlForm {margin-bottom:10px;}
83
84  a.button {color:#fff;padding:3px 8px;border:1px solid #91bb5c;font-size:0.9em;margin-right:3px;display:inline-block;
85    border-radius:5px;text-shadow:1px 1px 0 #666;
86    background:#84bb3c;background:linear-gradient(to bottom, #84bb3c 0%, #3f5a1d 100%);
87  }
88  a.button:hover {color:#E5FF00;}
89 
90  .methodInfo {float:right;display:inline-block;width:16px;height:16px;font-size:12px;line-height:16px;background:#555;border-radius:8px;font-family:"Times New Roman",sans-serif;font-style:italic;font-weight:bold;text-align:center;color:#fff;}
91  .methodInfo:hover {border:none;text-shadow:none;background:#888;cursor:pointer;color:#fff;}
92  #tiptip_content { font-size:12px; }
93 
94  #iframeWrapper {width:100%;height:300px;padding:3px 3px 20px 3px;background:#F9F9F9;border:1px solid #cdcdcd;overflow:hidden;position:relative;}
95  iframe {width:100%;height:100%;background:#fff;}
96  </style>
97 
98</head>
99
100<body>
101
102<div id="the_header">
103  <h1>Piwigo web API (web-services) explorer</h1>
104</div> <!-- the_header -->
105
106<div id="the_methods">
107  <h2>Available methods</h2>
108 
109  <ul id="methodsList">
110  </ul>
111</div> <!-- the_methods -->
112
113<div id="the_page">
114  <h2 id="methodName" style="display:none;"></h2>
115  <h2 id="errorWrapper" style="display:none;"></h2>
116 
117  <div id="the_content">
118    <form id="urlForm" style="display:none;">
119      <input type="text" name="ws_url" size="60">
120      <input type="submit" value="Go!">
121    </form>
122   
123    <blockquote id="introMessage">
124      <p>
125        <b>API = Application Programming Interface.</b><br>
126        This is the way other applications can communicate with Piwigo. This feature is also know as Web Services.
127      </p>
128
129      <p>Examples:</p>
130      <ul>
131        <li>Wordpress (web blog software) can display random photos from a Piwigo gallery in its sidebar</li>
132        <li>Lightroom (photo management software for desktop) can create albums and upload photos to Piwigo</li>
133      </ul>
134
135      <p>
136        This page lists all API methods available on your Piwigo installation, part of the Piwigo core or added by third-party plugins.
137        For each method you can consult required and optional parameters, and even test them in direct live!
138      </p>
139
140      <p>
141        For more information you can consult our Wiki <a href="http://piwigo.org/doc/doku.php?id=dev:webapi:start" target="_blank">Piwigo Web API</a> and <a href="http://piwigo.org/forum" target="_blank">our forums</a>.
142      </p>
143    </blockquote> <!-- introMessage -->
144
145
146    <form id="methodWrapper" style="display:none;">
147      <div id="methodDescription" style="display:none;">
148        <h3>Description</h3>
149        <blockquote>
150        </blockquote>
151        <br>
152      </div> <!-- methodDescription -->
153     
154      <div id="methodParams">
155        <h3>Method parameters</h3>
156        <table>
157          <thead>
158            <tr>
159              <td style="width:150px;">Name</td>
160              <td class="mini">Extra</td>
161              <td class="mini">Type</td>
162              <td style="width:300px;">Value</td>
163              <td class="mini">Send</td>
164            </tr>
165          </thead>
166         
167          <tbody>
168          </tbody>
169         
170          <tfoot>
171            <tr>
172              <td colspan="5">
173                <b>*</b>: required, <b>?</b>: optional, <b>[]</b>: can be an array (use a pipe | to split values)<br>
174                <b>B</b>: boolean, <b>I</b>: integer, <b>F</b>: float, <b>+</b>: positive, <b>&oslash;</b>: not null
175              </td>
176            </tr>
177          </tfoot>
178        </table>
179      </div> <!-- methodParams -->
180     
181      <div id="testForm">
182        <h3>Test</h3>
183        <blockquote>
184          <table>
185            <tr>
186              <td>Request format :</td>
187              <td>
188                <select id="requestFormat">
189                  <option value="get" selected="selected">GET</option>
190                  <option value="post">POST</option>
191                </select>
192              </td>
193            </tr>
194            <tr>
195              <td>Response format :</td>
196              <td>
197                <select id="responseFormat">
198                  <option value="rest" selected="selected">REST (xml)</option>
199                  <option value="json">JSON</option>
200                  <option value="php">PHP serial</option>
201                  <option value="xmlrpc">XML RPC</option>
202                </select>
203              </td>
204            </tr>
205            <tr>
206              <td colspan="2">
207                <a href="#" class="button" id="invokeMethod">INVOKE</a>
208                <a href="#" class="button" id="invokeMethodBlank">INVOKE (new window)</a>
209              </td>
210            </tr>
211          </table>
212        </blockquote>
213      </div> <!-- testForm -->
214     
215      <br><br>
216     
217      <h3>Result</h3>
218      <div id="iframeWrapper">
219        <iframe src="" id="invokeFrame" name="invokeFrame"></iframe>
220        <a href="#bottom" id="increaseIframe"><b>&darr;</b> increase height</a> &#8226; <a href="#bottom" id="decreaseIframe"><b>&uarr;</b> decrease height</a>
221        <a name="bottom"></a>
222      </div>
223    </form> <!-- iframeWrapper -->
224   
225    <!-- hidden form for POST submition -->
226    <form method="post" action="" target="" id="invokeForm" style="display:none;"></form>
227   
228  </div> <!-- the_content -->
229
230</div> <!-- the_page -->
231
232<div id="the_footer">
233  Copyright &copy; 2002-2013 <a href="http://piwigo.org">Piwigo Team</a>
234</div> <!-- the_footer -->
235
236<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
237<script type="text/javascript" src="https://cdn.jsdelivr.net/tiptip/1.3/jquery.tipTip.minified.js"></script>
238
239<script type="text/javascript"> 
240// global vars
241var cachedMethods = new Array;
242var ws_url = "http://";
243
244// automatic detection of ws_url
245match = document.location.toString().match(/^(https?.*\/)tools\/ws\.html?/);
246if (match==null) {
247  askForUrl();
248}
249else {
250  ws_url = match[1]+'ws.php';
251  getMethodList();
252}
253
254// manual set of ws_url
255$("#urlForm").submit(function() {
256  ws_url = $(this).children("input[name='ws_url']").val();
257  getMethodList();
258  return false;
259});
260
261// invoke buttons
262$("#invokeMethod").click(function() {
263  invokeMethod($("#methodName").html(), false);
264  return false;
265});
266$("#invokeMethodBlank").click(function() {
267  invokeMethod($("#methodName").html(), true);
268  return false;
269});
270
271// resizable iframe
272$("#increaseIframe").click(function() {
273  $("#iframeWrapper").css('height', $("#iframeWrapper").height()+100);
274  adaptHeight();
275});
276$("#decreaseIframe").click(function() {
277  if ($("#iframeWrapper").height() > 200) {
278    $("#iframeWrapper").css('height', $("#iframeWrapper").height()-100);
279    adaptHeight();
280  }
281});
282
283// mask all wrappers
284function resetDisplay() {
285  $("#errorWrapper").hide();
286  $("#methodWrapper").hide();
287  $("#methodName").hide();
288  $("#urlForm").hide();
289  $("#methodDescription").hide();
290  $("#invokeFrame").attr('src','');
291}
292
293// give the same size to methods list and main page
294function adaptHeight()
295{
296  $("#the_page").css('height', 'auto');
297  $("#the_methods").css('height', 'auto');
298 
299  min_h = $(window).height()-$("#the_header").outerHeight()-$("#the_footer").outerHeight()-3;
300  h = Math.max(min_h, Math.max($("#the_methods").height(), $("#the_page").height()));
301 
302  $("#the_page").css('height', h);
303  $("#the_methods").css('height', h);
304}
305
306// display error wrapper
307function displayError(error) {
308  resetDisplay();
309  $("#errorWrapper").html("<b>Error:</b> "+ error).show();
310  adaptHeight();
311}
312
313// display ws_url form
314function askForUrl() {
315  displayError("can't contact web-services, please give absolute url to 'ws.php'");
316  if ($("#urlForm input[name='ws_url']").val() == "") {
317    $("#urlForm input[name='ws_url']").val(ws_url);
318  }
319  $("#urlForm").show();
320}
321
322// parse Piwigo JSON
323function parsePwgJSON(json) {
324  try {
325    resp = jQuery.parseJSON(json);
326    if (resp==null | resp.result==null | resp.stat==null | resp.stat!='ok') {
327      throw new Error();
328    }
329  }
330  catch(e) {
331    displayError("unable to parse JSON string");
332    resp = {"stat": "ko", "result": "null"};
333  }
334 
335  return resp.result;
336}
337
338// fetch methods list
339function getMethodList() {
340  resetDisplay();
341 
342  $.ajax({
343    type: "GET",
344    url: ws_url,
345    data: { format: "json", method: "reflection.getMethodList" }
346  }).done(function(result) {
347    result = parsePwgJSON(result);
348   
349    if (result!=null) {
350      methods = result.methods;
351     
352      var ml = '';
353      for (var i=0; i<methods.length; i++)
354      {
355        ml += '<li><a href="#">'+ methods[i]+'</a></li>';
356      }
357      $("#methodsList").html(ml).show();
358     
359      adaptHeight();
360     
361      // trigger method selection
362      $("#methodsList li a").click(function() {
363        selectMethod($(this).html());
364        return false;
365      });
366    }
367  }).error(function(jqXHR, textStatus, errorThrown) {
368    askForUrl();
369  });
370}
371
372// select method
373function selectMethod(methodName) {
374  $("#introMessage").hide();
375  $("#tiptip_holder").fadeOut(200);
376 
377  if (cachedMethods[ methodName ]) {
378    fillNewMethod(methodName);
379  }
380  else {
381    $.ajax({
382      type: "GET",
383      url: ws_url,
384      data: { format: "json", method: "reflection.getMethodDetails", methodName: methodName }
385    }).done(function(result) { 
386      result = parsePwgJSON(result);
387   
388      if (result!=null) {
389        cachedMethods[ methodName ] = result;
390        fillNewMethod(methodName);
391      }
392    }).error(function(jqXHR, textStatus, errorThrown) {
393      displayError("unknown error");
394    });
395  }
396}
397
398// display method details
399function fillNewMethod(methodName) {
400  resetDisplay();
401 
402  method = cachedMethods[ methodName ];
403 
404  $("#methodName").html(method.name).show();
405 
406  if (method.description != "") {
407    $("#methodDescription blockquote").html(method.description);
408    $("#methodDescription").show();
409  }
410 
411  var methodParams = '';
412  if (method.params && method.params.length>0) {
413    for (var i=0; i<method.params.length; i++) {
414      var param = method.params[i],
415          isOptional = param.optional,
416          acceptArray = param.acceptArray,
417          defaultValue = param.defaultValue == null ? '' : param.defaultValue,
418          info = param.info == null ? '' : '<a class="methodInfo" title="'+ param.info.replace(/"/g, '&quot;') + '">i</a>',
419          type = '';
420     
421      if (param.type.match(/bool/)) type+= '<span class=type>B</span>';
422      if (param.type.match(/int/)) type+= '<span class=type>I</span>';
423      if (param.type.match(/float/)) type+= '<span class=type>F</span>';
424      if (param.type.match(/positive/)) type+= '<span class=subtype>+</span>';
425      if (param.type.match(/notnull/)) type+= '<span class=subtype>&oslash;</span>';
426     
427      // if an array is direclty printed, the delimiter is a comma where we use a pipe
428      if (typeof defaultValue == 'object') {
429        defaultValue = defaultValue.join('|');
430      }
431
432      methodParams+= '<tr>'+
433        '<td>'+ param.name + info +'</td>'+
434        '<td class="mini">'+ (isOptional ? '?':'*') + (acceptArray ? ' []':'') +'</td>'+
435        '<td class="mini">'+ type +'</td>'+
436        '<td class="input"><input type="text" class="methodParameterValue" data-id="'+ i +'" value="'+ defaultValue +'"></td>'+
437        '<td class="mini"><input type="checkbox" class="methodParameterSend" data-id="'+ i +'" '+ (isOptional ? '':'checked="checked"') +'></td>'+
438      '</tr>';
439    }
440        }
441  else {
442    methodParams = '<tr><td colspan="4">This method takes no parameters</td></tr>';
443  }
444 
445  $("#methodParams tbody").html(methodParams);
446  $("#methodWrapper").show(); 
447 
448  adaptHeight();
449 
450  // trigger field modification
451  $("input.methodParameterValue").change(function() {
452    $("input.methodParameterSend[data-id='"+ $(this).data('id') +"']").attr('checked', 'checked');
453  });
454 
455  // tiptip
456  $(".methodInfo").tipTip({
457    maxWidth:"300px",
458    defaultPosition:"right",
459    delay:0
460  });
461}
462
463// invoke method
464function invokeMethod(methodName, newWindow) {
465        var method = cachedMethods[ methodName ];
466
467  var reqUrl = ws_url +"?format="+ $("#responseFormat").val();
468 
469  // GET
470  if ($("#requestFormat").val() == 'get') {
471    reqUrl+= "&method="+ methodName;
472   
473    for (var i=0; i<method.params.length; i++) {
474      if (! $("input.methodParameterSend[data-id='"+ i +"']").is(":checked")) {
475        continue;
476      }
477
478      var paramValue = $("input.methodParameterValue[data-id='"+ i +"']").val();
479     
480      var paramSplitted = paramValue.split('|');
481      if (method.params[i].acceptArray &&  paramSplitted.length > 1) {
482        $.each(paramSplitted, function(v) {
483          reqUrl+= '&'+ method.params[i].name +'[]='+ paramSplitted[v];
484        });
485      }
486      else {
487        reqUrl+= '&'+ method.params[i].name +'='+ paramValue;
488      }
489    }
490   
491    if (newWindow) {
492      window.open(reqUrl);
493    }
494    else {
495      $("#invokeFrame").attr('src', reqUrl);
496    }
497  }
498  // POST
499  else {
500    var form = $("#invokeForm");
501    form.attr('action', reqUrl);
502   
503    var t = '<input type="hidden" name="method" value="'+ methodName +'">';
504   
505    for (var i=0; i<method.params.length; i++) {
506      if (! $("input.methodParameterSend[data-id='"+ i +"']").is(":checked")) {
507        continue;
508      }
509
510      var paramValue = $("input.methodParameterValue[data-id='"+ i +"']").val();
511
512      var paramSplitted = paramValue.split('|');
513      if (method.params[i].acceptArray &&  paramSplitted.length > 1) {
514        $.each(paramSplitted, function(v) {
515          t+= '<input type="hidden" name="'+ method.params[i].name +'[]" value="'+ paramSplitted[v] +'">';
516        });
517      }
518      else {
519        t+= '<input type="hidden" name="'+ method.params[i].name +'" value="'+ paramValue +'">';
520      }
521    }
522   
523    form.html(t);
524    form.attr('target', newWindow ? "_blank" : "invokeFrame");
525    form.submit();
526  }
527 
528  return false;
529}
530</script>
531
532</body>
533</html>
Note: See TracBrowser for help on using the repository browser.