Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Cleanup api documentation

Roland Geider6f9a2c50eadc
diff --git a/wger/core/templates/rest_framework/api.html b/wger/core/templates/rest_framework/api.html
index 9bc9c5010..2912ce266 100644
--- a/wger/core/templates/rest_framework/api.html
+++ b/wger/core/templates/rest_framework/api.html
@@ -1,5 +1,5 @@
{% extends "base.html" %}
-{% load rest_framework wger_extras django_bootstrap_breadcrumbs static crispy_forms_tags %}
+{% load rest_framework wger_extras django_bootstrap_breadcrumbs static crispy_forms_tags i18n %}
{% block header %}
<link rel="stylesheet" type="text/css" href="{% static 'rest_framework/css/prettify.css' %}"/>
@@ -29,87 +29,25 @@
{% block content %}
-{# #}
-{# GET button #}
-{# #}
-{% if 'GET' in allowed_methods %}
- <form id="get-form" class="float-end">
- <fieldset>
- <div class="btn-group format-selection">
- <a class="btn btn-primary js-tooltip" href='{{ request.get_full_path }}' rel="nofollow"
- title="Make a GET request on the {{ name }} resource">GET</a>
-
- <button class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown"
- title="Specify a format for the GET request">
- <span class="caret"></span>
- </button>
- <ul class="dropdown-menu">
- {% for format in available_formats %}
- <li>
- <a class="js-tooltip format-option dropdown-item"
- href='{% add_query_param request api_settings.URL_FORMAT_OVERRIDE format %}'
- rel="nofollow"
- title="Make a GET request on the {{ name }} resource with the format set to `{{ format }}`">{{ format }}</a>
- </li>
- {% endfor %}
- </ul>
- </div>
-
- </fieldset>
- </form>
-{% endif %}
-
-{# #}
-{# OPTIONS button #}
-{# #}
-{% if options_form %}
- <form class="float-end" action="{{ request.get_full_path }}" method="POST" style="margin-right: 1em;">
- {% csrf_token %}
- <input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="OPTIONS"/>
- <button class="btn btn-primary js-tooltip" title="Make an OPTIONS request on the {{ name }} resource">OPTIONS
- </button>
- </form>
-{% endif %}
-
-{# #}
-{# DELETE button #}
-{# #}
-{% if delete_form %}
- <form class="float-end" action="{{ request.get_full_path }}" method="POST" style="margin-right: 1em;">
- {% csrf_token %}
- <input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="DELETE"/>
- <button class="btn btn-danger js-tooltip" title="Make a DELETE request on the {{ name }} resource">DELETE
- </button>
- </form>
-{% endif %}
-<div class="clearfix"></div>
-
-
{# #}
{# Object/endpoint description #}
{# #}
<div>
- <!--<h4>{{ name }}</h4>-->
-
- {% block description %}
- <p>{{ description }}</p>
- {% endblock %}
-
- <div class="request-info" style="clear: both">
+ <div class="request-info" style="clear: both" aria-label="{% trans "request info" %}">
<pre class="prettyprint"><b>{{ request.method }}</b> {{ request.get_full_path }}</pre>
</div>
- <div class="response-info">
- <pre class="prettyprint">
- <div
- class="meta nocode"><b>HTTP {{ response.status_code }} {{ response.status_text }}</b>{% autoescape off %}
- {% for key, val in response_headers.items %}
- <b>{{ key }}:</b>
- <span class="lit">{{ val|break_long_headers|urlize }}</span>
- {% endfor %}
- </div>{{ content|urlize }}
- </pre>
- {% endautoescape %}
+
+ <div class="response-info" aria-label="{% trans "response info" %}">
+ <pre class="prettyprint">
+ <span class="meta nocode"><b>HTTP {{ response.status_code }} {{ response.status_text }}</b>
+ {% for key, val in response_headers|items %}
+ <b>{{ key }}:</b> <span class="lit">{{ val|break_long_headers|urlize }}</span>
+ {% endfor %}
+
+ </span>{{ content|urlize }}</pre>
</div>
+
+
</div>
{% endblock %}
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index 3327234fd..52e3c6288 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -6,32 +6,34 @@
{% block title %}REST API{% endblock %}
{% block content %}
- <p>wger Workout Manager provides a full REST API to all database
- objects: <a href="/api/v2/" rel="nofollow">https://wger.de/api/v2/</a></p>
+ <p>
+ wger provides a full REST API to all database objects:
+ <a href="{% url 'api-root' %}" rel="nofollow">{% url 'api-root' %}</a>
+ </p>
<h3>Documentation</h3>
<p>The API is documented with openAPI:</p>
<ul>
- <li><a href="{% url 'schema' %}">Download schema file</a></li>
+ <li><a href="{% url 'api-root' %}">Browse the API</a></li>
<li><a href="{% url 'api-swagger-ui' %}">Swagger UI</a></li>
<li><a href="{% url 'api-redoc' %}">Redoc</a></li>
</ul>
<h3>Authentication</h3>
- <p>Public endpoints, such as the list of exercises or the
- ingredients can be accessed without authentication. For <strong>user owned
- objects such as workouts, you need to generate an API KEY</strong> and pass
- it in the header, see the link on the sidebar for details.</p>
+ <p>
+ Public endpoints, such as the list of exercises or the ingredients can be
+ accessed without authentication. For user owned objects such as
+ workouts, you need to authenticate.</p>
- <h6>JWT Authentication</h6>
+ <h5>JWT Authentication</h5>
<p>
- You can generate access token via <code>/token/</code> endpoint. Send a username and
- password, and you will get
- the
- <code>access</code> token which you can use to access the private endpoints.
+ This is the suggested way. Generate an access token from the <code>/token/</code>
+ endpoint. Send a username and password, and you will get the <code>access</code> token
+ which you can use to access the private endpoints.
+ </p>
<pre>
curl \
-X POST \
@@ -46,11 +48,14 @@ curl \
}
</pre>
- <p>Additionally, you can send an access token to <code>/token/verify/</code> endpoint to verify
- that token.</p>
+ <p>
+ Additionally, you can send an access token to <code>/token/verify/</code>
+ endpoint to verify that token.
+ </p>
- <p>When this short-lived access token expires, you can use the longer-lived <code>refresh</code>
- token to obtain another access token.
+ <p>
+ When this short-lived access token expires, you can use the longer-lived
+ <code>refresh</code> token to obtain another access token.
<pre>
curl \
-X POST \
@@ -61,15 +66,20 @@ curl \
...
{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNTY3LCJqdGkiOiJjNzE4ZTVkNjgzZWQ0NTQyYTU0NWJkM2VmMGI0ZGQ0ZSJ9.ekxRxgb9OKmHkfy-zs1Ro_xs1eMLXiR17dIDBVxeT-w"}
</pre>
- </p>
+
+ <h5>Token</h5>
+ <p>
+ You can also pass a permanent token in the header to authenticate, but this
+ method should be considered deprecated. If you want to generate a token
+ <a href="{% url 'core:user:api-key' %}">use this page</a>.
</p>
- <p>You should always use HTTPS if possible when communicating with the server.</p>
- <p>At the moment it is not possible to register via the API.</p>
- <p><strong>Deprecated: </strong>You can also generate a token via the <code>login</code>
- endpoint. Send a
- username and password, and you will get the user's token or a new one will be
- generated.</p>
+ <h6>Username and password</h6>
+ <p>
+ You can also get a token from the <code>login</code> endpoint.
+ Send a username and password in a POST request. IF the user doesn't
+ currently have a token, a new one will be generated for you
+ </p>
<div class="container">
@@ -84,7 +94,7 @@ curl \
<li>exercisecategory</li>
<li>exercisecomment</li>
<li>exerciseimage</li>
- <li>exerciseinfo</li>
+ <li>exercisebaseinfo</li>
<li>ingredient</li>
<li>ingredientinfo</li>
<li>ingredienttoweightunit</li>
@@ -122,9 +132,9 @@ curl \
<h3>Format negotiation</h3>
<p>
- At the moment only JSON and the browsable HTML view are supported, but other
- formats such as YAML or XML could be theoretically be added, should the
- need arise. Because of this, for the majority of REST clients it will not be
+ At the moment only JSON and the browsable HTML view are supported. That
+ means that you can use the same endpoints from your browser or a client.
+ Because of this, for the majority of REST clients it will not be
necessary to explicitly set the format, but you have the following options:
</p>
<ul>
@@ -157,33 +167,6 @@ curl \
</ul>
- <h3>Fetching Data</h3>
-
- <pre>
-# Api-Wide
-/api/v2/
-
-# Object detail view
-/api/v2/&lt;endpoint&gt;/&lt;id&gt;/
-</pre>
-
- <p>
- This lists out all the different resources available. If you visit the
- link with a browser, you'll get a human-browsable HTML version of the
- contents. If you are logged in, you can use the built in form to try
- different requests (POST, PATCH, etc.)
- </p>
-
- <p>
- You can do a <code>OPTIONS /api/v2/&lt;endpoint&gt;/</code> to get more
- information on the endpoint such as the accepted formats. It also outputs
- a listing with all the resource's fields, their type (date, int, etc.) and
- a short description. This is more easier done when using the browseable
- version.
- </p>
-
-
-
<h3>Miscellaneous operations</h3>
<h4>Ordering</h4>
<p>
@@ -194,7 +177,6 @@ curl \
</p>
-
<h4>Pagination</h4>
<p>
By default all results are paginated by 20 elements per page. If you want to
@@ -204,7 +186,6 @@ curl \
</p>
-
<h4>Filtering resources</h4>
<p>
You can easily filter all resources by specifying the filter queries in the
@@ -230,230 +211,7 @@ curl \
</li>
</ul>
- <h4>Submitting exercise images</h4>
- <p>
- If you want to submit exercise pictures, remember that you have to set the
- correct header <code>Content-Type: multipart/form-data</code>, otherwise the
- file upload won't work.
- </p>
-
- <h4>Exercises</h4>
- <p>
- Also note that, at the moment, to actually retrieve all the details for an exercise
- you will need to fire up different queries for the images, comments, etc.
- </p>
-
-
- <h3>Special endpoints</h3>
- <p>
- The following endpoints provide additional information, comfort functions, etc.:
- </p>
- <div style="margin-top: 1em;">
- <code>api/v2/workout/&lt;id&gt;/canonical_representation/</code>
- </div>
- <div class="row">
- <div class="offset-md-1 col-md-10">
- This is a complete representation of a workout, including all objects
- like workout days, exercises, etc. This form used by the django application
- to render workouts or perform other operations. The data is cached so,
- accessing or using is faster than performing many different requests.
- </div>
- </div>
-
-
- <div style="margin-top: 1em;">
- <code>api/v2/exerciseimage/&lt;id&gt;/thumbnails/</code>
- </div>
- <div class="row">
- <div class="offset-md-1 col-md-10">
- Returns a list of available thumbnails for this image. The 'settings' key
- refers to the settings used by the thumbnailing application to generate
- that image. The special key 'original' is simply a downloadable link to
- the original image used.
- </div>
- </div>
-
- <div style="margin-top: 1em;">
- <code>api/v2/exerciseiinfo/&lt;id&gt;/</code>
- </div>
- <div class="row">
- <div class="offset-md-1 col-md-10">
- A convenience function that returns all the information about an exercise,
- including, muscles, equipment, etc. This can be used to avoid having to call
- the other endpoints individually.
- </div>
- </div>
-
- <div style="margin-top: 1em;">
- <code>api/v2/nutritionplan/&lt;id&gt;/nutritional_values/</code><br>
- <code>api/v2/meal/&lt;id&gt;/nutritional_values/</code><br>
- <code>api/v2/mealitem/&lt;id&gt;/nutritional_values/</code>
- </div>
- <div class="row">
- <div class="offset-md-1 col-md-10">
- Returns the total nutritional values for a nutritional plan, a meal or
- an individual meal item. The nutritional plan lists this under the
- 'total' key, the other two return a flat list.
-
- The nutritional plan also returns under the 'per_kg' key the calculated
- values of grams per user body weight (if available) and 'percent' lists
- the percent each macronutrient contributes to the total calories.
- </div>
- </div>
-
- <div style="margin-top: 1em;">
- <code>api/v2/nutritionplan/&lt;id&gt;/get_log_overview/</code><br>
- </div>
- <div class="row">
- <div class="col-md-offset-1 col-md-10">
- Returns a list with the combined nutritional values logged for the given
- plan for all available days where a diary entry was logged.
- </div>
- </div>
-
- <div style="margin-top: 1em;">
- <code>api/v2/nutritionplan/&lt;id&gt;/log_summary/?year=&lt;year&gt;&month=&lt;month&gt;&day=&lt;day&gt;</code><br>
- </div>
- <div class="row">
- <div class="col-md-offset-1 col-md-10">
- Returns the nutritional values logged for the nutrition diary for a
- plan for the given date. You can omit the GET parameters, then you
- will get the values for today.
- </div>
- </div>
-
-
- <h3>Data documentation</h3>
- <p>The data structures should be pretty forward and easy to understand: a workout
- is composed of workout days. Each day has different exercises they in turn have
- repetitions, and so on. These diagrams are automatically generated from the
- database structure, which is basically exposed through the API 1-to-1. Click on
- the images to download a bigger version.</p>
-
-
- <div class="accordion mb-4" id="api-accordion">
- <div class="card">
- <div class="card-header">
- <h4 class="mb-0">
- <button class="btn btn-link btn-block text-left"
- type="button"
- data-bs-toggle="collapse"
- data-bs-target="#api-workout"
- aria-expanded="true"
- aria-controls="api-workout">
- Workouts
- </button>
- </h4>
- </div>
- <div id="api-workout" class="collapse" data-parent="#api-accordion">
- <div class="card-body">
- <a href="{% static 'images/api/db-manager.svg' %}">
- <img src="{% static 'images/api/db-manager.svg' %}" class="img-fluid">
- </a>
- </div>
- </div>
- </div>
-
- <div class="card">
- <div class="card-header">
- <h4 class="mb-0">
- <button class="btn btn-link btn-block text-left"
- type="button"
- data-bs-toggle="collapse"
- data-bs-target="#api-nutrition"
- aria-expanded="true"
- aria-controls="api-nutrition">
- Nutrition
- </button>
- </h4>
- </div>
- <div id="api-nutrition" class="collapse" data-parent="#api-accordion">
- <div class="card-body">
- <a href="{% static 'images/api/db-nutrition.svg' %}">
- <img src="{% static 'images/api/db-nutrition.svg' %}" class="img-fluid">
- </a>
- </div>
- </div>
- </div>
-
- <div class="card">
- <div class="card-header">
- <h4 class="mb-0">
- <button class="btn btn-link btn-block text-left"
- type="button"
- data-bs-toggle="collapse"
- data-bs-target="#api-exercises"
- aria-expanded="true"
- aria-controls="api-exercises">
- Exercises
- </button>
- </h4>
- </div>
- <div id="api-exercises" class="collapse" data-parent="#api-accordion">
- <div class="card-body">
- <a href="{% static 'images/api/db-exercises.svg' %}">
- <img src="{% static 'images/api/db-exercises.svg' %}" class="img-fluid">
- </a>
- </div>
- </div>
- </div>
-
- <div class="card">
- <div class="card-header">
- <h4 class="mb-0">
- <button class="btn btn-link btn-block text-left"
- type="button"
- data-bs-toggle="collapse"
- data-bs-target="#api-all"
- aria-expanded="true"
- aria-controls="api-all">
- Complete DB
- </button>
- </h4>
- </div>
- <div id="api-all" class="collapse" data-parent="#api-accordion">
- <div class="card-body">
- <a href="{% static 'images/api/db-all.svg' %}">
- <img src="{% static 'images/api/db-all.svg' %}" class="img-fluid">
- </a>
- </div>
- </div>
- </div>
- </div>
-
-
-
- <h3>Tools</h3>
- <p>
- You can play and experiment with the API by sending the requests with many
- tools, here are two:
- </p>
-
- <pre>
-# In the terminal, e.g. bash
-curl -H "Authorization: Token 123456..." \
- -H "Accept: application/json; indent=4" \
- -X PATCH --data '{"key": value, ...}' \
- --dump-header -
- https://wger.de/api/v2/....
-</pre>
-
- <pre>
-# In the python console
->>> import requests
->>> import json
->>> from pprint import pprint
->>>
->>> url = 'https://wger.de/api/v2/....'
->>> data = '{"key": "value"}'
->>> headers = {'Accept': 'application/json',
- 'Authorization': 'Token 12345...'}
->>> r = requests.patch(url=url, data=data, headers=headers)
->>> r
->>> r.content
->>> pprint(json.loads(r.content))
-</pre>
{% endblock %}
@@ -461,12 +219,12 @@ curl -H "Authorization: Token 123456..." \
{% block sidebar %}
<div class="alert alert-info" style="margin-top:1em;">
- This is also new for us, if you plan on using the API,
- <a href="https://github.com/wger-project/wger" class="alert-link">we'd love to hear from
- you</a>.
+ If you plan on using the API,
+ <a href="https://github.com/wger-project/wger" class="alert-link">
+ we'd love to hear from you
+ </a>.
</div>
- <p><a href="/api/v2/" class="btn btn-block btn-light" rel="nofollow">Browse the API</a></p>
- <p><a href="{% url 'core:user:api-key' %}" class="btn btn-block btn-light">Generate API KEY</a>
+ <p><a class="btn btn-light" href="{% url 'schema' %}">Download openAPI3 schema file</a></p>
</p>
{% endblock %}
diff --git a/wger/urls.py b/wger/urls.py
index 1ac332286..abd0f73cd 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -280,18 +280,25 @@ urlpatterns += [
core_api_views.RequiredApplicationVersionView.as_view({'get': 'get'}),
name='min_app_version'
),
- path('email/', include(email_urls)),
# Api documentation
path(
- 'api/schema/',
+ 'api/v2/schema',
SpectacularAPIView.as_view(),
name='schema',
),
path(
- 'api/schema/ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='api-swagger-ui'
+ 'api/v2/schema/ui',
+ SpectacularSwaggerView.as_view(url_name='schema'),
+ name='api-swagger-ui',
+ ),
+ path(
+ 'api/v2/schema/redoc',
+ SpectacularRedocView.as_view(url_name='schema'),
+ name='api-redoc',
),
- path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='api-redoc'),
+
+ path('email/', include(email_urls)),
]
#