Skip to content

Instantly share code, notes, and snippets.

@alexmic
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmic/8997655 to your computer and use it in GitHub Desktop.
Save alexmic/8997655 to your computer and use it in GitHub Desktop.
Sketch for how I'd like a simple REST service framework to work
from swiss import Api
from swiss.resources import Resource, Group
from swiss.auth import BasicAuth, noauth
from swiss.fields import parse, serialize, parselize
from swiss.fields import String, Int, Boolean
# simple dicts for schemas and serialization
schema = {
'name': String,
'surname': String(maxlen=15),
'age': Int
}
@noauth
class Thing(Resource):
def get(self, order_id, id):
pass
class Things(Resource):
# allows ad-hoc parameter parsing
@parse(pay=Boolean(default=False))
@serialize(schema)
def get(self):
print self.params # GET, POST, PUT data
# parse and serialize with the same schema
@parselize(schema)
def post(self):
return []
def put(self):
# if an object can be json.dump'd then no need for a serializer
return {}
# partial parsing
@parse(schema, partial=True)
def patch(self):
pass
def delete(self):
pass
class GroupedThing(Resource):
pass
class GroupedThings(Resource):
@noauth
def get(self):
pass
# groups allow for cleaner route management
group = Group(prefix='/group')
group.route('/things', GroupedThings)
group.route('/thing/1', GroupedThing)
# Api is a WSGI application
api = Api()
# Simple auth (not sure about this yet..)
api.auth(BasicAuth)
api.route('/things/', Things)
api.route('/things/1', Thing)
api.group(group)
# or...
api.routes(
('/things/', Things),
('/things/1', Things)
)
if __name__ == '__main__':
api.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment