source: trunk/tools/ws.htm @ 25394

Last change on this file since 25394 was 25394, checked in by mistic100, 10 years ago

Improve display on ws.htm

  • Property svn:eol-style set to LF
File size: 18.3 KB
Line 
1<!DOCTYPE html>
2<html lang="en" dir="ltr">
3<head>
4  <meta charset="utf-8">
5  <title>Piwigo web API (web-services) explorer</title>
6 
7  <link rel="stylesheet" href="http://cdn.jsdelivr.net/tiptip/1.3/tipTip.css">
8 
9  <style>
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 {margin-right:230px;}
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 {float:right;}
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 
97  div.onlys {background:#faa;color:#fff;border:1px solid #f22;padding:.25em .5em;display:table;border-radius:4px;margin-bottom:0.5em;}
98  </style>
99 
100</head>
101
102<body>
103<a name="top"></a>
104
105<div id="the_header">
106  <h1>Piwigo web API (web-services) explorer</h1>
107</div> <!-- the_header -->
108
109<div id="the_methods">
110  <h2>Available methods</h2>
111 
112  <ul id="methodsList">
113  </ul>
114</div> <!-- the_methods -->
115
116<div id="the_page">
117  <h2 id="methodName" style="display:none;"></h2>
118  <h2 id="errorWrapper" style="display:none;"></h2>
119 
120  <div id="the_content">
121    <form id="urlForm" style="display:none;">
122      <input type="text" name="ws_url" size="60">
123      <input type="submit" value="Go!">
124    </form>
125   
126    <blockquote id="introMessage">
127      <p>
128        <b>API = Application Programming Interface.</b><br>
129        This is the way other applications can communicate with Piwigo. This feature is also know as Web Services.
130      </p>
131
132      <p>Examples:</p>
133      <ul>
134        <li>Wordpress (web blog software) can display random photos from a Piwigo gallery in its sidebar</li>
135        <li>Lightroom (photo management software for desktop) can create albums and upload photos to Piwigo</li>
136      </ul>
137
138      <p>
139        This page lists all API methods available on your Piwigo installation, part of the Piwigo core or added by third-party plugins.
140        For each method you can consult required and optional parameters, and even test them in direct live!
141      </p>
142
143      <p>
144        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>.
145      </p>
146    </blockquote> <!-- introMessage -->
147
148
149    <form id="methodWrapper" style="display:none;">
150      <div id="methodDescription" style="display:none;">
151        <h3>Description</h3>
152        <blockquote>
153        </blockquote>
154        <br>
155      </div> <!-- methodDescription -->
156     
157      <div id="testForm">
158        <h3>Test</h3>
159        <blockquote>
160          <table>
161            <tr>
162              <td>Request format :</td>
163              <td>
164                <select id="requestFormat">
165                  <option value="get" selected>GET</option>
166                  <option value="post">POST</option>
167                </select>
168              </td>
169            </tr>
170            <tr>
171              <td>Response format :</td>
172              <td>
173                <select id="responseFormat">
174                  <option value="rest" selected>REST (xml)</option>
175                  <option value="json">JSON</option>
176                  <option value="php">PHP serial</option>
177                  <option value="xmlrpc">XML RPC</option>
178                </select>
179              </td>
180            </tr>
181            <tr>
182              <td colspan="2">
183                <a href="#" class="button" id="invokeMethod">INVOKE</a>
184                <a href="#" class="button" id="invokeMethodBlank">INVOKE (new window)</a>
185              </td>
186            </tr>
187          </table>
188        </blockquote>
189      </div> <!-- testForm -->
190     
191      <div id="methodParams">
192        <h3>Method parameters</h3>
193        <table>
194          <thead>
195            <tr>
196              <td style="width:150px;">Name</td>
197              <td class="mini">Extra</td>
198              <td class="mini">Type</td>
199              <td style="width:300px;">Value</td>
200              <td class="mini">Send</td>
201            </tr>
202          </thead>
203         
204          <tbody>
205          </tbody>
206         
207          <tfoot>
208            <tr>
209              <td colspan="5">
210                <b>*</b>: required, <b>?</b>: optional, <b>[]</b>: can be an array (use a pipe | to split values)<br>
211                <b>B</b>: boolean, <b>I</b>: integer, <b>F</b>: float, <b>+</b>: positive, <b>&oslash;</b>: not null
212              </td>
213            </tr>
214          </tfoot>
215        </table>
216      </div> <!-- methodParams -->
217     
218      <br><br>
219     
220      <h3>Result</h3>
221      <div id="iframeWrapper">
222        <iframe src="" id="invokeFrame" name="invokeFrame"></iframe>
223        <a href="#iframe-bottom" id="increaseIframe"><b>&darr;</b> increase height</a> &#8226;
224        <a href="#iframe-bottom" id="decreaseIframe"><b>&uarr;</b> decrease height</a>
225        <a name="iframe-bottom"></a>
226      </div>
227    </form> <!-- iframeWrapper -->
228   
229    <!-- hidden form for POST submition -->
230    <form method="post" action="" target="" id="invokeForm" style="display:none;"></form>
231   
232  </div> <!-- the_content -->
233
234</div> <!-- the_page -->
235
236<div id="the_footer">
237  Copyright &copy; 2002-2013 <a href="http://piwigo.org">Piwigo Team</a>
238</div> <!-- the_footer -->
239
240<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
241<script src="https://cdn.jsdelivr.net/tiptip/1.3/jquery.tipTip.minified.js"></script>
242
243<script> 
244// global vars
245var cachedMethods = new Array;
246var ws_url = "http://";
247
248// automatic detection of ws_url
249match = document.location.toString().match(/^(https?.*\/)tools\/ws\.html?/);
250if (match==null) {
251  askForUrl();
252}
253else {
254  ws_url = match[1]+'ws.php';
255  getMethodList();
256}
257
258// manual set of ws_url
259$("#urlForm").submit(function() {
260  ws_url = $(this).children("input[name='ws_url']").val();
261  getMethodList();
262  return false;
263});
264
265// invoke buttons
266$("#invokeMethod").click(function() {
267  invokeMethod($("#methodName").html(), false);
268  return false;
269});
270$("#invokeMethodBlank").click(function() {
271  invokeMethod($("#methodName").html(), true);
272  return false;
273});
274
275// resizable iframe
276$("#increaseIframe").click(function() {
277  $("#iframeWrapper").css('height', $("#iframeWrapper").height()+100);
278  adaptHeight();
279});
280$("#decreaseIframe").click(function() {
281  if ($("#iframeWrapper").height() > 200) {
282    $("#iframeWrapper").css('height', $("#iframeWrapper").height()-100);
283    adaptHeight();
284  }
285});
286
287// mask all wrappers
288function resetDisplay() {
289  $("#errorWrapper").hide();
290  $("#methodWrapper").hide();
291  $("#methodName").hide();
292  $("#urlForm").hide();
293  $("#methodDescription blockquote").empty();
294  $("#methodDescription").hide();
295  $("#invokeFrame").attr('src','');
296}
297
298// give the same size to methods list and main page
299function adaptHeight()
300{
301  $("#the_page").css('height', 'auto');
302  $("#the_methods").css('height', 'auto');
303 
304  min_h = $(window).height()-$("#the_header").outerHeight()-$("#the_footer").outerHeight()-3;
305  h = Math.max(min_h, Math.max($("#the_methods").height(), $("#the_page").height()));
306 
307  $("#the_page").css('height', h);
308  $("#the_methods").css('height', h);
309}
310
311// display error wrapper
312function displayError(error) {
313  resetDisplay();
314  $("#errorWrapper").html("<b>Error:</b> "+ error).show();
315  adaptHeight();
316}
317
318// display ws_url form
319function askForUrl() {
320  displayError("can't contact web-services, please give absolute url to 'ws.php'");
321  if ($("#urlForm input[name='ws_url']").val() == "") {
322    $("#urlForm input[name='ws_url']").val(ws_url);
323  }
324  $("#urlForm").show();
325}
326
327// parse Piwigo JSON
328function parsePwgJSON(json) {
329  try {
330    resp = jQuery.parseJSON(json);
331    if (resp==null | resp.result==null | resp.stat==null | resp.stat!='ok') {
332      throw new Error();
333    }
334  }
335  catch(e) {
336    displayError("unable to parse JSON string");
337    resp = {"stat": "ko", "result": "null"};
338  }
339 
340  return resp.result;
341}
342
343// fetch methods list
344function getMethodList() {
345  resetDisplay();
346 
347  $.ajax({
348    type: "GET",
349    url: ws_url,
350    data: { format: "json", method: "reflection.getMethodList" }
351  }).done(function(result) {
352    result = parsePwgJSON(result);
353   
354    if (result!=null) {
355      methods = result.methods;
356     
357      var ml = '';
358      for (var i=0; i<methods.length; i++)
359      {
360        ml += '<li><a href="#top">'+ methods[i]+'</a></li>';
361      }
362      $("#methodsList").html(ml).show();
363     
364      adaptHeight();
365     
366      // trigger method selection
367      $("#methodsList li a").click(function() {
368        selectMethod($(this).html());
369      });
370    }
371  }).error(function(jqXHR, textStatus, errorThrown) {
372    askForUrl();
373  });
374}
375
376// select method
377function selectMethod(methodName) {
378  $("#introMessage").hide();
379  $("#tiptip_holder").fadeOut(200);
380 
381  if (cachedMethods[ methodName ]) {
382    fillNewMethod(methodName);
383  }
384  else {
385    $.ajax({
386      type: "GET",
387      url: ws_url,
388      data: { format: "json", method: "reflection.getMethodDetails", methodName: methodName }
389    }).done(function(result) { 
390      result = parsePwgJSON(result);
391   
392      if (result!=null) {
393        cachedMethods[ methodName ] = result;
394        fillNewMethod(methodName);
395      }
396    }).error(function(jqXHR, textStatus, errorThrown) {
397      displayError("unknown error");
398    });
399  }
400}
401
402// display method details
403function fillNewMethod(methodName) {
404  resetDisplay();
405 
406  method = cachedMethods[ methodName ];
407 
408  $("#methodName").html(method.name).show();
409 
410  if (method.options.post_only || method.options.admin_only) {
411    var onlys = '<div class="onlys">';
412    if (method.options.post_only) {
413      onlys+= 'POST only. ';
414    }
415    if (method.options.admin_only) {
416      onlys+= 'Admin only. ';
417    }
418    onlys+= '</div>';
419   
420    method.description = onlys + method.description;
421  }
422 
423  if (method.description != "") {
424    $("#methodDescription blockquote").html(method.description);
425    $("#methodDescription").show();
426  }
427 
428  $("#requestFormat").val(method.options.post_only ? 'post' : 'get');
429 
430  var methodParams = '';
431  if (method.params && method.params.length>0) {
432    for (var i=0; i<method.params.length; i++) {
433      var param = method.params[i],
434          isOptional = param.optional,
435          acceptArray = param.acceptArray,
436          defaultValue = param.defaultValue == null ? '' : param.defaultValue,
437          info = param.info == null ? '' : '<a class="methodInfo" title="'+ param.info.replace(/"/g, '&quot;') + '">i</a>',
438          type = '';
439     
440      if (param.type.match(/bool/)) type+= '<span class=type>B</span>';
441      if (param.type.match(/int/)) type+= '<span class=type>I</span>';
442      if (param.type.match(/float/)) type+= '<span class=type>F</span>';
443      if (param.type.match(/positive/)) type+= '<span class=subtype>+</span>';
444      if (param.type.match(/notnull/)) type+= '<span class=subtype>&oslash;</span>';
445     
446      // if an array is direclty printed, the delimiter is a comma where we use a pipe
447      if (typeof defaultValue == 'object') {
448        defaultValue = defaultValue.join('|');
449      }
450
451      methodParams+= '<tr>'+
452        '<td>'+ param.name + info +'</td>'+
453        '<td class="mini">'+ (isOptional ? '?':'*') + (acceptArray ? ' []':'') +'</td>'+
454        '<td class="mini">'+ type +'</td>'+
455        '<td class="input"><input type="text" class="methodParameterValue" data-id="'+ i +'" value="'+ defaultValue +'"></td>'+
456        '<td class="mini"><input type="checkbox" class="methodParameterSend" data-id="'+ i +'" '+ (isOptional ? '':'checked="checked"') +'></td>'+
457      '</tr>';
458    }
459        }
460  else {
461    methodParams = '<tr><td colspan="4">This method takes no parameters</td></tr>';
462  }
463 
464  $("#methodParams tbody").html(methodParams);
465  $("#methodWrapper").show(); 
466 
467  adaptHeight();
468 
469  // trigger field modification
470  $("input.methodParameterValue").change(function() {
471    $("input.methodParameterSend[data-id='"+ $(this).data('id') +"']").attr('checked', 'checked');
472  });
473 
474  // tiptip
475  $(".methodInfo").tipTip({
476    maxWidth:"300px",
477    defaultPosition:"right",
478    delay:0
479  });
480}
481
482// invoke method
483function invokeMethod(methodName, newWindow) {
484        var method = cachedMethods[ methodName ];
485
486  var reqUrl = ws_url +"?format="+ $("#responseFormat").val();
487 
488  // GET
489  if ($("#requestFormat").val() == 'get') {
490    reqUrl+= "&method="+ methodName;
491   
492    for (var i=0; i<method.params.length; i++) {
493      if (! $("input.methodParameterSend[data-id='"+ i +"']").is(":checked")) {
494        continue;
495      }
496
497      var paramValue = $("input.methodParameterValue[data-id='"+ i +"']").val();
498     
499      var paramSplitted = paramValue.split('|');
500      if (method.params[i].acceptArray &&  paramSplitted.length > 1) {
501        $.each(paramSplitted, function(v) {
502          reqUrl+= '&'+ method.params[i].name +'[]='+ paramSplitted[v];
503        });
504      }
505      else {
506        reqUrl+= '&'+ method.params[i].name +'='+ paramValue;
507      }
508    }
509   
510    if (newWindow) {
511      window.open(reqUrl);
512    }
513    else {
514      $("#invokeFrame").attr('src', reqUrl);
515    }
516  }
517  // POST
518  else {
519    var form = $("#invokeForm");
520    form.attr('action', reqUrl);
521   
522    var t = '<input type="hidden" name="method" value="'+ methodName +'">';
523   
524    for (var i=0; i<method.params.length; i++) {
525      if (! $("input.methodParameterSend[data-id='"+ i +"']").is(":checked")) {
526        continue;
527      }
528
529      var paramValue = $("input.methodParameterValue[data-id='"+ i +"']").val();
530
531      var paramSplitted = paramValue.split('|');
532      if (method.params[i].acceptArray &&  paramSplitted.length > 1) {
533        $.each(paramSplitted, function(v) {
534          t+= '<input type="hidden" name="'+ method.params[i].name +'[]" value="'+ paramSplitted[v] +'">';
535        });
536      }
537      else {
538        t+= '<input type="hidden" name="'+ method.params[i].name +'" value="'+ paramValue +'">';
539      }
540    }
541   
542    form.html(t);
543    form.attr('target', newWindow ? "_blank" : "invokeFrame");
544    form.submit();
545  }
546 
547  return false;
548}
549</script>
550
551</body>
552</html>
Note: See TracBrowser for help on using the repository browser.