Created
January 3, 2017 11:18
-
-
Save podhmo/2750312d6a6324add580831594d74d8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import os.path | |
from pyramid.config import Configurator | |
from pyramid.renderers import JSON | |
import datetime | |
def make_app(settings): | |
config = Configurator(settings=settings) | |
config.include("app.routes") | |
# override: json renderer | |
json_renderer = JSON() | |
def datetime_adapter(obj, request): | |
return obj.isoformat() | |
json_renderer.add_adapter(datetime.datetime, datetime_adapter) | |
config.add_renderer('json', json_renderer) | |
return config.make_wsgi_app() | |
def main(): | |
from wsgiref.simple_server import make_server | |
here = os.path.dirname(os.path.abspath(__file__)) | |
settings = { | |
"here": here, | |
"pyramid.reload_all": True, | |
} | |
app = make_app(settings) | |
server = make_server('0.0.0.0', 8080, app) | |
logging.basicConfig(level=logging.DEBUG) # xxx | |
server.serve_forever() | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run: | |
PYTHONPATH=. python app/__init__.py | |
views: | |
pyramid-swagger-router swagger.yaml . | |
fetch: | |
wget https://raw.githubusercontent.com/hjacobs/connexion-example/master/swagger.yaml | |
gsed -i 's@app.@app.views.@' swagger.yaml | |
fetch_test: | |
wget https://raw.githubusercontent.com/hjacobs/connexion-example/master/test.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── Makefile | |
├── app | |
│ ├── __init__.py | |
│ ├── __pycache__ | |
│ │ ├── __init__.cpython-35.pyc | |
│ │ ├── routes.cpython-35.pyc | |
│ │ └── views.cpython-35.pyc | |
│ ├── routes.py | |
│ └── views.py | |
├── structure.txt | |
├── swagger.yaml | |
└── test.sh | |
2 directories, 10 files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
swagger: '2.0' | |
info: | |
title: Pet Shop Example API | |
version: "0.1" | |
consumes: | |
- app.views.ication/json | |
produces: | |
- app.views.ication/json | |
security: | |
# enable OAuth protection for all REST endpoints | |
# (only active if the TOKENINFO_URL environment variable is set) | |
- oauth2: [uid] | |
paths: | |
/pets: | |
x-pyramid-route-name: pets | |
get: | |
tags: [Pets] | |
operationId: app.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.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.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.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 | |
securityDefinitions: | |
oauth2: | |
type: oauth2 | |
flow: implicit | |
authorizationUrl: https://example.com/oauth2/dialog | |
scopes: | |
uid: Unique identifier of the user accessing the service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
HTTP=$(which http) | |
if [ ! -x "$HTTP" ]; then | |
echo 'You need HTTPie to run this script!' | |
echo 'sudo pip3 install httpie' | |
exit 1 | |
fi | |
URL=:8080 | |
set -x | |
http PUT $URL/pets/1 name=foo animal_type=test | |
http $URL/pets/1 | |
http PUT $URL/pets/1 name=foo animal_type=test tags:='{"color": "brown"}' | |
http $URL/pets/1 | |
http $URL/pets animal_type==test | |
http DELETE $URL/pets/1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import datetime | |
from pyramid import httpexceptions | |
from pyramid.view import( | |
view_config | |
) | |
logger = logging.getLogger(__name__) | |
# our memory-only pet storage | |
PETS = {} | |
@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}` | |
""" | |
animal_type = request.GET.get("animal_type") | |
limit = request.GET.get("limit") or 100 | |
return [pet for pet in PETS.values() if not animal_type or pet['animal_type'] == animal_type][:limit] | |
@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-]+$"}` | |
""" | |
pet_id = request.matchdict["pet_id"] | |
if pet_id not in PETS: | |
raise httpexceptions.HTTPNotFound() | |
return PETS[pet_id] | |
@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 | |
} | |
} | |
} | |
``` | |
""" | |
pet_id = request.matchdict["pet_id"] | |
pet = request.json_body | |
exists = pet_id in PETS | |
pet['id'] = pet_id | |
if exists: | |
logger.info('Updating pet %s..', pet_id) | |
PETS[pet_id].update(pet) | |
return httpexceptions.HTTPOk() | |
else: | |
logger.info('Creating pet %s..', pet_id) | |
pet['created'] = datetime.datetime.utcnow() | |
PETS[pet_id] = pet | |
return httpexceptions.HTTPCreated() | |
@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-]+$"}` | |
""" | |
pet_id = request.matchdict["pet_id"] | |
if pet_id in PETS: | |
logger.info('Deleting pet %s..', pet_id) | |
del PETS[pet_id] | |
raise httpexceptions.HTTPNoContent() | |
else: | |
raise httpexceptions.HTTPNotFound() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment