{% extends "layout.html" %}

{% comment %}
Copyright (C) 2015-2019  The Software Heritage developers
See the AUTHORS file at the top-level directory of this distribution
License: GNU Affero General Public License version 3, or any later version
See top-level LICENSE file for more information
{% endcomment %}

{% load swh_templatetags %}

{% block title %}{{ heading }} &ndash; Software Heritage API {% endblock %}

{% block navbar-content %}
<nav class="bread-crumbs">
  <ul>
    <li><a href="/api/"><h4>Web API</h4></a></li>
    <li class="bc-no-root"><i class="fa fa-caret-right fa-fw" aria-hidden="true"></i></li>
    <li class="bc-no-root"><a href="/api/1/">endpoints</a></li>
    {% for endpoint in endpoint_path %}
      <li class="bc-no-root"><i class="fa fa-caret-right fa-fw" aria-hidden="true"></i></li>
      {% if endpoint.name != 'stat' and endpoint.name != 'vault' and endpoint.path != 'vault/revision' %}
        <li class="bc-no-root"><a href="{{ '/api/1/'|add:endpoint.path }}">{{ endpoint.name }}</a></li>
      {% else %}
        <li class="bc-no-root"><span>{{ endpoint.name }}</span></li>
      {% endif %}
    {% endfor %}
  </ul>
</nav>
{% endblock %}

{% block content %}

<div class='swh-apidoc'>
{% if description %}
  <div>
    <h4> Description </h4>
    {{ description | safe_docstring_display | safe }}
  </div>
{% endif %}
{% if response_data is not None %}
  <div>
    <h4>Request</h4>
    <pre><strong>{{ request.method }}</strong> {{ request.build_absolute_uri }}</pre>
    <hr/>
    <h4>Response</h4>
    {% if status_code != 200 %}
      <h5>Status Code</h5>
      <pre>{{ status_code }}</pre>
    {% endif %}
    {% if headers_data %}
      <h5>Headers</h5>
      {% for header_name, header_value in headers_data.items %}
        <pre><strong>{{ header_name }}</strong> {{ header_value | urlize_header_links | safe }}</pre>
      {% endfor %}
    {% endif %}
    <h5>Body</h5>
    <pre><code class="json">{{ response_data | urlize_links_and_mails | safe }}</code></pre>
  </div>
{% endif %}
<hr/>
{% if urls and urls|length > 0 %}
  <div>
    <table class="m-x-auto table">
      <thead>
        <tr>
          <th>URL</th>
          <th>Allowed Methods</th>
        </tr>
      </thead>
      <tbody>
        {% for url in urls %}
          <tr>
            <td>{{ url.rule | safe_docstring_display | safe }}</td>
            <td>{{ url.methods | dictsort:0 | join:', ' }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  <hr/>
{% endif %}
{% if args and args|length > 0 %}
  <div>
    <h4> Arguments </h4>
    {% for arg in args %}
      <dl class="row">
        <dt class="col col-md-2 text-right"> {{ arg.name }} ({{ arg.type }}) </dt>
        <dd class="col col-md-9"> {{ arg.doc | safe_docstring_display | safe }} </dd>
      </dl>
    {% endfor %}
  </div>
  <hr/>
{% endif %}
{% if params and params|length > 0 %}
  <div>
    <h4> Query parameters </h4>
    {% for param in params %}
      <dl class="row">
        <dt class="col col-md-2 text-right"> {{ param.name }} ({{ param.type }}) </dt>
        <dd class="col col-md-9"> {{ param.doc | safe_docstring_display | safe }} </dd>
      </dl>
    {% endfor %}
  </div>
  <hr/>
{% endif %}
{% if reqheaders and reqheaders|length > 0 %}
  <div>
    <h4> Request headers </h4>
    {% for header in reqheaders %}
      <dl class="row">
        <dt class="col col-md-2 text-right"> {{ header.name }} </dt>
        <dd class="col col-md-9"> {{ header.doc | safe_docstring_display | safe }} </dd>
      </dl>
    {% endfor %}
  </div>
<hr/>
{% endif %}
{% if resheaders and resheaders|length > 0 %}
  <div>
    <h4> Response headers </h4>
    {% for header in resheaders %}
      <dl class="row">
        <dt class="col col-md-2 text-right"> {{ header.name }} </dt>
        <dd class="col col-md-9"> {{ header.doc | safe_docstring_display | safe }} </dd>
      </dl>
    {% endfor %}
  </div>
<hr/>
{% endif %}
{% if return_type %}
  <div>
    <h4> Returns </h4>
    <dl class="row">
      <dt class="col col-md-2 text-right"> {{ return_type }} </dt>
      <dd class="col col-md-9">
        <p>
          {% if return_type == 'array' %}
            an array of objects containing the following keys:
          {% elif return_type == 'octet stream' %}
            the raw data as an octet stream
          {% else %}
            an object containing the following keys:
          {% endif %}
          {{ returns_list | safe_docstring_display | safe }}
        </p>
      </dd>
    </dl>
  </div>
  <hr/>
{% endif %}
{% if status_codes and status_codes|length > 0 %}
  <div>
    <h4> HTTP status codes </h4>
    {% for status in status_codes %}
      <dl class="row">
        <dt class="col col-md-2 text-right"> {{ status.code }} </dt>
        <dd class="col col-md-9"> {{ status.doc | safe_docstring_display | safe }} </dd>
      </dl>
    {% endfor %}
  </div>
  <hr/>
{% endif %}
{% if examples and examples|length > 0 %}
  <div>
    <h4> Examples </h4>
    {% for example in examples %}
      <dl class="row">
        <dt class="col col-md-2"></dt>
        <dd class="col col-md-9">
          <a href="{{ example }}">{{ example }}</a>
        </dd>
      </dl>
    {% endfor %}
  </div>
{% endif %}
</div>

<script>
  swh.webapp.initPage('api');
  swh.webapp.highlightCode(false);
</script>
{% endblock %}
