Skip to content

Use _MiddlewareFactory as decorator with arguments#228

Merged
digitalresistor merged 6 commits into
Pylons:masterfrom
ryd994:master
Jul 30, 2016
Merged

Use _MiddlewareFactory as decorator with arguments#228
digitalresistor merged 6 commits into
Pylons:masterfrom
ryd994:master

Conversation

@ryd994
Copy link
Copy Markdown

@ryd994 ryd994 commented Dec 29, 2015

This change allows _MiddlewareFactory to be used as decorator with argument:

@wsgify.middleware
def restrict_ip(req, app, ips):
    if req.remote_addr not in ips:
        raise webob.exc.HTTPForbidden('Bad IP: %s' % req.remote_addr)
    return app

@restrict_ip(ips=['127.0.0.1'])
@wsgify
def app(req):
      return 'hi'

@digitalresistor digitalresistor added this to the Version 1.6 milestone Dec 29, 2015
@mmerickel
Copy link
Copy Markdown
Member

@renyidong Could you update this PR with a test case showing your intended workflow? I think the idea itself is fine!

@digitalresistor digitalresistor removed this from the Version 1.6 milestone Jan 29, 2016
@ryd994
Copy link
Copy Markdown
Author

ryd994 commented Jan 29, 2016

@mmerickel Taking the example from http://docs.webob.org/en/latest/api/dec.html

@wsgify.middleware
def restrict_ip(req, app, ips):
    if req.remote_addr not in ips:
        raise webob.exc.HTTPForbidden('Bad IP: %s' % req.remote_addr)
    return app

@wsgify
def app(req):
    return 'hi'

wrapped = restrict_ip(app, ips=['127.0.0.1'])

I have to define an extra function and wrap it with the middleware.

With this patch, I would be able to achieve same effect as the example at very beginning.

@mmerickel
Copy link
Copy Markdown
Member

@renyidong I understand the patch. I was asking if you could please add a test case to the codebase to ensure this keeps working in the future.

@ryd994
Copy link
Copy Markdown
Author

ryd994 commented Jan 30, 2016

@mmerickel Apologies for my misinterpret. I appended test code to test_middleware(). Or you would prefer adding a separate test?

@digitalresistor
Copy link
Copy Markdown
Member

A separate test would be preferred. Thanks!

@ryd994
Copy link
Copy Markdown
Author

ryd994 commented Jan 31, 2016

@bertjwregeer Let me know if there is anything else you need.

@mmerickel
Copy link
Copy Markdown
Member

LGTM!

@digitalresistor digitalresistor added this to the 1.7.0 milestone Jul 30, 2016
@digitalresistor digitalresistor merged commit 686b859 into Pylons:master Jul 30, 2016
digitalresistor added a commit that referenced this pull request Jul 30, 2016
@digitalresistor
Copy link
Copy Markdown
Member

Sorry for taking so long! I've merged this into master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants