Skip to content

Commit

Permalink
version bump and changelog update
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeywaites committed Feb 14, 2018
1 parent 20851f4 commit dc08991
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 125 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,13 @@
Changelog
========================

v1.0.1
-----------------------

* Fixed issue with Endpoint middleware. #11

v1.0.0
-----------------------

Official release of Arrested.

2 changes: 1 addition & 1 deletion arrested/__init__.py
@@ -1,5 +1,5 @@

__version__ = '0.1.0'
__version__ = '0.1.1'

from .api import *
from .endpoint import *
Expand Down
81 changes: 63 additions & 18 deletions arrested/endpoint.py
Expand Up @@ -66,35 +66,80 @@ class Endpoint(MethodView):
#: A list of functions called after all requests are dispatched
after_all_hooks = []

def process_before_request_hooks(self):
"""Process the list of before_{method}_hooks and the before_all_hooks. The hooks
will be processed in the following order
1 - any before_all_hooks defined on the :class:`arrested.ArrestedAPI` object
2 - any before_all_hooks defined on the :class:`arrested.Resource` object
3 - any before_all_hooks defined on the :class:`arrested.Endpoint` object
4 - any before_{method}_hooks defined on the :class:`arrested.Endpoint` object
"""

hooks = []

if self.resource:
hooks.extend(self.resource.api.before_all_hooks)
hooks.extend(self.resource.before_all_hooks)

hooks.extend(self.before_all_hooks)
hooks.extend(
getattr(
self,
'before_{method}_hooks'.format(method=self.meth),
[]
)
)

for hook in chain(hooks):
hook(self)

def process_after_request_hooks(self, resp):
"""Process the list of before_{method}_hooks and the before_all_hooks. The hooks
will be processed in the following order
1 - any after_{method}_hooks defined on the :class:`arrested.Endpoint` object
2 - any after_all_hooks defined on the :class:`arrested.Endpoint` object
2 - any after_all_hooks defined on the :class:`arrested.Resource` object
4 - any after_all_hooks defined on the :class:`arrested.ArrestedAPI` object
"""

hooks = []
meth_hooks = getattr(
self,
'after_{method}_hooks'.format(method=self.meth),
[]
)

hooks.extend(meth_hooks)
hooks.extend(self.after_all_hooks)

if self.resource:
hooks.extend(self.resource.after_all_hooks)
hooks.extend(self.resource.api.after_all_hooks)

for hook in chain(hooks):
resp = hook(self, resp)

return resp

def dispatch_request(self, *args, **kwargs):
"""Dispatch the incoming HTTP request to the appropriate handler.
"""
self.args = args
self.kwargs = kwargs
meth = request.method.lower()

if request.method not in self.methods:
self.return_error(405)
self.meth = request.method.lower()
self.resource = current_app.blueprints.get(request.blueprint, None)

resource = current_app.blueprints.get(request.blueprint, None)
if resource:
for hook in chain(resource.api.before_all_hooks, resource.before_all_hooks):
hook(self)
if not any([self.meth in self.methods, self.meth.upper() in self.methods]):
return self.return_error(405)

before_hooks = getattr(self, 'before_{method}_hooks'.format(method=meth))
for hook in before_hooks:
hook(self)
self.process_before_request_hooks()

resp = super(Endpoint, self).dispatch_request(*args, **kwargs)
resp = self.make_response(resp)

after_hooks = getattr(self, 'after_{method}_hooks'.format(method=meth))
for hook in after_hooks:
resp = hook(self, resp)

if resource:
for hook in chain(resource.after_all_hooks, resource.api.after_all_hooks):
resp = hook(self, resp)
resp = self.process_after_request_hooks(resp)

return resp

Expand Down
1 change: 0 additions & 1 deletion tests/test_api.py
Expand Up @@ -130,7 +130,6 @@ def get(self, *args, **kwargs):
)

resp = client.get(url_for('example.test'))
print(resp.data)
assert resp.data == b'request|resource_after|api_after'
assert evts == ['api_before', 'resource_before', 'resource_after', 'api_after']

Expand Down

0 comments on commit dc08991

Please sign in to comment.