将生成的 Flask 应用程序代码 (Swagger-Codegen) 粘合到后端实现的最简洁方法 [英] cleanest way to glue generated Flask app code (Swagger-Codegen) to backend implementation

查看:34
本文介绍了将生成的 Flask 应用程序代码 (Swagger-Codegen) 粘合到后端实现的最简洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

  1. 一个可以做 [Stuff] 的库
  2. 一个 swagger API 定义,大致是 #1,有细微差别以干净地映射到 REST 服务
  3. 使用 Swagger-Codegen 生成 #2 的烧瓶应用程序 - 例如,python 控制器功能与 #1 大致一对一.

我的意图是,flask 应用程序(所有生成的代码)应该只处理实际 REST api 的映射和参数解析,以匹配以 swagger 编码的 API 规范.在任何参数解析(同样是生成的代码)之后,它应该直接调用我的(非生成的)后端.

My intent is that the flask app (all generated code) should only handle mapping that actual REST api and parameter parsing to match the API spec coded in swagger. After any parameter parsing (again, generated code) it should call directly over to my (non-generated) backend.

我的问题是,如何在不手动编辑生成的 python/flask 代码的情况下最好地连接它们?(对我的设计的反馈,或者实现这一点的正式设计模式的细节也很棒;我是这个领域的新手).

My question is, how best to hook these up withOUT hand-editing the generated python/flask code? (Feedback on my design, or details of a formal design pattern that accomplishes this would be great too; I'm new to this space).

刚从生成器开始,我最终得到了 python 函数,例如:

Fresh from the generator, I end up with python functions like:

def create_task(myTaskDefinition):
    """
    comment as specified in swagger.json
    :param myTaskDefinition: json blah blah blah
    :type myTaskDefinition: dict | bytes
    :rtype: ApiResponse
    """
    if connexion.request.is_json:
        myTaskDefinition = MyTaskTypeFromSwagger.from_dict(connexion.request.get_json())
    return 'do some magic!' # swagger codegen inserts this string :)

在后端我有我的实际逻辑:

On the backend I have my actual logic:

def create_task_backend(myTaskDefinition):
    # hand-coded, checked into git: do all the things
    return APIResponse(...)

create_task() 调用 create_task_backend() 的正确方法是什么?

What is the right way to get create_task() to call create_task_backend()?

当然,如果我对我的 swagger 规范进行重大更改,无论如何我都必须手动更新未生成的代码;但是,我可能想要重新生成我的 API 的原因有很多(例如,添加/优化 MyTaskTypeFromSwagger 类,或者根本跳过检查 git 生成的代码)以及是否必须手动编辑生成的 API 代码,然后所有这些编辑都会随着每次重新生成而消失.

Of course if I make breaking changes to my swagger spec I will have to hand-update the non-generated code regardless; however there are many reasons I may want to re-generate my API (say, add/refine the MyTaskTypeFromSwagger class, or skip checking into git the generated code at all) and if I have to hand-edit the generated API code, then all those edits are blown away with each re-generation.

当然,我可以用 ~simple 语法编写它,例如.解析;虽然这是我第一次遇到这个问题,但它似乎已经得到广泛解决!

Of course I could script this with a ~simple grammar in eg. pyparsing; but while this is my first time with this issue, it seems likely it's been widely solved already!

推荐答案

以下方法对我有用:

  • 创建了三个目录:

  • created three directories:

  • src - 对于我的代码,
  • src-gen 用于 swagger 生成的代码,
  • codegen 在其中我放了一个脚本来生成服务器以及一些技巧.
  • src - for my code,
  • src-gen for the swagger generated code,
  • codegen in which I have put a script that generate the server along with a few tricks.

我将所有模板(在 swagger build 中可用)复制到 codegen/templates 并编辑了 controller.mustache 以引用 src/server_impl,所以它可以使用我自己的代码.编辑使用模板语言,所以它是通用的.它仍然不完美(我会更改一些命名约定)但它可以完成工作.所以,首先添加到controller.mustache:

I copied all the templates (available in the swagger build) to codegen/templates and edited the controller.mustache to refer to src/server_impl, so it can use my own code. The editing uses the template language so it is generic. Still it is not perfect (I would change a few naming conventions) but it does the job. So, first add to controller.mustache:

from {{packageName}}.server_impl.controllers_impl import {{classname}}_impl

然后添加以下内容而不是 return 'do some magic!':

then add instead of return 'do some magic!' the following:

return {{classname}}_impl.{{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})

  • 脚本:
    • src 有一个 server_impl 目录.
    • 它创建了一个符号链接,以便 server_impl 可以作为 python 模块导入
      • Script:
        • The src has a server_impl directory.
        • It creates a symobolic link so that server_impl can be imported as a python module
        • cd ../src-gen/swagger_server/
          ln -s ../../src/server_impl/
          cd ../../codegen
          java -jar swagger-codegen-cli.jar generate  
          -i /path_to_your_swagger definition.yaml 
          -l python-flask 
          -o ../src-gen 
          -t ./templates
          cd ../src-gen/
          python3 -m swagger_server
          

          这篇关于将生成的 Flask 应用程序代码 (Swagger-Codegen) 粘合到后端实现的最简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆