项目作者: alysivji

项目描述 :
apispec plugin that generates OpenAPI specification (aka Swagger Docs) for Falcon web applications.
高级语言: Python
项目地址: git://github.com/alysivji/falcon-apispec.git
创建时间: 2018-07-26T19:32:11Z
项目社区:https://github.com/alysivji/falcon-apispec

开源协议:MIT License

下载


falcon-apispec

Build Status codecov PyPI License: MIT

apispec plugin that generates OpenAPI specification (aka Swagger) for Falcon web applications.

Apispec uses three sources of information. Basic information is directly given to APISpec(). The plugin reads information about paths from the Falcon app. Information about an object could be given by marshmallow specification

Installation

  1. pip install falcon-apispec

Optionaly:

  1. pip install marshmallow

Works with apispec v1.0+.

Example Application

  1. from apispec import APISpec
  2. from apispec.ext.marshmallow import MarshmallowPlugin
  3. import falcon
  4. from falcon_apispec import FalconPlugin
  5. from marshmallow import Schema, fields
  6. # Optional marshmallow support
  7. class CategorySchema(Schema):
  8. id = fields.Int()
  9. name = fields.Str(required=True)
  10. class PetSchema(Schema):
  11. category = fields.Nested(CategorySchema, many=True)
  12. name = fields.Str()
  13. # Create Falcon web app
  14. app = falcon.API()
  15. class RandomPetResource:
  16. def on_get(self, req, resp):
  17. """A cute furry animal endpoint.
  18. ---
  19. description: Get a random pet
  20. responses:
  21. 200:
  22. description: A pet to be returned
  23. schema: PetSchema
  24. """
  25. pet = get_random_pet() # returns JSON
  26. resp.media = pet
  27. # create instance of resource
  28. random_pet_resource = RandomPetResource()
  29. # pass into `add_route` for Falcon
  30. app.add_route("/random", random_pet_resource)
  31. # Create an APISpec
  32. spec = APISpec(
  33. title='Swagger Petstore',
  34. version='1.0.0',
  35. openapi_version='2.0',
  36. plugins=[
  37. FalconPlugin(app),
  38. MarshmallowPlugin(),
  39. ],
  40. )
  41. # Register entities and paths
  42. spec.components.schema('Category', schema=CategorySchema)
  43. spec.components.schema('Pet', schema=PetSchema)
  44. # pass created resource into `path` for APISpec
  45. spec.path(resource=random_pet_resource)

Generated OpenAPI Spec

  1. spec.to_dict()
  2. # {
  3. # "info": {
  4. # "title": "Swagger Petstore",
  5. # "version": "1.0.0"
  6. # },
  7. # "swagger": "2.0",
  8. # "paths": {
  9. # "/random": {
  10. # "get": {
  11. # "description": "A cute furry animal endpoint.",
  12. # "responses": {
  13. # "200": {
  14. # "schema": {
  15. # "$ref": "#/definitions/Pet"
  16. # },
  17. # "description": "A pet to be returned"
  18. # }
  19. # },
  20. # }
  21. # }
  22. # },
  23. # "definitions": {
  24. # "Pet": {
  25. # "properties": {
  26. # "category": {
  27. # "type": "array",
  28. # "items": {
  29. # "$ref": "#/definitions/Category"
  30. # }
  31. # },
  32. # "name": {
  33. # "type": "string"
  34. # }
  35. # }
  36. # },
  37. # "Category": {
  38. # "required": [
  39. # "name"
  40. # ],
  41. # "properties": {
  42. # "name": {
  43. # "type": "string"
  44. # },
  45. # "id": {
  46. # "type": "integer",
  47. # "format": "int32"
  48. # }
  49. # }
  50. # }
  51. # },
  52. # }
  53. spec.to_yaml()
  54. # definitions:
  55. # Pet:
  56. # enum: [name, photoUrls]
  57. # properties:
  58. # id: {format: int64, type: integer}
  59. # name: {example: doggie, type: string}
  60. # info: {description: 'This is a sample Petstore server. You can find out more ', title: Swagger Petstore, version: 1.0.0}
  61. # parameters: {}
  62. # paths: {}
  63. # security:
  64. # - apiKey: []
  65. # swagger: '2.0'
  66. # tags: []

Contributing

Setting Up for Local Development

  1. Fork falcon-apispec on Github
  2. Install development requirements. Virtual environments are highly recommended
  1. pip install -r requirements.txt

Running Tests

  1. pytest