PWZER / swagger-ui-py

Swagger UI for Python web framework, such Tornado, Flask and Sanic. https://pwzer.github.io/swagger-ui-py/
https://pypi.org/project/swagger-ui-py
Apache License 2.0
68 stars 31 forks source link

Add Swagger UI for bottle #11

Closed carstencodes closed 4 years ago

carstencodes commented 4 years ago

Hey there,

I just got in touch with this cool package. I'm currently working on a small project with the bottle-py API.

It was little work to create an implementation for bottle using a simple specialization of the Interface class:

class BottleInterface(Interface):

    def __init__(self, *args, **kwargs):
        kwargs['app_type'] = 'bottle'
        super(BottleInterface, self).__init__(*args, **kwargs)

    def _bottle_handler(self):
        app = self._app

        @app.get("/")
        def index():
            return self.doc_html

        @app.get('/<filepath:re:.*\.(js|css)>')
        def java_script_file(filepath):
            return static_file(filepath, root=self.static_dir)

        @app.get("/swagger.json")
        def config_handler():
            from bottle import redirect
            # If bottle runs with the python 3.6 built-in wsgi-server, the sub-request will cause the wsgi server to fail the request, so redirecting will work. The host most be set in the config.
            redirect(self._config_url)

        if self._editor:
            @app.get("/editor")
            def editor():
                return self.editor_html

    def _auto_match_handler(self):
       try:
            from bottle import Bottle
            if isinstance(self._app, Bottle):
                  return self._bottle_handler()
        except:
             pass

        return super(BottleInterface, self)._auto_match_handler()

Is there a chance, that this might find a way to the next release?

PWZER commented 4 years ago

Can you submit a PR?

carstencodes commented 4 years ago

Sure. But It might take some time. It should be there by the weekend

PWZER commented 4 years ago

@carstencodes Added v0.3.0

carstencodes commented 4 years ago

@PWZER Thanks a lot.

I'm sorry to say, but something, somehow went terribly wrong.

Looking at the current version of the index page, the index function has an empty decorator. This will cause bottle to ignore this function.

I fixed it locally and ran into another error. The config url function misses the redirect function.

Both lines of code were correct in the pull request !12.

Do you understand, how this happened?

carstencodes commented 4 years ago

@PWZER ok, the issue is in my code.

I'm using marshmallow and apispec to create a json file, which is already mapped to another url. When I additionally dump the json file to file on the harddisk drive, it works.

PWZER commented 4 years ago

@carstencodes Here are the two ways, config_path and config_url. Do you have any other questions?

carstencodes commented 4 years ago

No, I'm using the config_path right now. Works fine. Thankx