Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bc836aa
Initial pass at schema support
lovelydinosaur Jun 8, 2016
b64340b
Add coreapi to optional requirements.
lovelydinosaur Jun 9, 2016
c890ad4
Clean up test failures
lovelydinosaur Jun 9, 2016
2d28390
Add missing newline
lovelydinosaur Jun 9, 2016
744dba4
Minor docs update
lovelydinosaur Jun 9, 2016
56ece73
Version bump for coreapi in requirements
lovelydinosaur Jun 9, 2016
99adbf1
Catch SyntaxError when importing coreapi with python 3.2
lovelydinosaur Jun 9, 2016
80c595e
Add --diff to isort
lovelydinosaur Jun 9, 2016
47c7765
Import coreapi from compat
lovelydinosaur Jun 9, 2016
29e228d
Fail gracefully if attempting to use schemas without coreapi being in…
lovelydinosaur Jun 9, 2016
eeffca4
Tutorial updates
lovelydinosaur Jun 9, 2016
6c60f58
Docs update
lovelydinosaur Jun 9, 2016
b7fcdd2
Initial schema generation & first tutorial 7 draft
lovelydinosaur Jun 10, 2016
2e60f41
Spelling
lovelydinosaur Jun 10, 2016
2ffa145
Remove unused variable
lovelydinosaur Jun 10, 2016
b709dd4
Docs tweak
lovelydinosaur Jun 10, 2016
474a23e
Merge branch 'master' into schema-support
lovelydinosaur Jun 14, 2016
4822896
Added SchemaGenerator class
lovelydinosaur Jun 15, 2016
1f76cca
Fail gracefully if coreapi is not installed and SchemaGenerator is used
lovelydinosaur Jun 15, 2016
cad24b1
Schema docs, pagination controls, filter controls
lovelydinosaur Jun 21, 2016
8fb2602
Resolve NameError
lovelydinosaur Jun 21, 2016
b438281
Add 'view' argument to 'get_fields()'
lovelydinosaur Jun 21, 2016
8519b4e
Remove extranous blank line
lovelydinosaur Jun 21, 2016
2f5c974
Add integration tests for schema generation
lovelydinosaur Jun 22, 2016
e78753d
Only set 'encoding' if a 'form' or 'body' field exists
lovelydinosaur Jun 22, 2016
84bb5ea
Do not use schmea in tests if coreapi is not installed
lovelydinosaur Jun 24, 2016
63e8467
Inital pass at API client docs
lovelydinosaur Jun 29, 2016
bdbcb33
Inital pass at API client docs
lovelydinosaur Jun 29, 2016
7236af3
More work towards client documentation
lovelydinosaur Jun 30, 2016
89540ab
Add coreapi to optional packages list
lovelydinosaur Jul 4, 2016
e3ced75
Clean up API clients docs
lovelydinosaur Jul 4, 2016
12be5b3
Resolve typo
lovelydinosaur Jul 4, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tutorial updates
  • Loading branch information
lovelydinosaur committed Jun 9, 2016
commit eeffca40eb9c40cc3b7ac61d7eaa7426d14b0bb3
Binary file added docs/img/corejson-format.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[tut-4]: tutorial/4-authentication-and-permissions.md
[tut-5]: tutorial/5-relationships-and-hyperlinked-apis.md
[tut-6]: tutorial/6-viewsets-and-routers.md
[tut-7]: tutorial/7-schemas-and-client-libraries.md

[request]: api-guide/requests.md
[response]: api-guide/responses.md
Expand Down
88 changes: 75 additions & 13 deletions docs/tutorial/7-schemas-and-client-libraries.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,84 @@
# Tutorial 7: Schemas & Client Libraries
# Tutorial 7: Schemas & client libraries

An API schema is a document that describes the available endpoints that
a service provides. Schemas are a useful tool for documentation, and can also
be used to provide information to client libraries, allowing for simpler and
more robust interaction with an API.
A schema is a machine-readable document that describes the available API
endpoints, their URLS, and what operations they support.

Schemas can be a useful tool for auto-generated documentation, and can also
be used to drive dynamic client libraries that can interact with the API.

## Core API

In order to provide schema support REST framework uses [Core API][coreapi].

Core API is a document specification for describing APIs. It is used to provide
an internal representation format of the available endpoints and possible
interactions that an API exposes. It can either be used server-side, or
client-side.

When used server-side, Core API allows an API to support rendering to a wide
range of schema or hypermedia formats.

When used client-side, Core API allows for dynamically driven client libraries
that can interact with any API that exposes a supported schema or hypermedia
format.

## Adding a schema

REST framework supports either explicitly defined schema views, or
automatically generated schemas. Since we're using viewsets and routers,
we can simply use the automatic schema generation.

To include a schema for our API, we add a `schema_title` argument to the
router instantiation.
You'll need to install the `coreapi` python package in order to include an
API schema.

$ pip install coreapi

We can now include a schema for our API, by adding a `schema_title` argument to
the router instantiation.

router = DefaultRouter(schema_title='Pastebin API')

If you visit the root of the API in a browser you should now see ... TODO
If you visit the API root endpoint in a browser you should now see `corejson`
representation become available as an option.

![Schema format](../img/corejson-format.png)

We can also request the schema from the command line, by specifying the desired
content type in the `Accept` header.

$ http http://127.0.0.1:8000/ Accept:application/vnd.coreapi+json
HTTP/1.0 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/vnd.coreapi+json

{
"_meta": {
"title": "Pastebin API"
},
"_type": "document",
...

## Using a command line client

Now that our API is exposing a schema endpoint, we can use a dynamic client
library to interact with the API. To demonstrate this, let's install the
Core API command line client.
library to interact with the API. To demonstrate this, let's use the
Core API command line client. We've already installed the `coreapi` package
using `pip`, so the client tool should already be available. Check that it
is available on the command line...

$ coreapi
Usage: coreapi [OPTIONS] COMMAND [ARGS]...

Command line client for interacting with CoreAPI services.

Visit http://www.coreapi.org for more information.

$ pip install coreapi-cli
Options:
--version Display the package version number.
--help Show this message and exit.

Commands:
...

First we'll load the API schema using the command line client.

Expand All @@ -50,6 +104,8 @@ We can now interact with the API using the command line client:

$ coreapi action list_snippets

## Authenticating our client

TODO - authentication

$ coreapi action snippets create --param code "print('hello, world')"
Expand All @@ -58,8 +114,14 @@ TODO - authentication

## Using a client library

TODO
*TODO - document using python client library, rather than the command line tool.*

## Using another schema format

*TODO - document using OpenAPI instead.*

## Customizing schema generation

TODO
*TODO - document writing an explict schema view.*

[coreapi]: http://www.coreapi.org
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pages:
- '4 - Authentication and permissions': 'tutorial/4-authentication-and-permissions.md'
- '5 - Relationships and hyperlinked APIs': 'tutorial/5-relationships-and-hyperlinked-apis.md'
- '6 - Viewsets and routers': 'tutorial/6-viewsets-and-routers.md'
- '7 - Schemas and client libraries': 'tutorial/7-schemas-and-client-libraries.md'
- API Guide:
- 'Requests': 'api-guide/requests.md'
- 'Responses': 'api-guide/responses.md'
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def get_api_root_view(self):
for prefix, viewset, basename in self.registry:
api_root_dict[prefix] = list_name.format(basename=basename)

view_renderers = api_settings.DEFAULT_RENDERER_CLASSES
view_renderers = list(api_settings.DEFAULT_RENDERER_CLASSES)

if self.schema_title:
assert coreapi, '`coreapi` must be installed for schema support.'
Expand Down
2 changes: 1 addition & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

FLAKE8_ARGS = ['rest_framework', 'tests', '--ignore=E501']

ISORT_ARGS = ['--recursive', '--diff', '-p', 'tests', 'rest_framework', 'tests']
ISORT_ARGS = ['--recursive', '--check-only', '-o' 'uritemplate', '-p', 'tests', 'rest_framework', 'tests']

sys.path.append(os.path.dirname(__file__))

Expand Down