python - cleanest way to glue generated Flask app code (Swagger-Codegen) to backend implementation -


i have:

  1. a library [stuff]
  2. a swagger api definition, #1 minor differences map cleanly rest service
  3. a flask app generated #2 using swagger-codegen - eg results in python controller functions one-to-one #1.

my intent flask app (all generated code) should handle mapping actual rest api , parameter parsing match api spec coded in swagger. after parameter parsing (again, generated code) should call directly on (non-generated) backend.

my question is, how best hook these without hand-editing generated python/flask code? (feedback on design, or details of formal design pattern accomplishes great too; i'm new space).

fresh generator, end python functions like:

def create_task(mytaskdefinition):     """     comment 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 magic!' # swagger codegen inserts string :) 

on backend have actual logic:

def create_task_backend(mytaskdefinition):     # hand-coded, checked git: things     return apiresponse(...) 

what right way create_task() call create_task_backend()?

of course if make breaking changes swagger spec have hand-update non-generated code regardless; there many reasons may want re-generate api (say, add/refine mytasktypefromswagger class, or skip checking git generated code @ all) , if have hand-edit generated api code, edits blown away each re-generation.

of course script ~simple grammar in eg. pyparsing; while first time issue, seems it's been solved already!

i tempted use swagger-codegen before , ran same conundrum. fine until update spec. although can use custom templates, seemed lot of overhead , maintenance, when want design first api.

i ended using connexion instead, uses swagger specification automatically handle routing, marshaling, validation, etc. connexion built on flask, not need worry switching frameworks or anything, benefit of portions of application being automatically handled swagger instead of having maintain auto-generated code.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -