Flask Restplus无法按预期读取URL变量

时间:2019-07-11 17:44:55

标签: python rest flask crud flask-restplus

我正在使用CRUD api(使用应用程序工厂模式),并且我对带有变量的url的请求未正确读取,并且我不确定如何最好地理解它。这是我与Flask-Restful合作的第一个项目,所以我可能缺少明显的东西。

最小示例如下:

Factory.py

from flask import Flask
from flask_mongoengine import MongoEngine
from flask_restplus import Api

from config import config_by_name

db = MongoEngine()
api = Api()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_by_name[config_name])

    db.init_app(app)
    api.init_app(app)

    from .foo import namespace as foo_namespace
    api.add_namespace(foo_namespace)
    return app

foo/__init__.py

from flask_restplus import Namespace

namespace = Namespace(
    "Foo",
    description="Foo and Bar and Baz",
    path="/Foo"
)

from app.foo import routes

foo/routes.py

#foo/routes.py
from flask import request
from flask_restplus import Resource, fields

from app.foo import namespace
from app.foo.models import Foo as Foo_ODM

foo_api_model = namespace.model(
    name="Foo",
    model={
        "name": fields.String(
            required=True,
            description="Identifier for a foo",
            example="GloVe-CommonCrawl-6b"
        ),
        "download_url": fields.Url(
            required=True,
            description="Endpoint for downloading a foo",
            example="http://nlp.stanford.edu/data/glove.6B.zip"
        ),
    }
)


@namespace.route("/<name>")
class Foo(Resource):
    @namespace.marshal_with(foo_api_model)
    def get(self, name):
        """Get metadata for a single foo by name"""
        foo = Foo_ODM.objects.get_or_404(name=name)
        return foo

    @namespace.marshal_with(foo_api_model)
    def delete(self, name):
        """"Remove a foo from the server"""
        # Create Mongo ODM and delete the relevant document
        return 404


@namespace.route("/")
class FooList(Resource):
    def get(self):
        """List avalible Foos"""
        ret_json = []
        for obj in Foo_ODM.objects:
            ret_json.append(
                {"name": obj.name, "download_url": obj.download_url}
            )
            if len(ret_json) >= 100:
                return ret_json, 202
        return ret_json

    @namespace.expect(foo_api_model, validate=True)
    def put(self):
        """Add a new Foo to this server"""
        req_json = request.get_json()
        new_foo = Foo_ODM(
            name=req_json.get("name"),
            download_url=req_json.get("download_url"),
        )
        new_foo.save()

当我请求/foo/时,所有路由均按预期工作。我可以PUT个项目和GET个完整的对象列表。

当我请求/foo/6b(数据库中的文档)时:

curl -X GET "http://localhost:5000/foo/" -H "accept: application/json"

我收到以下错误:

{
  "message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. You have requested this URI [/foo/6b] but did you mean /foo/<name> ?"
}

当我期望像这样的结果时

{
  "name": "6b",
  "download_url": "internet.com/static/6b.tar",
}

好像Flask-Restplus正在以某种方式将url压缩为列表而不是字符串,并且路由中的变量未得到处理或解释。有人有建议吗?谢谢!

0 个答案:

没有答案