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

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

Example

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

#}

{% set sql_example_url = datastore_root_url + '/datastore_search_sql?sql=SELECT * from "' + resource_id + '" WHERE title LIKE \'jones\'' %}

<div{% if not embedded %} class="modal"{% endif %}>
  <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-group">
      <div class="accordion-heading">

        <a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-endpoints">{{ _('Endpoints') }} &raquo;</a>
      </div>
      <div id="collapse-endpoints" class="in accordion-body collapse">
        <div class="accordion-inner">
          <p>{{ _('The Data API can be accessed via the following actions of the CKAN action API.') }}</p>
          <table class="table-condensed table-striped table-bordered">
            <thead></thead>
            <tbody>
              <tr>
                <th scope="row">{{ _('Create') }}</th>
                <td><code>{{ datastore_root_url }}/datastore_create</code></td>
              </tr>
              <tr>
                <th scope="row">{{ _('Update / Insert') }}</th>
                <td><code>{{ datastore_root_url }}/datastore_upsert</code></td>
              </tr>
              <tr>
                <th scope="row">{{ _('Query') }}</th>
                <td><code>{{ datastore_root_url }}/datastore_search</code></td>
              </tr>
              <tr>
                <th scope="row">{{ _('Query (via SQL)') }}</th>
                <td><code>{{ datastore_root_url }}/datastore_search_sql</code></td>
              </tr>

            </tbody>
          </table>
        </div>
      </div>
    </div>

    <div class="accordion-group">
      <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-querying">{{ _('Querying') }} &raquo;</a>
      </div>
      <div id="collapse-querying" class="collapse accordion-body in">
        <div class="accordion-inner">
          <strong>{{ _('Query example (first 5 results)') }}</strong>
          <p>
          <code><a href="{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&limit=5" target="_blank">{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&limit=5</a></code>
          </p>

          <strong>{{ _('Query example (results containing \'jones\')') }}</strong>
          <p>
          <code><a href="{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&q=jones"
              target="_blank">{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&q=jones</a></code>
          </p>

          <strong>{{ _('Query example (via SQL statement)') }}</strong>
          <p>
          <code><a href="{{sql_example_url}}"
              target="_blank">{{sql_example_url}}</a></code>
          </p>

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

    <div class="accordion-group">
      <div class="accordion-heading">
       <a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-javascript">{{ _('Example: Javascript') }} &raquo;</a>
      </div>
      <div id="collapse-javascript" class="accordion-body collapse">
        <div class="accordion-inner">
          <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: '{{ datastore_root_url }}/datastore_search',
    data: data,
    dataType: 'jsonp',
    success: function(data) {
      alert('Total results found: ' + data.result.total)
    }
  });</pre>
        </div>
      </div>
    </div>

    <div class="accordion-group">
      <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-python">{{ _('Example: Python') }} &raquo;</a>
      </div>
      <div id="collapse-python" class="accordion-body collapse">
        <div class="accordion-inner">
          <pre>
import urllib
url = '{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&amp;limit=5&amp;q=title:jones'
fileobj = urllib.urlopen(url)
print fileobj.read()
</pre>
        </div>
      </div>
    </div>
  </div>

</div>

</div>
