Created
May 21, 2013 17:28
-
-
Save jagt/5621599 to your computer and use it in GitHub Desktop.
simple mocking rest api with file based storage, using bottle
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
# simple mocking rest api with file based storage | |
import json | |
from datetime import datetime, timedelta | |
from time import mktime | |
from bottle import * | |
tabs = ('users', 'biddings', 'auctions') | |
for tab in tabs: | |
globals()[tab] = [] | |
def tstamp(dt): | |
return int(time.mktime(dt.timetuple())) | |
def saveall(): | |
for tab in tabs: | |
with open(tab + '.json', 'w') as f: | |
json.dump(globals()[tab], f) | |
def loadall(): | |
for tab in tabs: | |
try: | |
with open(tab + '.json', 'r') as f: | |
globals()[tab] = json.load(f) | |
except IOError: | |
globals()[tab] = [] | |
OK = {'status' : 'ok'} | |
err = lambda x : {'status' : 'error', 'error' : x} | |
# users | |
def getuser(username): | |
result = [user for user in users if user['username'] == username] | |
return result[0] if result else None | |
@route('/users', method=['GET', 'POST']) | |
def _users_handler(): | |
if request.method == 'GET': | |
return json.dumps(users) | |
if request.method == 'POST': | |
username = request.forms.get('username') | |
user = getuser(username) | |
if user or (not username): | |
return err('user already exists') | |
else: | |
users.append({ | |
'username' : username | |
}) | |
saveall() | |
return OK | |
@route('/users/<username>', method=['GET', 'PUT']) | |
def _user_handler(username): | |
if request.method == 'GET': | |
return result[0] if result else err('user not found') | |
elif request.method == 'PUT': | |
# TODO nothing to edit yet | |
saveall() | |
return OK | |
# auctions | |
@route('/auctions', method=['GET', 'POST']) | |
def _auctions_handler(): | |
if request.method == 'GET': | |
return json.dumps(auctions) | |
elif request.method == 'POST': | |
auc = {'aucid' : len(auctions)} | |
auc.update(request.forms) | |
if set(auc.keys()) != set(auctions[0].keys()): | |
return err('malform auction form') | |
else: | |
auctions.append(auc) | |
saveall() | |
return OK | |
@route('/auction/<aucid>', method=['GET', 'PUT']) | |
def _auction_handler(aucid): | |
aucid = int(aucid) | |
if aucid >= len(auctions): | |
return err('invalid auc') | |
if request.method == 'GET': | |
return json.dumps(auctions[aucid]) | |
elif request.method == 'PUT': | |
auc = auctions[aucid] | |
if not set(auc.keys()).issuperset(set(request.forms.keys())): | |
return err('invalid updating keys') | |
auc.update(request.forms) | |
saveall() | |
return OK | |
if __name__ == '__main__': | |
loadall() | |
if not any((users, biddings, auctions)): | |
print 'load fixture' | |
now = datetime.now() | |
users.extend([{ | |
'username' : 'adam' | |
}, { | |
'username' : 'eva' | |
}]) | |
auctions.extend([{ | |
'aucid' : '0', | |
'owner' : 'adam', | |
'name' : 'boat', | |
'desc' : "it's a boat", | |
'startdate' : tstamp(now), | |
'enddate' : tstamp(now + timedelta(days=10)), | |
'reserve' : 20000 | |
}]) | |
biddings.extend([{ | |
'bidid' : '0', | |
'auction' : '0', | |
'bidder' : 'adam', | |
'bidprice' : 19999, | |
'time' : tstamp(now + timedelta(days=5)) | |
}, { | |
'bidid' : '1', | |
'auction' : '0', | |
'bidder' : 'eva', | |
'bidprice' : 25000, | |
'time' : tstamp(now + timedelta(days=6)) | |
}]) | |
saveall() | |
run(host='localhost', port='80') |
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
1. do not consider IE7 support. IE7 has sucky js support, even using Extjs it is likely to break on random js code. | |
2. consider pagenation later. maybe don't even need it. | |
API draft: | |
1. No items. Use auction only. If an old auction need to be re submited, create a new auction with the same data. | |
!GET users/ - list all users | |
!POST users - create new user | |
!GET users/:username - user info, settings | |
!PUT users/:username - edit user info | |
!DELETE users/:username - delete user | |
GET users/:username/biddings - user's all biddings | |
GET users/:username/auctions - user's all auctions | |
!GET auctions/ - list all auctions | |
!POST auctions/ - create a new auction | |
!GET auctions/:aucid - list auction info | |
PUT auctions/:aucid - update auction info | |
DELTE auctions/:aucid - delete auction info | |
GET auctions/:aucid/biddings - list auction's all biddings | |
GET biddings/ - list all biddings | |
GET biddings/:bidid - list bid info | |
PUT biddings/:bidid - update bid info | |
DELETE biddings/:bidid - delete bid info | |
POST biddings/ - create a new bidding | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment