APIExplorer - Split "Rest" in two ("Rest" vs "curl")
authorTim Otten <totten@civicrm.org>
Thu, 28 Jan 2021 00:16:33 +0000 (16:16 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 28 Jan 2021 00:20:21 +0000 (16:20 -0800)
We're serving a dual purpose of educating generally about the REST request,
and providing an example that is runnable.

However, we're not doing either well if the REST example is simultaneously
incorrect and an obscure block of text.

This splits it into two examples, one emphasizing general structure
and the other emphasizing precision/correctness.

templates/CRM/Admin/Page/APIExplorer.js
templates/CRM/Admin/Page/APIExplorer.tpl

index 45172a1b900fbcfc7d72fe7bdf62f0b8fdeb767d..6ef22537bf2f8f02fdd9918480e9c32e8781876e 100644 (file)
@@ -19,6 +19,7 @@
     chainTpl = _.template($('#api-chain-tpl').html()),
     docCodeTpl = _.template($('#doc-code-tpl').html()),
     joinTpl = _.template($('#join-tpl').html()),
+    restTpl = _.template($('#api-rest-tpl').html()),
 
     // The following apis do not use Api3SelectQuery so do not support advanced features like joins or OR
     NO_JOINS = ['Contact', 'Contribution', 'Pledge', 'Participant'],
    * Render the api request in various formats
    */
   function formatQuery() {
+    var http = {
+      url: CRM.config.resourceBase + "extern/rest.php",
+      method:  action.startsWith('get') ? 'GET' : 'POST',
+      query: {entity: entity, action: action, json: JSON.stringify(params), api_key: 'FIXME_USER_KEY', key: 'FIXME_SITE_KEY'}
+    };
     var i = 0, q = {
       smarty: "{crmAPI var='result' entity='" + entity + "' action='" + action + "'" + (params.sequential ? '' : ' sequential=0'),
       php: "$result = civicrm_api3('" + entity + "', '" + action + "'",
       cv: "cv api " + entity + '.' + action + ' ',
       drush: "drush cvapi " + entity + '.' + action + ' ',
       wpcli: "wp cv api " + entity + '.' + action + ' ',
-      rest: CRM.config.resourceBase + "extern/rest.php?entity=" + encodeURI(entity) + "&action=" + encodeURI(action) + "&api_key=FIXME_USER_KEY&key=FIXME_SITE_KEY&json=" + encodeURI(JSON.stringify(params))
+      curl: http.method === 'GET' ?
+        "curl '" + http.url + "?" + $.param(http.query) + "'"
+        : "curl -X " + http.method + " -d '" + $.param(http.query) +"' \\\n  '" + http.url + "'"
     };
     smartyPhp = [];
     $.each(params, function(key, value) {
     } else if (smartyPhp.length) {
       q.smarty = "{php}\n  " + smartyPhp.join("\n  ") + "\n{/php}\n" + q.smarty;
     }
+    $('#api-rest').html(restTpl(http));
     $.each(q, function(type, val) {
       $('#api-' + type).text(val);
     });
index 2be5f1f3c4abce9ce90fbf526556149db4d08d75..f17588fafe4ea60f7d96e36413ba006ed1cca427 100644 (file)
       <div id="api-generated-wraper">
         <table id="api-generated" border=1>
           <caption>{ts}Code{/ts}</caption>
-          <tr><td>Rest</td><td><pre id="api-rest"></pre></td></tr>
+          <tr><td>Rest</td><td><div id="api-rest"></div></td></tr>
           <tr><td>Smarty</td><td><pre class="linenums" id="api-smarty" title='smarty syntax (for get actions)'></pre></td></tr>
           <tr><td>Php</td><td><pre class="linenums" id="api-php" title='php syntax'></pre></td></tr>
           <tr><td>Javascript</td><td><pre class="linenums" id="api-json" title='javascript syntax'></pre></td></tr>
           {if $config->userSystem->is_wordpress}
             <tr><td><a href="http://wp-cli.org/" target="_blank">wp-cli</a></td><td><pre id="api-wpcli" title='wp-cli syntax'></pre></td></tr>
           {/if}
+          <tr><td><a href="https://curl.se/">curl</a></td><td><pre id="api-curl"></pre></td></tr>
         </table>
       </div>
       <div class="crm-submit-buttons">
 </div>
 </div>
 {strip}
+<script type="text/template" id="api-rest-tpl">
+  <pre><%- method %> <%- url %></pre>
+  <ul>{literal}
+    <% _.forEach(query, function(value, field){ %>
+      <li><b><%- field %></b>: <pre style="display: inline-block"><%- value %></pre></li>
+    <% }); %>
+  {/literal}</ul>
+</script>
+
 <script type="text/template" id="api-param-tpl">
   <tr class="api-param-row">
     <td>