Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'webargs' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
def error400(request):
def always_fail(value):
raise ValidationError("something went wrong", status_code=400)
argmap = {"text": fields.Str(validate=always_fail)}
return parser.parse(argmap, request)
def test_nested_many(web_request, parser):
web_request.json = {"pets": [{"name": "Pips"}, {"name": "Zula"}]}
args = {"pets": fields.Nested({"name": fields.Str()}, required=True, many=True)}
parsed = parser.parse(args, web_request)
assert parsed == {"pets": [{"name": "Pips"}, {"name": "Zula"}]}
web_request.json = {}
with pytest.raises(ValidationError):
parser.parse(args, web_request)
def always_error():
def always_fail(value):
raise ma.ValidationError("something went wrong")
args = {"text": fields.Str(validate=always_fail)}
return parser.parse(args)
import json
from flask import Flask, jsonify as J, Response, request
from flask.views import MethodView
import marshmallow as ma
from webargs import fields, ValidationError, missing
from webargs.flaskparser import parser, use_args, use_kwargs
from webargs.core import MARSHMALLOW_VERSION_INFO
class TestAppConfig:
TESTING = True
hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}
class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
strict_kwargs = {"strict": True} if MARSHMALLOW_VERSION_INFO[0] < 3 else {}
hello_many_schema = HelloSchema(many=True, **strict_kwargs)
app = Flask(__name__)
app.config.from_object(TestAppConfig)
@app.route("/echo", methods=["GET", "POST"])
def echo():
def ModelFilterSet(self, session, models):
class ModelFilterSet(FilterSet):
class Meta:
model = models.Album
query = session.query(models.Album)
operators = (operators.Equal, operators.In)
parser = parser
name__like = Filter('name', fields.Str(), operator=operators.Like)
return ModelFilterSet
import json
import falcon
import marshmallow as ma
from webargs import fields, ValidationError
from webargs.falconparser import parser, use_args, use_kwargs
from webargs.core import MARSHMALLOW_VERSION_INFO
hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}
class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
strict_kwargs = {"strict": True} if MARSHMALLOW_VERSION_INFO[0] < 3 else {}
hello_many_schema = HelloSchema(many=True, **strict_kwargs)
class Echo(object):
def on_get(self, req, resp):
parsed = parser.parse(hello_args, req)
resp.body = json.dumps(parsed)
def test_it_should_parse_json_arguments(self):
attrs = {"string": fields.Str(), "integer": fields.List(fields.Int())}
request = make_json_request({"string": "value", "integer": [1, 2]})
parsed = parser.parse(attrs, request)
assert parsed["integer"] == [1, 2]
assert parsed["string"] == value
def test_it_should_parse_multiple_arg_required(self):
args = {"foo": fields.List(fields.Int(), required=True)}
request = make_json_request({})
msg = "Missing data for required field."
with pytest.raises(tornado.web.HTTPError, match=msg):
parser.parse(args, request)
@use_args(api_args)
def apidog(args):
sleep(2)
return handle_request("dognzb", args)
def test_parse_list_allow_none(parser, web_request):
web_request.json = {"foo": None}
args = {"foo": fields.List(fields.Field(allow_none=True), allow_none=True)}
assert parser.parse(args, web_request) == {"foo": None}