def test_simple_parse_with_urlencoded_request_body(self): self.reset_settings() @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. Request Body: foo (string) -- Required. Image file. name (string) -- Required. Name. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['post'] assert obj['paths']['/test']['post']['requestBody'] assert obj['paths']['/test']['post']['requestBody']['content'] assert obj['paths']['/test']['post']['requestBody']['content'][ "application/x-www-form-urlencoded"]
def test_simple_2(self): self.reset_settings() @swirl.restapi(r'/test/(?P<a>\w+)/(?P<b>\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """ self.finish() self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404']
def test_deprecated2(self): self.reset_settings() @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. [DEPRECATED] Request Body: user (User) -- sample user. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['post'] assert obj['paths']['/test']['post']['deprecated']
def test_simple_1(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. Response: out (string) -- An output. """ self.finish() self._app.add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] obj = obj['paths']['/test']['get'] assert obj['responses'] assert obj['responses']['200'] assert obj['responses']['200']['description'] == 'An output.' assert obj['responses']['200']['content']['text/plain']['schema'] assert obj['responses']['200']['content']['text/plain']['schema'][ 'type'] == 'string'
def test_request_body_file_data(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """This is a simple test post with form data. This is a simple description. Request Body: file (file:text/csv) -- The file. Returns: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """ self.finish() self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) print(obj) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content'][ 'text/csv']
def make_app(self): db = self.database.get_motor_connection() return Application(api_routes(), db=db, cookie_secret=settings.CookieSecret, debug=settings.Debug, key_version=settings.KeyVersion, version=settings.Version, login_url='api/login')
def test_simple_1(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. Response: test (B) -- An output. """ self.finish() @swirl.schema class A(object): """Parent class Properties: name (string) -- Test name. """ @swirl.schema class B(A): """Sub class Properties: subname (string) -- Test sub name """ self._app.add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] handler = obj['paths']['/test']['get'] assert handler['responses'] assert handler['responses']['200'] assert handler['responses']['200']['description'] == 'An output.' assert handler['responses']['200']['content']['application/json'][ 'schema'] assert handler['responses']['200']['content']['application/json'][ 'schema']['$ref'] schemas = obj['components']['schemas'] assert schemas assert schemas['A'] assert schemas['B'] assert schemas['B']['allOf'] assert schemas['B']['allOf'][0] assert schemas['B']['allOf'][1]
def test_simple_3(self): self.reset_settings() @swirl.restapi(r'/test/(?P<a>\w+)/(?P<b>\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (Model) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """ self.finish() @swirl.schema class Model(object): """This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] assert obj['components'] assert obj['components']['schemas'] assert obj['components']['schemas']['Model'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404']
def test_request_body_model(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """This is a simple test post with form data. This is a simple description. Request Body: user (Model) -- Model model. Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """ self.finish() @swirl.schema class Model(object): """This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content'][ 'application/json']
def test_security_scheme2(self): self.reset_settings() swirl.describe(title='My API', description='My description') swirl.add_security_scheme("test_basic", security.HTTP("bearer", bearerFormat="JWT")) @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. Security: test_basic Request Body: user (User) -- sample user. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['post'] assert obj['paths']['/test']['post']['security'] assert obj['paths']['/test']['post']['security'][0] assert 'test_basic' in obj['paths']['/test']['post']['security'][0] assert obj['components'] assert obj['components']['securitySchemes'] assert obj['components']['securitySchemes']['test_basic'] assert obj['components']['securitySchemes']['test_basic'][ 'type'] == 'http' assert obj['components']['securitySchemes']['test_basic'][ 'scheme'] == 'bearer' assert obj['components']['securitySchemes']['test_basic'][ 'bearerFormat'] == 'JWT'
def test_describe_1(self): self.reset_settings() swirl.describe(title='title', description='description', servers=[{ 'url': 'http://test/', 'description': 'test', 'foo': 'foo' }]) @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. Request Body: file (file:image/png) -- Required. Image file. name (string) -- Required. Name. """ pass self._app.add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj.get("openapi", None) == "3.0.0" assert obj["info"] assert obj["info"]["title"] assert obj["info"]["title"] == "title" assert obj["info"]["description"] assert obj["info"]["description"] == "description" assert obj["servers"] assert len(obj["servers"]) == 1 assert obj["servers"][0] assert obj["servers"][0]["url"] assert obj["servers"][0]["url"] == "http://test/" assert obj["servers"][0].get("foo") is None
def test_enabled_methods(self): self.reset_settings() swirl.describe(title='My API', description='My description', enabled_methods=['head']) @swirl.restapi("/test") class Handler(RequestHandler): def head(self): """This is a head method. Test Head only method Response: out (string) -- Hello World string """ pass def post(self): """This is the simple description. With a second line. Long description. With a second line. [DEPRECATED] Request Body: user (User) -- sample user. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['head'] assert obj['paths']['/test'].get('post') == None
def test_schema_spec_descriptions(self): self.reset_settings() @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. Request Body: user (User) -- sample user. """ pass @swirl.schema class User: """Sample Schema Properties: name (string) -- This should be the description. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['components'] assert obj['components']['schemas'] assert obj['components']['schemas']['User'] assert obj['components']['schemas']['User']['properties'] assert obj['components']['schemas']['User']['properties']['name'] assert obj['components']['schemas']['User']['properties']['name'][ 'description'] assert obj['components']['schemas']['User']['properties']['name'][ 'description'] == 'This should be the description.'
def test_spec_html(self): @swirl.restapi("/test") class Handler(RequestHandler): def post(self): """This is the simple description. With a second line. Long description. With a second line. Request Body: foo (string) -- Required. Image file. name (string) -- Required. Name. """ pass self.get_app().add_handlers(r".*", api_routes()) response = yield self.http_client.fetch( self.get_url('/swagger/spec.html')) assert response.code == 200
def make_app(): logging.info("Server start http://%s:%i", config.app_host, config.app_port) logging.info("DOC start http://%s:%i/swagger/spec.html", config.app_host, config.app_port) settings = dict( cookie_secret=config.app_key, debug=True, description=config.app_title, ) # 라우트 설정 routes = swirl.api_routes() + [ (r"/(.*)", RedirectHandler, { 'url': '/api' }), ] return swirl.Application( routes, db=SQLAlchemy( f"{config.db_type}://{config.db_user}:{config.db_pass}@{config.db_host}/{config.db_name}" ), **settings)
def make_app(): return Application(api_routes(), autoreload=True)