mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 18:08:03 +03:00 
			
		
		
		
	Allowed customising API documentation code samples (#5752)
* Allowed additional languages in API documentation * Documented renderer_classes parameter and customising languages.
This commit is contained in:
		
							parent
							
								
									588b61e171
								
							
						
					
					
						commit
						a540acdc95
					
				| 
						 | 
					@ -107,6 +107,7 @@ The `rest_framework.documentation` module provides three helper functions to hel
 | 
				
			||||||
* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
 | 
					* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
 | 
				
			||||||
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
					* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
				
			||||||
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
 | 
					* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
 | 
				
			||||||
 | 
					* `renderer_classes`: Default `None`. May be used to pass custom renderer classes to the `SchemaView`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### `get_docs_view`
 | 
					#### `get_docs_view`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -117,7 +118,8 @@ The `rest_framework.documentation` module provides three helper functions to hel
 | 
				
			||||||
* `patterns`: Default `None`. A list of URLs to inspect when generating the schema. If `None` project's URL conf will be used.
 | 
					* `patterns`: Default `None`. A list of URLs to inspect when generating the schema. If `None` project's URL conf will be used.
 | 
				
			||||||
* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
 | 
					* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
 | 
				
			||||||
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
					* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
				
			||||||
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
 | 
					* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES`. May be used to pass custom permission classes to the `SchemaView`.
 | 
				
			||||||
 | 
					* `renderer_classes`: Default `None`. May be used to pass custom renderer classes to the `SchemaView`. If `None` the `SchemaView` will be configured with `DocumentationRenderer` and `CoreJSONRenderer` renderers, corresponding to the (default) `html` and `corejson` formats.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### `get_schemajs_view`
 | 
					#### `get_schemajs_view`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -130,6 +132,25 @@ The `rest_framework.documentation` module provides three helper functions to hel
 | 
				
			||||||
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
					* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
 | 
				
			||||||
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
 | 
					* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Customising code samples
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The built-in API documentation includes automatically generated code samples for
 | 
				
			||||||
 | 
					each of the available API client libraries.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					You may customise these samples by subclassing `DocumentationRenderer`, setting
 | 
				
			||||||
 | 
					`languages` to the list of languages you wish to support:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    from rest_framework.renderers import DocumentationRenderer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class CustomRenderer(DocumentationRenderer):
 | 
				
			||||||
 | 
					        languages = ['ruby', 'go']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					For each language you need to provide an `intro` template, detailing installation instructions and such,
 | 
				
			||||||
 | 
					plus a generic template for making API requests, that can be filled with individual request details.
 | 
				
			||||||
 | 
					See the [templates for the bundled languages][client-library-templates] for examples.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Third party packages
 | 
					## Third party packages
 | 
				
			||||||
| 
						 | 
					@ -314,3 +335,4 @@ To implement a hypermedia API you'll need to decide on an appropriate media type
 | 
				
			||||||
[image-self-describing-api]: ../img/self-describing.png
 | 
					[image-self-describing-api]: ../img/self-describing.png
 | 
				
			||||||
[schemas-examples]: ../api-guide/schemas/#example
 | 
					[schemas-examples]: ../api-guide/schemas/#example
 | 
				
			||||||
[metadata-docs]: ../api-guide/metadata/
 | 
					[metadata-docs]: ../api-guide/metadata/
 | 
				
			||||||
 | 
					[client-library-templates]: https://github.com/encode/django-rest-framework/tree/master/rest_framework/templates/rest_framework/docs/langs
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,7 +11,10 @@ def get_docs_view(
 | 
				
			||||||
        title=None, description=None, schema_url=None, public=True,
 | 
					        title=None, description=None, schema_url=None, public=True,
 | 
				
			||||||
        patterns=None, generator_class=SchemaGenerator,
 | 
					        patterns=None, generator_class=SchemaGenerator,
 | 
				
			||||||
        authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
 | 
					        authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
 | 
				
			||||||
        permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
 | 
					        permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
 | 
				
			||||||
 | 
					        renderer_classes=None):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if renderer_classes is None:
 | 
				
			||||||
        renderer_classes = [DocumentationRenderer, CoreJSONRenderer]
 | 
					        renderer_classes = [DocumentationRenderer, CoreJSONRenderer]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return get_schema_view(
 | 
					    return get_schema_view(
 | 
				
			||||||
| 
						 | 
					@ -51,7 +54,8 @@ def include_docs_urls(
 | 
				
			||||||
        title=None, description=None, schema_url=None, public=True,
 | 
					        title=None, description=None, schema_url=None, public=True,
 | 
				
			||||||
        patterns=None, generator_class=SchemaGenerator,
 | 
					        patterns=None, generator_class=SchemaGenerator,
 | 
				
			||||||
        authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
 | 
					        authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
 | 
				
			||||||
        permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
 | 
					        permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
 | 
				
			||||||
 | 
					        renderer_classes=None):
 | 
				
			||||||
    docs_view = get_docs_view(
 | 
					    docs_view = get_docs_view(
 | 
				
			||||||
        title=title,
 | 
					        title=title,
 | 
				
			||||||
        description=description,
 | 
					        description=description,
 | 
				
			||||||
| 
						 | 
					@ -60,6 +64,7 @@ def include_docs_urls(
 | 
				
			||||||
        patterns=patterns,
 | 
					        patterns=patterns,
 | 
				
			||||||
        generator_class=generator_class,
 | 
					        generator_class=generator_class,
 | 
				
			||||||
        authentication_classes=authentication_classes,
 | 
					        authentication_classes=authentication_classes,
 | 
				
			||||||
 | 
					        renderer_classes=renderer_classes,
 | 
				
			||||||
        permission_classes=permission_classes,
 | 
					        permission_classes=permission_classes,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    schema_js_view = get_schemajs_view(
 | 
					    schema_js_view = get_schemajs_view(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -830,6 +830,8 @@ class DocumentationRenderer(BaseRenderer):
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            'document': data,
 | 
					            'document': data,
 | 
				
			||||||
            'langs': self.languages,
 | 
					            'langs': self.languages,
 | 
				
			||||||
 | 
					            'lang_htmls': ["rest_framework/docs/langs/%s.html" % l for l in self.languages],
 | 
				
			||||||
 | 
					            'lang_intro_htmls': ["rest_framework/docs/langs/%s-intro.html" % l for l in self.languages],
 | 
				
			||||||
            'code_style': pygments_css(self.code_style),
 | 
					            'code_style': pygments_css(self.code_style),
 | 
				
			||||||
            'request': request
 | 
					            'request': request
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,9 +8,9 @@
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
<div class="col-md-6 intro-code">
 | 
					<div class="col-md-6 intro-code">
 | 
				
			||||||
    {% if 'shell' in langs %}{% include "rest_framework/docs/langs/shell-intro.html" %}{% endif %}
 | 
					    {% for html in lang_intro_htmls %}
 | 
				
			||||||
    {% if 'python' in langs %}{% include "rest_framework/docs/langs/python-intro.html" %}{% endif %}
 | 
					        {% include html %}
 | 
				
			||||||
    {% if 'javascript' in langs %}{% include "rest_framework/docs/langs/javascript-intro.html" %}{% endif %}
 | 
					    {% endfor %}
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
{% if document|data %}
 | 
					{% if document|data %}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,6 +51,7 @@
 | 
				
			||||||
                $('#auth-control').children().removeClass('active');
 | 
					                $('#auth-control').children().removeClass('active');
 | 
				
			||||||
                $('#auth-control').find("[data-auth='session']").closest('li').addClass('active');
 | 
					                $('#auth-control').find("[data-auth='session']").closest('li').addClass('active');
 | 
				
			||||||
            {% endif %}
 | 
					            {% endif %}
 | 
				
			||||||
 | 
						    $('pre.highlight').filter('[data-language="{{ langs | first }}"]').removeClass('hide');
 | 
				
			||||||
        </script>
 | 
					        </script>
 | 
				
			||||||
    </body>
 | 
					    </body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,3 @@
 | 
				
			||||||
{% load rest_framework %}
 | 
					{% load rest_framework %}
 | 
				
			||||||
<pre class="highlight shell" data-language="shell"><code>{% code bash %}# Install the command line client
 | 
					<pre class="highlight shell hide" data-language="shell"><code>{% code bash %}# Install the command line client
 | 
				
			||||||
$ pip install coreapi-cli{% endcode %}</code></pre>
 | 
					$ pip install coreapi-cli{% endcode %}</code></pre>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
{% load rest_framework %}
 | 
					{% load rest_framework %}
 | 
				
			||||||
<pre class="highlight shell" data-language="shell"><code>{% code bash %}# Load the schema document
 | 
					<pre class="highlight shell hide" data-language="shell"><code>{% code bash %}# Load the schema document
 | 
				
			||||||
$ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
 | 
					$ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Interact with the API endpoint
 | 
					# Interact with the API endpoint
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -93,9 +93,9 @@
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <div class="col-md-6 code-samples">
 | 
					    <div class="col-md-6 code-samples">
 | 
				
			||||||
        {% if 'shell' in langs %}{% include "rest_framework/docs/langs/shell.html" %}{% endif %}
 | 
					        {% for html in lang_htmls %}
 | 
				
			||||||
        {% if 'python' in langs %}{% include "rest_framework/docs/langs/python.html" %}{% endif %}
 | 
					            {% include html %}
 | 
				
			||||||
        {% if 'javascript' in langs %}{% include "rest_framework/docs/langs/javascript.html" %}{% endif %}
 | 
					        {% endfor %}
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,12 +31,12 @@
 | 
				
			||||||
            </ul>
 | 
					            </ul>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <li data-toggle="collapse" data-target="#language-control" class="collapsed">
 | 
					            <li data-toggle="collapse" data-target="#language-control" class="collapsed">
 | 
				
			||||||
                <a><i class="fa fa-code fa-lg"></i> Source Code</a> <span id="selected-language">shell</span>
 | 
					                <a><i class="fa fa-code fa-lg"></i> Source Code</a> <span id="selected-language">{{ langs | first }}</span>
 | 
				
			||||||
            </li>
 | 
					            </li>
 | 
				
			||||||
            <ul class="sub-menu collapse out" id="language-control">
 | 
					            <ul class="sub-menu collapse out" id="language-control">
 | 
				
			||||||
                <li class="active"><a href="#" data-language="shell">shell</a></li>
 | 
					            {% for lang in langs %}
 | 
				
			||||||
                <li><a href="#" data-language="javascript">javascript</a></li>
 | 
					                <li{% if loop.first %} class="active"{% endif %}><a href="#" data-language="{{ lang }}">{{ lang }}</a></li>
 | 
				
			||||||
                <li><a href="#" data-language="python">python</a></li>
 | 
						    {% endfor %}
 | 
				
			||||||
            </ul>
 | 
					            </ul>
 | 
				
			||||||
        </ul>
 | 
					        </ul>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user