summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBarry Warsaw2014-08-13 10:30:13 -0400
committerBarry Warsaw2014-08-13 10:30:13 -0400
commit45691d23d4fb4dca8e7a3d7442186a333e7f9663 (patch)
tree2841f503f93429b2f80e4965897259850bd5056a /src
parent72dd28e26b1fa369de93652bd51869cebd79c0a3 (diff)
downloadmailman-45691d23d4fb4dca8e7a3d7442186a333e7f9663.tar.gz
mailman-45691d23d4fb4dca8e7a3d7442186a333e7f9663.tar.zst
mailman-45691d23d4fb4dca8e7a3d7442186a333e7f9663.zip
Diffstat (limited to 'src')
-rw-r--r--src/mailman/rest/docs/domains.rst7
-rw-r--r--src/mailman/rest/domains.py55
-rw-r--r--src/mailman/rest/lists.py34
-rw-r--r--src/mailman/rest/tests/test_domains.py14
4 files changed, 63 insertions, 47 deletions
diff --git a/src/mailman/rest/docs/domains.rst b/src/mailman/rest/docs/domains.rst
index 92c73ffbf..b28326f73 100644
--- a/src/mailman/rest/docs/domains.rst
+++ b/src/mailman/rest/docs/domains.rst
@@ -110,13 +110,6 @@ The information for a single domain is available by following one of the
self_link: http://localhost:9001/3.0/domains/lists.example.net
url_host: example.net
-But we get a 404 for a non-existent domain.
-
- >>> dump_json('http://localhost:9001/3.0/domains/does-not-exist')
- Traceback (most recent call last):
- ...
- HTTPError: HTTP Error 404: 404 Not Found
-
You can also list all the mailing lists for a given domain. At first, the
example.com domain does not contain any mailing lists.
::
diff --git a/src/mailman/rest/domains.py b/src/mailman/rest/domains.py
index 1a260ea3d..23b1c6b3c 100644
--- a/src/mailman/rest/domains.py
+++ b/src/mailman/rest/domains.py
@@ -26,18 +26,19 @@ __all__ = [
]
-from restish import http, resource
-from zope.component import getUtility
+import falcon
from mailman.interfaces.domain import (
BadDomainSpecificationError, IDomainManager)
-from mailman.rest.helpers import CollectionMixin, etag, no_content, path_to
+from mailman.rest.helpers import (
+ BadRequest, CollectionMixin, NotFound, child, etag, path_to)
from mailman.rest.lists import ListsForDomain
from mailman.rest.validator import Validator
+from zope.component import getUtility
-class _DomainBase(resource.Resource, CollectionMixin):
+class _DomainBase(CollectionMixin):
"""Shared base class for domain representations."""
def _resource_as_dict(self, domain):
@@ -62,41 +63,42 @@ class ADomain(_DomainBase):
def __init__(self, domain):
self._domain = domain
- @resource.GET()
- def domain(self, request):
+ def on_get(self, request, response):
"""Return a single domain end-point."""
domain = getUtility(IDomainManager).get(self._domain)
if domain is None:
- return http.not_found()
- return http.ok([], self._resource_as_json(domain))
+ falcon.responders.path_not_found(request, response)
+ else:
+ response.status = falcon.HTTP_200
+ response.body = self._resource_as_json(domain)
- @resource.DELETE()
- def delete(self, request):
+ def on_delete(self, request, response):
"""Delete the domain."""
try:
getUtility(IDomainManager).remove(self._domain)
except KeyError:
# The domain does not exist.
- return http.not_found()
- return no_content()
+ falcon.responders.path_not_found(
+ request, response, '404 Not Found')
+ else:
+ response.status = falcon.HTTP_204
- @resource.child()
+ @child()
def lists(self, request, segments):
"""/domains/<domain>/lists"""
if len(segments) == 0:
domain = getUtility(IDomainManager).get(self._domain)
if domain is None:
- return http.not_found()
+ return NotFound()
return ListsForDomain(domain)
else:
- return http.bad_request()
+ return BadRequest(), []
class AllDomains(_DomainBase):
"""The domains."""
- @resource.POST()
- def create(self, request):
+ def on_post(self, request, response):
"""Create a new domain."""
domain_manager = getUtility(IDomainManager)
try:
@@ -108,15 +110,18 @@ class AllDomains(_DomainBase):
'contact_address'))
domain = domain_manager.add(**validator(request))
except BadDomainSpecificationError:
- return http.bad_request([], b'Domain exists')
+ falcon.responders.bad_request(
+ request, response, body=b'Domain exists')
except ValueError as error:
- return http.bad_request([], str(error))
- location = path_to('domains/{0}'.format(domain.mail_host))
- # Include no extra headers or body.
- return http.created(location, [], None)
+ falcon.responders.bad_request(
+ request, response, body=str(error))
+ else:
+ location = path_to('domains/{0}'.format(domain.mail_host))
+ response.status = falcon.HTTP_201
+ response.location = location
- @resource.GET()
- def collection(self, request):
+ def on_get(self, request, response):
"""/domains"""
resource = self._make_collection(request)
- return http.ok([], etag(resource))
+ response.status = falcon.HTTP_200
+ response.body = etag(resource)
diff --git a/src/mailman/rest/lists.py b/src/mailman/rest/lists.py
index 552824927..7f0e6e797 100644
--- a/src/mailman/rest/lists.py
+++ b/src/mailman/rest/lists.py
@@ -29,6 +29,8 @@ __all__ = [
]
+import falcon
+
from lazr.config import as_boolean
from operator import attrgetter
from restish import http, resource
@@ -103,7 +105,7 @@ def config_matcher(request, segments):
-class _ListBase(resource.Resource, CollectionMixin):
+class _ListBase(CollectionMixin):
"""Shared base class for mailing list representations."""
def _resource_as_dict(self, mlist):
@@ -205,8 +207,7 @@ class AList(_ListBase):
class AllLists(_ListBase):
"""The mailing lists."""
- @resource.POST()
- def create(self, request):
+ def on_post(self, request, response):
"""Create a new mailing list."""
try:
validator = Validator(fqdn_listname=unicode,
@@ -214,16 +215,19 @@ class AllLists(_ListBase):
_optional=('style_name',))
mlist = create_list(**validator(request))
except ListAlreadyExistsError:
- return http.bad_request([], b'Mailing list exists')
+ falcon.responders.bad_request(
+ request, response, body=b'Mailing list exists')
except BadDomainSpecificationError as error:
- return http.bad_request([], b'Domain does not exist: {0}'.format(
+ falcon.responders.bad_request(
+ request, response, body=b'Domain does not exist: {0}'.format(
error.domain))
except ValueError as error:
- return http.bad_request([], str(error))
- # wsgiref wants headers to be bytes, not unicodes.
- location = path_to('lists/{0}'.format(mlist.list_id))
- # Include no extra headers or body.
- return http.created(location, [], None)
+ falcon.responders.bad_request(
+ request, response, body=str(error))
+ else:
+ location = path_to('lists/{0}'.format(mlist.list_id))
+ response.status = falcon.HTTP_201
+ response.location = location
@resource.GET()
def collection(self, request):
@@ -241,7 +245,7 @@ class MembersOfList(MemberCollection):
self._mlist = mailing_list
self._role = role
- @paginate
+ #@paginate
def _get_collection(self, request):
"""See `CollectionMixin`."""
# Overrides _MemberBase._get_collection() because we only want to
@@ -257,13 +261,13 @@ class ListsForDomain(_ListBase):
def __init__(self, domain):
self._domain = domain
- @resource.GET()
- def collection(self, request):
+ def on_get(self, request, response):
"""/domains/<domain>/lists"""
resource = self._make_collection(request)
- return http.ok([], etag(resource))
+ response.status = falcon.HTTP_200
+ response.body = etag(resource)
- @paginate
+ #@paginate
def _get_collection(self, request):
"""See `CollectionMixin`."""
return list(self._domain.mailing_lists)
diff --git a/src/mailman/rest/tests/test_domains.py b/src/mailman/rest/tests/test_domains.py
index ec08b749d..44cf11ef3 100644
--- a/src/mailman/rest/tests/test_domains.py
+++ b/src/mailman/rest/tests/test_domains.py
@@ -66,3 +66,17 @@ class TestDomains(unittest.TestCase):
'http://localhost:9001/3.0/domains/example.com', method='DELETE')
self.assertEqual(response.status, 204)
self.assertEqual(getUtility(IListManager).get('ant@example.com'), None)
+
+ def test_missing_domain(self):
+ # You get a 404 if you try to access a nonexisting domain.
+ with self.assertRaises(HTTPError) as cm:
+ call_api('http://localhost:9001/3.0/domains/does-not-exist.com')
+ self.assertEqual(cm.exception.code, 404)
+
+ def test_missing_domain_lists(self):
+ # You get a 404 if you try to access the mailing lists of a
+ # nonexisting domain.
+ with self.assertRaises(HTTPError) as cm:
+ call_api(
+ 'http://localhost:9001/3.0/domains/does-not-exist.com/lists')
+ self.assertEqual(cm.exception.code, 404)