Skip to content

Commit

Permalink
Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
handstandsam committed Apr 30, 2012
2 parents f3ab046 + 22790bb commit 44a674d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,7 @@
<<<<<<< HEAD


.project .project
=======
*.sw[op]
*.pyc
>>>>>>> 22790bbe987567fb0d0b9b40607a9193c1b35a29
74 changes: 74 additions & 0 deletions api/app.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python
import web
import json
import datetime
import requests

urls = (
'/.*', 'everything'
)
app = web.application(urls, globals())

#web.config.debug = False

GET_KEYS = set(['locationId', 'apikey', 'count'])
MIN_KEYS = set(['locationId', 'apikey', 'userId'])
MAX_KEYS = set([
"locationId",
"appId",
"appUserId",
"time1",
"time2",
"time3",
"timediff21",
"timediff31",
"timediff32",
"appspecific",
])

class everything:
def POST(self):
'Query string like ?doc={saoehaostnoeau:asoetusoaestn,soe:soe} '
try:
doc = json.loads(web.data())

# Test this
# doc['locationId']

# Minimum keys
if not MIN_KEYS.issubset(doc.keys()):
raise ValueError('locationId, apikey and userId are required.')
elif MAX_KEYS.issuperset(doc.keys()):
raise ValueError('one of the keys you entered isn\'t allowed')

# Validate times, but not now
doc["timeReceived"] = datetime.datetime.now().isoformat()

# Hack
doc['appId'] = doc['apikey']
del(doc['apikey'])

docs = json.dumps({"docs":[doc]})
dbresponse = requests.post(
'http://waittimes.iriscouch.com/waittimes/_bulk_docs',
docs,
headers = {
'Content-Type': "application/json; charset=utf-8"
}
)
return dbresponse.text[1:-1] #Remove brackets from JSON list
except Exception, msg:
return json.dumps({"ok": False, "message": unicode(msg)})

def GET(self):
try:
apprequest = json.loads(web.data())
if set(apprequest.keys()) != set(GET_KEYS):
raise ValueError('You need to send %s' % ', '.join(list(GET_KEYS)))
except Exception, msg:
return json.dumps({"ok": False, "message": unicode(msg)})

PUT = POST

if __name__ == "__main__":
app.run()

0 comments on commit 44a674d

Please sign in to comment.