Skip to content

Commit

Permalink
Merge pull request #225 from onefinestay/request_arg
Browse files Browse the repository at this point in the history
http entrypoint to take a requset arg
  • Loading branch information
davidszotten committed Mar 30, 2015
2 parents 995fb1b + dd6c9c7 commit d023657
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
6 changes: 3 additions & 3 deletions docs/examples/advanced_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Service(object):
name = "advanced_http_service"

@http('GET', '/privileged')
def forbidden(self):
def forbidden(self, request):
return 403, "Forbidden"

@http('GET', '/headers')
def redirect(self):
def redirect(self, request):
return 201, {'Location': 'https://www.example.com/widget/1'}, ""

@http('GET', '/custom')
def custom(self):
def custom(self, request):
return Response("payload")
6 changes: 3 additions & 3 deletions docs/examples/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class HttpService(object):
name = "http_service"

@http('GET', '/get/<int:value>')
def get_method(self, value):
def get_method(self, request, value):
return json.dumps({'value': value})

@http('POST', '/post')
def do_post(self, body):
return "received: {}".format(body)
def do_post(self, request):
return "received: {}".format(request.get_data())
11 changes: 3 additions & 8 deletions nameko/web/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,16 @@ def stop(self):
self.server.unregister_provider(self)
super(HttpRequestHandler, self).stop()

def process_request_data(self, request):
if request.method == 'POST':
data = request.get_data()
args = (data,)
else:
args = ()

def get_entrypoint_parameters(self, request):
args = (request,)
kwargs = request.path_values
return args, kwargs

def handle_request(self, request):
request.shallow = False
try:
context_data = self.server.context_data_from_headers(request)
args, kwargs = self.process_request_data(request)
args, kwargs = self.get_entrypoint_parameters(request)

self.check_signature(args, kwargs)
event = Event()
Expand Down
16 changes: 8 additions & 8 deletions test/web/test_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ class ExampleService(object):
name = "exampleservice"

@http('GET', '/foo/<int:bar>')
def do_get(self, bar):
def do_get(self, request, bar):
return 'value: {}'.format(bar)

@http('POST', '/post')
def do_post(self, payload):
data = json.loads(payload)
def do_post(self, request):
data = json.loads(request.get_data())
value = data['value']

return value

@http('GET', '/custom')
def do_custom(self):
def do_custom(self, request):
return Response('response')

@http('GET', '/status_code')
def do_status_code(self):
def do_status_code(self, request):
return 201, 'created'

@http('GET', '/headers')
def do_headers(self):
def do_headers(self, request):
return 201, {'x-foo': 'bar'}, 'created'

@http('GET', '/fail')
def fail(self):
def fail(self, request):
raise ValueError('oops')

@http('GET', '/fail_expected', expected_exceptions=ValueError)
def fail_expected(self):
def fail_expected(self, request):
raise ValueError('oops')


Expand Down
4 changes: 2 additions & 2 deletions test/web/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class ExampleService(object):
name = "exampleservice"

@http('GET', '/')
def do_index(self):
def do_index(self, request):
return ''

@http('GET', '/large')
def do_large(self):
def do_large(self, request):
# more than a buffer's worth
return 'x' * (10**6)

Expand Down

0 comments on commit d023657

Please sign in to comment.