rnduldulaojr / tornado-swirl

Tornado Swagger Doc Generator
MIT License
25 stars 7 forks source link

How to add custom headers to the SwaggerUI handlers #17

Closed jfgaudreault-p closed 5 years ago

jfgaudreault-p commented 5 years ago

Is there an easy way to add custom headers to the SwaggerUI and SwaggerAPI internal handlers? For example to override or set the Cache-Control headers.

rduldulao commented 5 years ago

Hi @freydrich , sorry for the late reply. To answer your question, not at the moment. If you could describe your requirements though, I could probably squeeze it in for the next release. Thanks.

jfgaudreault-p commented 5 years ago

Thansk a lot. The requirement at high level is simple, I would like to be able to easily change (add/modify) the http response headers of any of the swagger handlers which are handled internally right now.

My main requirement is to be able to add Cache-Control header to those handlers.

Maybe if we could pass somewhere an array of headers (a key/value dictionary) on initialization that would be sufficient for my use case. I am not too sure where would be that initialization happening, right now I am only using the describe(...) method to initiliaze some values on startup. Maybe the settings.py would be the right place to set these, but I haven't used those settings so far since I didn't see how they could be used in the examples.

rduldulao commented 5 years ago

Hi @freydrich

Best I can do at this time:

swirl.describe(title="My REST API", description="Example API that does wonders",
               swagger_ui_handlers_header=[
                   ('Cache-Control', 'public'),
                   ('Cache-Control', 'max-age=300')
               ],
               swagger_spec_headers=[
                   ('Cache-Control', 'no-cache')
               ],)

I made the header info a list of tuples since some headers can be specified multiple times (can't do if it were a dict).

Please see recent release 0.1.15. Thanks.

jfgaudreault-p commented 5 years ago

Thanks, this should work perfectly, I'll try that.