Skip to main content

pyramid-swagger-router

view’s code generation from a swagger’s definition file.

motiviation

this package’s motivation is below.

Code generation is better than meta-programming, and onetime scaffold is simply bad.

Code generation is better than meta-programming

Rational:

  • Reading generated code is more easier than reading meta-programming code

  • If you want to stop the code generation, just stop it (stopping using code generation is more easier than stopping meta-programming)

Onetime scaffold is simply bad

Onetime scaffold (like a cookiecutter) is good for first creation. But, after scaffold implementation is changed, the migration about past generated code, handling manually(by yourself). So, this is bad.

It is ok, pyramid-swagger-router command called multiple times(because of merging code via FST(full syntax tree) (thunks to redbaron)).

Where is the Router?

Nothing. This is the code generation tool for routing setting(including view configuration).

How to use

How to use this.

$ pip install pyramid-swagger-router
$ pyramid-swagger-router <swagger.yaml> <dst>

if you passing swagger.yaml such as below.

swagger: '2.0'
info:
  title: Pet Shop Example API
  version: "0.1"
consumes:
  - app.pet.views.ication/json
produces:
  - app.pet.views.ication/json
paths:
  /pets:
    x-pyramid-route-name: pets
    get:
      tags: [Pets]
      operationId: app.pet.views.get_pets
      summary: Get all pets
      parameters:
        - name: animal_type
          in: query
          type: string
          pattern: "^[a-zA-Z0-9]*$"
        - name: limit
          in: query
          type: integer
          minimum: 0
          default: 100
      responses:
        200:
          description: Return pets
          schema:
            type: array
            items:
              $ref: '#/definitions/Pet'
  /pets/{pet_id}:
    x-pyramid-route-name: pet
    get:
      tags: [Pets]
      operationId: app.pet.views.get_pet
      summary: Get a single pet
      parameters:
        - $ref: '#/parameters/pet_id'
      responses:
        200:
          description: Return pet
          schema:
            $ref: '#/definitions/Pet'
        404:
          description: Pet does not exist
    put:
      tags: [Pets]
      operationId: app.pet.views.put_pet
      summary: Create or update a pet
      parameters:
        - $ref: '#/parameters/pet_id'
        - name: pet
          in: body
          schema:
            $ref: '#/definitions/Pet'
      responses:
        200:
          description: Pet updated
        201:
          description: New pet created
    delete:
      tags: [Pets]
      operationId: app.pet.views.delete_pet
      summary: Remove a pet
      parameters:
        - $ref: '#/parameters/pet_id'
      responses:
        204:
          description: Pet was deleted
        404:
          description: Pet does not exist


parameters:
  pet_id:
    name: pet_id
    description: Pet's Unique identifier
    in: path
    type: string
    required: true
    pattern: "^[a-zA-Z0-9-]+$"

definitions:
  Pet:
    type: object
    required:
      - name
      - animal_type
    properties:
      id:
        type: string
        description: Unique identifier
        example: "123"
        readOnly: true
      name:
        type: string
        description: Pet's name
        example: "Susie"
        minLength: 1
        maxLength: 100
      animal_type:
        type: string
        description: Kind of animal
        example: "cat"
        minLength: 1
      tags:
        type: object
        description: Custom tags
      created:
        type: string
        format: date-time
        description: Creation time
        example: "2015-07-07T15:49:51.230+02:00"
        readOnly: true

output code are like these.

app/pet/__init__.py

def includeme_swagger_router(config):
    config.add_route('pets', '/pets')
    config.add_route('pet', '/pets/{pet_id}')
    config.scan('.views')


def includeme(config):
    config.include(includeme_swagger_router)

app/pet/views.py

from pyramid.view import(
    view_config
)


@view_config(renderer='json', request_method='GET', route_name='pets')
def get_pets(context, request):
    """
    Get all pets

    request.GET:

        * 'animal_type'  -  `{"type": "string", "pattern": "^[a-zA-Z0-9]*$"}`
        * 'limit'  -  `{"type": "integer", "minimum": 0, "default": 100}`
    """
    return {}


@view_config(renderer='json', request_method='GET', route_name='pet')
def get_pet(context, request):
    """
    Get a single pet

    request.matchdict:

        * 'pet_id'  Pet's Unique identifier  `{"type": "string", "required": true, "pattern": "^[a-zA-Z0-9-]+$"}`
    """
    return {}


@view_config(renderer='json', request_method='PUT', route_name='pet')
def put_pet(context, request):
    """
    Create or update a pet

    request.matchdict:

        * 'pet_id'  Pet's Unique identifier  `{"type": "string", "required": true, "pattern": "^[a-zA-Z0-9-]+$"}`

    request.json_body:

    ```
        {
          "type": "object",
          "required": [
            "name",
            "animal_type"
          ],
          "properties": {
            "id": {
              "type": "string",
              "description": "Unique identifier",
              "example": "123",
              "readOnly": true
            },
            "name": {
              "type": "string",
              "description": "Pet's name",
              "example": "Susie",
              "minLength": 1,
              "maxLength": 100
            },
            "animal_type": {
              "type": "string",
              "description": "Kind of animal",
              "example": "cat",
              "minLength": 1
            },
            "tags": {
              "type": "object",
              "description": "Custom tags"
            },
            "created": {
              "type": "string",
              "format": "date-time",
              "description": "Creation time",
              "example": "2015-07-07T15:49:51.230+02:00",
              "readOnly": true
            }
          }
        }
    ```
    """
    return {}


@view_config(renderer='json', request_method='DELETE', route_name='pet')
def delete_pet(context, request):
    """
    Remove a pet

    request.matchdict:

        * 'pet_id'  Pet's Unique identifier  `{"type": "string", "required": true, "pattern": "^[a-zA-Z0-9-]+$"}`
    """
    return {}

appendix 1

if you want to set custom route_name, using x-pyramid-route-name.

appendix 2

When desrialization from json request, swagger-marshmallow-codegen is helpful, maybe.

0.1.3

  • fix paths level’s paramaters are not existed in docstring

0.1.2

  • fix merge detection bug

  • fix error is occured when including parameters in methods section

Metadata

Release files for pyramid-swagger-router 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyramid-swagger-router 0.1.3
File Size Uploaded Provenance Actions
pyramid-swagger-router-0.1.3.tar.gz 11.2 kB Without provenance Details for pyramid-swagger-router-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyramid-swagger-router 0.1.3
File Interpreter ABI Platform Actions
pyramid_swagger_router-0.1.3-py2.py3-none-any.whl Python 3, Python 2 none any Details for pyramid_swagger_router-0.1.3-py2.py3-none-any.whl

Total release size: 25.1 kB

Release files / pyramid-swagger-router-0.1.3.tar.gz

Download URL pyramid-swagger-router-0.1.3.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2bd01b38de43469a3ff7171cf3469111f24bc08f8e8660e094f5fb8776fc556b
BLAKE2b-256 checksum
How to use checksums
ab3b7dc8e31e74bca06b4e21f1f8f43617d8ec9a253bc7fee5ae85241fedf1e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyramid_swagger_router-0.1.3-py2.py3-none-any.whl

Download URL pyramid_swagger_router-0.1.3-py2.py3-none-any.whl
Size 13.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7feea9d6f7e5a227c9a4b7f6a36485be7df990b4c3d4989ea5ff3a4e82351a30
BLAKE2b-256 checksum
How to use checksums
e7faa1142539226195c0a89e4b3fbfb99e1bc91f5c45fc62f5a7a7c407aba98f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pyramid-swagger-router 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page