Move the missing argument verification in a wsme.runtime module so that it can be used in all adapters

This commit is contained in:
Christophe de Vienne 2013-04-08 15:45:08 +02:00
parent d78a48c7ba
commit b2e760894e
7 changed files with 25 additions and 10 deletions

View File

@ -9,6 +9,7 @@ from wsme.exc import ClientSideError, UnknownArgument
from wsme.types import iscomplex, list_attributes, Unset
from wsme.types import UserType, ArrayType, DictType, File
from wsme.utils import parse_isodate, parse_isotime, parse_isodatetime
import wsme.runtime
ARRAY_MAX_SIZE = 1000
@ -273,8 +274,10 @@ def get_args(funcdef, args, kwargs, params, form, body, mimetype):
(from_params, from_form_params, from_body)
)
return combine_args(
args, kwargs = combine_args(
funcdef,
(from_args, from_params_and_body),
allow_override=True
)
wsme.runtime.check_arguments(funcdef, args, kwargs)
return args, kwargs

View File

@ -6,6 +6,7 @@ from wsme.protocol import CallContext, Protocol
import wsme.rest
import wsme.rest.args
import wsme.runtime
log = logging.getLogger(__name__)
@ -120,7 +121,7 @@ class RestProtocol(Protocol):
(wsme.rest.args.args_from_params(funcdef, request.params),
wsme.rest.args.args_from_body(funcdef, body, context.inmime))
)
assert len(args) == 0
wsme.runtime.check_arguments(funcdef, args, kwargs)
return kwargs
def encode_result(self, context, result):

View File

@ -7,7 +7,7 @@ import six
import webob
from wsme.exc import ClientSideError, MissingArgument, UnknownFunction
from wsme.exc import ClientSideError, UnknownFunction
from wsme.protocol import getprotocol
from wsme.rest import scan_api
from wsme import spore
@ -180,10 +180,6 @@ class WSRoot(object):
kw = protocol.read_arguments(context)
args = list(args)
for arg in context.funcdef.arguments:
if arg.mandatory and arg.name not in kw:
raise MissingArgument(arg.name)
txn = self.begin()
try:
result = context.func(*args, **kw)

9
wsme/runtime.py Normal file
View File

@ -0,0 +1,9 @@
from wsme.exc import MissingArgument
def check_arguments(funcdef, args, kw):
"""Check if some arguments are missing"""
assert len(args) == 0
for arg in funcdef.arguments:
if arg.mandatory and arg.name not in kw:
raise MissingArgument(arg.name)

View File

@ -19,6 +19,7 @@ from __future__ import absolute_import
import wsme
from wsme.rest import json as restjson
from wsme.rest import xml as restxml
import wsme.runtime
import functools
from wsme.rest.args import (
@ -76,6 +77,7 @@ def signature(*args, **kwargs):
(args_from_params(funcdef, request.params),
args_from_body(funcdef, request.body, request.content_type))
)
wsme.runtime.check_arguments(funcdef, args, kwargs)
request.override_renderer = 'wsme' + get_outputformat(request)
if funcdef.pass_request:
kwargs[funcdef.pass_request] = request

View File

@ -363,9 +363,11 @@ class ExtDirectProtocol(Protocol):
def read_arguments(self, context):
if isinstance(context, ExtCallContext):
return self.read_std_arguments(context)
kwargs = self.read_std_arguments(context)
elif isinstance(context, FormExtCallContext):
return self.read_form_arguments(context)
kwargs = self.read_form_arguments(context)
wsme.runtime.check_arguments(context.funcdef, (), kwargs)
return kwargs
def encode_result(self, context, result):
return json.dumps({

View File

@ -25,6 +25,8 @@ except ImportError:
from wsme.protocol import CallContext, Protocol, expose
import wsme.types
import wsme.runtime
from wsme import exc
from wsme.utils import parse_isodate, parse_isotime, parse_isodatetime
@ -384,7 +386,7 @@ class SoapProtocol(Protocol):
'type': self.typenamespace,
})
kw[name] = value
wsme.runtime.check_arguments(context.funcdef, (), kw)
return kw
def soap_response(self, path, funcdef, result):