最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】flask中用reqparse实现枚举参数和嵌套参数

Flask crifan 5364浏览 0评论

flask reqparse 枚举

flask reqparse 枚举

python – Nested validation with the flask-restful RequestParser – Stack Overflow

Request Parsing — Flask-RESTful 0.2.1 documentation

Building beautiful RESTful APIs using Flask, Swagger UI and Flask-RESTPlus – Michał Karzyński

flask reqparse para enum

flask reqparse nested para

Support validation of nested arguments · Issue #149 · flask-restful/flask-restful · GitHub

flask RequestParser nested para

GitHub – noirbizarre/flask-restplus: Fully featured framework for fast, easy and documented API development with Flask

Flask RESTful 中文文档 – [ Flask 扩展文档汇总 ]

【总结】

最后实现了枚举参数:

就没有去用reqparse的枚举的支持

-》因为reqparse的枚举会导致:

如果输入的参数,不在所允许的字符串范围内,就会抛出参数异常的错误而直接返回对应的错误的信息了

-》而reqparse的错误信息,与我自己要返回的信息格式又不一致,且又无法自定义,所以只能放弃

-》暂时还只是作为普通的字符串输入,内部再去转换为enmu变量

-》以及用额外的RequestParser实现嵌套参数的解析:

class TaskAPI(Resource):
    decorators = [login_required]
    def __init__(self):
        self.rootParser = reqparse.RequestParser()
        self.rootParser.add_argument(‘initiatorStartLocation’,
                                   type=dict,
                                   location=‘json’)
        self.rootParser.add_argument(‘itemTypeStr’,
                                   type=unicode,
                                   default="",
                                   location=’json’)
        self.rootArgs = self.rootParser.parse_args()
        self.initiatorStartLocationParser = reqparse.RequestParser()
        self.initiatorStartLocationParser.add_argument(‘longitude’, type=float,  location=(‘initiatorStartLocation’,))
        self.initiatorStartLocationParser.add_argument(‘latitude’, type=float,   location=(‘initiatorStartLocation’,))
        self.initiatorStartLocationParser.add_argument(‘shortStr’, type=unicode, location=(‘initiatorStartLocation’,))
        self.initiatorStartLocationParser.add_argument(‘fullStr’,  type=unicode, location=(‘initiatorStartLocation’,))
        self.initiatorStartLocationArgs = self.initiatorStartLocationParser.parse_args(req=self.rootArgs)
        super(TaskAPI, self).__init__()
   def post(self, userId):
        gLog.debug("self.rootArgs=%s", self.rootArgs)
        initiatorStartLocationDict = self.rootArgs["initiatorStartLocation"]
        gLog.debug("initiatorStartLocationDict=%s", initiatorStartLocationDict)
        itemTypeStr = self.rootArgs["itemTypeStr"]
        gLog.debug("itemTypeStr=%s", itemTypeStr)
        itemType = None
        try:
            itemType = ItemType(itemTypeStr)
            gLog.debug("itemType=%s", itemType)  # itemType=ItemType.Small
        except ValueError as valueErr:
            gLog.debug("value error %s for itemTypeStr=%s", valueErr, itemTypeStr)
            # ValueError:  is not a valid ItemType
            return genRespFailDict(code=BadRequest.code, message="invalid item type %s" % (itemTypeStr))
        initiatorStartLocation = Location(longitude=initiatorStartLocationDict["longitude"],
                                             latitude=initiatorStartLocationDict["latitude"],
                                             shortStr=initiatorStartLocationDict["shortStr"],
                                             fullStr=initiatorStartLocationDict["fullStr"]
                                          )
        db.session.add(initiatorStartLocation)
        db.session.flush()  # to assign id value to location instance
        gLog.debug(‘after  flush initiatorStartLocation=%s’, initiatorStartLocation)

转载请注明:在路上 » 【已解决】flask中用reqparse实现枚举参数和嵌套参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.184 seconds, using 22.14MB memory