{#
Displays information about accessing a resource via the API.

resource_id - The resource id
embedded - If true will not include the "modal" classes on the snippet.

Example

    {% snippet 'ajax_snippets/api_info.html', resource_id=resource_id, embedded=true %}

#}

{% set resource_id = h.sanitize_id(resource_id) %}
{% set sql_example_url = h.url_for('api.action', ver=3, logic_function='datastore_search_sql', qualified=True) + '?sql=SELECT * from "' + resource_id + '" WHERE title LIKE \'jones\'' %}
{# not urlencoding the sql because its clearer #}
<div {% if not embedded %} class="modal"{% endif %} tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
          <h3>
            {{ _('CKAN Data API') }}
          </h3>
        </div>
        <div {% if not embedded %} class="modal-body"{% endif %}>
          <p><strong>{{ _('Access resource data via a web API with powerful query support') }}</strong>.
          {% trans %}
          Further information in the <a
            href="http://docs.ckan.org/en/latest/maintaining/datastore.html" target="_blank">main
            CKAN Data API and DataStore documentation</a>.</p>
          {% endtrans %}
          <div class="accordion" id="accordion2">
            <div class="accordion-item">
              <h2 class="accordion-heading">
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-endpoints" aria-expanded="true" aria-controls="collapse-endpoints">{{ _('Endpoints') }} &raquo;</button>
              </h2>
              <div id="collapse-endpoints" class="accordion-collapse collapse show" aria-labelledby="collapse-endpoints" data-bs-parent="#accordion2">
                <div class="accordion-body">
                  <p>{{ _('The Data API can be accessed via the following actions of the CKAN action API.') }}</p>
                  <table class="table table-striped table-bordered">
                    <thead></thead>
                    <tbody>
                      <tr>
                        <th scope="row">{{ _('Create') }}</th>
                        <td><code>{{ h.url_for('api.action', ver=3, logic_function='datastore_create', qualified=True) }}</code></td>
                      </tr>
                      <tr>
                        <th scope="row">{{ _('Update / Insert') }}</th>
                        <td><code>{{ h.url_for('api.action', ver=3, logic_function='datastore_upsert', qualified=True) }}</code></td>
                      </tr>
                      <tr>
                        <th scope="row">{{ _('Query') }}</th>
                        <td><code>{{ h.url_for('api.action', ver=3, logic_function='datastore_search', qualified=True) }}</code></td>
                      </tr>
                      {% if h.datastore_search_sql_enabled() %}
                      <tr>
                        <th scope="row">{{ _('Query (via SQL)') }}</th>
                        <td><code>{{ h.url_for('api.action', ver=3, logic_function='datastore_search_sql', qualified=True) }}</code></td>
                      </tr>
                      {% endif %}
                    </tbody>
                  </table>
                </div>
              </div>
            </div>

            <div class="accordion-item">
              <h2 class="accordion-heading">
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-querying" aria-expanded="false" aria-controls="collapse-querying">{{ _('Querying') }} &raquo;</button>
              </h2>
              <div id="collapse-querying"  class="accordion-collapse collapse" aria-labelledby="collapse-querying" data-bs-parent="#accordion2">
                <div class="accordion-body">
                  <strong>{{ _('Query example (first 5 results)') }}</strong>
                  <p>
                  <code><a href="{{ h.url_for('api.action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5, qualified=True) }}" target="_blank" rel="noreferrer">{{ h.url_for('api.action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5, qualified=True) }}</a></code>
                  </p>

                  <strong>{{ _('Query example (results containing \'jones\')') }}</strong>
                  <p>
                  <code><a href="{{ h.url_for('api.action', ver=3, logic_function='datastore_search', resource_id=resource_id, q='jones', qualified=True) }}" target="_blank" rel="noreferrer">{{ h.url_for('api.action', ver=3, logic_function='datastore_search', resource_id=resource_id, q='jones', qualified=True) }}</a></code>
                  </p>
                  {% if h.datastore_search_sql_enabled() %}
                  <strong>{{ _('Query example (via SQL statement)') }}</strong>
                  <p>
                  <code><a href="{{sql_example_url}}" target="_blank" rel="noreferrer">{{ sql_example_url }}</a></code>
                  </p>
                  {% endif %}
                </div>
              </div>
            </div>

            <div class="accordion-item">
              <h2 class="accordion-heading">
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-javascript" aria-expanded="false" aria-controls="collapse-javascript"> {{ _('Example: Javascript') }} &raquo;</button>
              </h2>
              <div id="collapse-javascript" class="accordion-collapse collapse" aria-labelledby="collapse-javascript" data-bs-parent="#accordion2">
                <div class="accordion-body">
                  <p>{{ _('A simple ajax (JSONP) request to the data API using jQuery.') }}</p>
                  <pre>
          var data = {
            resource_id: '{{resource_id}}', // the resource id
            limit: 5, // get 5 results
            q: 'jones' // query for 'jones'
          };
          $.ajax({
            url: '{{ h.url_for('api.action', ver=3, logic_function='datastore_search', qualified=True) }}',
            data: data,
            dataType: 'jsonp',
            success: function(data) {
              alert('Total results found: ' + data.result.total)
            }
          });</pre>
                </div>
              </div>
            </div>

            <div class="accordion-item">
              <div class="accordion-heading">
                <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-python" aria-expanded="false" aria-controls="collapse-python"> {{ _('Example: Python') }} &raquo;</button>
              </div>
              <div id="collapse-python" class="accordion-collapse collapse" aria-labelledby="collapse-python" data-bs-parent="#accordion2">
                <div class="accordion-body">
                  <pre>
        import urllib.request
        url = '{{ h.url_for('api.action', qualified=True, ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5) + '&q=title:jones' }}'  {# not urlencoding the ":" because its clearer #}
        fileobj = urllib.request.urlopen(url)
        print(fileobj.read())
        </pre>
                </div>
              </div>
            </div>
        </div>

      </div>
    </div>
  </div>

</div>
