[RFC 1/2] REST: Add base configuration hooks for a REST API

Finucane, Stephen stephen.finucane at intel.com
Fri Mar 25 02:36:21 AEDT 2016


On 16 Mar 11:13, Andy Doan wrote:
> This adds the ability to expose a REST API based on the Django REST
> framework project. Since this project isn't packaged in most current
> distributions, we ensure that its both installed and enabled before
> trying to use it.

Having hacked around with tastypie myself, I think going with DRF is
likely the best approach at this point. This series is a good start,
comments below withstanding. We should obviously use as much of
Damien's work as possible also, rather than reinventing the wheel.

However, I would like to scope out the endpoints we're going to expose
and, to a lesser degree (because we can fix mistakes with API
revisions) the kind of responses we would return. If you agree, then I
think we can do this as a formalized document at the beginning, or
develop said document iteratively through many small series like this
one. I have a draft Swagger/OpenAPI document that I could provide, if
this would help. Thoughts?

Stephen

> Signed-off-by: Andy Doan <andy.doan at linaro.org>
> Inspired-by: Damien Lespiau <damien.lespiau at intel.com>
>
> ---
>  patchwork/settings/base.py  | 11 +++++++++++
>  patchwork/urls.py           |  9 +++++++++
>  patchwork/views/rest_api.py | 23 +++++++++++++++++++++++
>  requirements-test.txt       |  1 +
>  4 files changed, 44 insertions(+)
>  create mode 100644 patchwork/views/rest_api.py
> 
> diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
> index ef2a9ee..7ef6e31 100644
> --- a/patchwork/settings/base.py
> +++ b/patchwork/settings/base.py
> @@ -27,6 +27,14 @@ INSTALLED_APPS = [
>      'patchwork',
>  ]
>  
> +try:
> +    # django rest framework isn't a standard package in most distros, so
> +    # don't make it cumpulsory
> +    import rest_framework  # NOQA
> +    INSTALLED_APPS.append('rest_framework')
> +except ImportError:
> +    pass
> +

IMO, if 'ENABLE_REST_API' is 'True' then this should actually raise the
error, so check that value instead of using a try..catch? Note: I do
realise that placing 'ENABLE_REST_API' higher in the file will mess up
the structure of the file, but it seems the lesser of two evils :)

>  # HTTP
>  
>  MIDDLEWARE_CLASSES = [
> @@ -136,6 +144,9 @@ NOTIFICATION_FROM_EMAIL = DEFAULT_FROM_EMAIL
>  # Set to True to enable the Patchwork XML-RPC interface
>  ENABLE_XMLRPC = False
>  
> +# Set to True to enable the Patchwork REST API
> +ENABLE_REST_API = False
> +
>  # Set to True to enable redirections or URLs from previous versions
>  # of patchwork
>  COMPAT_REDIR = True
> diff --git a/patchwork/urls.py b/patchwork/urls.py
> index 022b92c..095df75 100644
> --- a/patchwork/urls.py
> +++ b/patchwork/urls.py
> @@ -132,6 +132,15 @@ if settings.ENABLE_XMLRPC:
>              name='pwclientrc'),
>      ]
>  
> +if settings.ENABLE_REST_API:
> +    if 'rest_framework' not in settings.INSTALLED_APPS:
> +        raise RuntimeError(
> +            'djangorestframework must be installed to enable the REST API.')

Doing the above allows you to remove this check.

> +    import patchwork.views.rest_api
> +    urlpatterns += [
> +        url(r'^api/1.0/', include(patchwork.views.rest_api.router.urls)),
> +    ]
> +
>  # redirect from old urls
>  if settings.COMPAT_REDIR:
>      urlpatterns += [
> diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py
> new file mode 100644
> index 0000000..a39bcf4
> --- /dev/null
> +++ b/patchwork/views/rest_api.py
> @@ -0,0 +1,23 @@
> +# Patchwork - automated patch tracking system
> +# Copyright (C) 2008 Jeremy Kerr <jk at ozlabs.org>

nit: I doubt you need this? Looks like new code below.

> +# Copyright (C) 2016 Linaro Corporation
> +#
> +# This file is part of the Patchwork package.
> +#
> +# Patchwork is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +#
> +# Patchwork is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with Patchwork; if not, write to the Free Software
> +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
> +
> +from rest_framework import routers
> +
> +router = routers.DefaultRouter()
> diff --git a/requirements-test.txt b/requirements-test.txt
> index 2d0b32c..fb6a8c8 100644
> --- a/requirements-test.txt
> +++ b/requirements-test.txt
> @@ -2,3 +2,4 @@ mysqlclient==1.3.7  # replace this with psycopg2 for a PostgreSQL backend
>  django-debug-toolbar==1.4
>  python-dateutil>2.0,<3.0
>  selenium>2.0,<3.0
> +djangorestframework==3.3.3
> -- 
> 2.7.0


More information about the Patchwork mailing list