-
Notifications
You must be signed in to change notification settings - Fork 3
/
developer_api.py
180 lines (161 loc) · 4.99 KB
/
developer_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from auth import public_api_auth
from bson.objectid import ObjectId
from apps import App
from libraries.python.web_util import encode_json, decode_json
from libraries.python.web_util import parse_int_param, parse_int_param_as_bool
from libraries.python.web_util import error_response
import json
import logging
import pymongo
import web
urls = (
'/apps/?', 'AppsHandler',
'/apps/([^/]+)/?', 'AppHandler',
)
app = web.application(urls, locals())
class AppsHandler(object):
'''
Handler for retrieving information about a developer's registered
apps. Also allows for new apps to be registered.
'''
@public_api_auth
def GET(self, auth_user=None, auth_app_id=None):
'''
Return JSON array of basic app info. Only returns apps
created by the authenticated user.
'''
try:
spec = {
'deleted': False,
'creator': auth_user._id
}
apps = App.collection.find(spec)
formatted_apps = [app.formatted_dict() for app in apps]
return encode_json(formatted_apps)
except Exception, e:
logging.error(e)
return error_response(500)
@public_api_auth
def POST(self, auth_user=None, auth_app_id=None):
'''
Create a new app and return a JSON object containing the new
app information.
'''
params = web.input(data=None)
try:
if params.data:
data = decode_json(params.data)
else:
data = decode_json(web.ctx.data)
App.validate(data)
except Exception, e:
logging.error(e)
return error_response(400, 'Data did not pass validation')
try:
app = App(data)
app.set_creator(auth_user)
app.save()
formatted_dict = app.formatted_dict()
return encode_json(formatted_dict)
except Exception, e:
logging.error(e)
return error_response(500)
class AppHandler(object):
'''
Handler for retrieving information about a specific app. Also
allows app information to be updated, or for an app to be deleted. Only
the creator can access and modify the app information.
'''
@public_api_auth
def GET(self, app_id, auth_user=None, auth_app_id=None):
'''
Return a JSON object containing information about the specific app.
'''
try:
app_id = ObjectId(app_id)
except Exception, e:
logging.error(e)
return error_response(400, 'Not a valid app id')
try:
app = App.collection.find_one({'_id': app_id, 'deleted': False})
if not app:
message = 'App does not exist'
logging.warn(message)
return error_response(404, message)
if not app.user_can_update(auth_user):
message = 'App cannot be accessed by the user'
logging.warn(message)
return error_response(403, message)
formatted_dict = app.formatted_dict()
return encode_json(formatted_dict)
except Exception, e:
logging.error(e)
return error_response(500, 'Server Error')
@public_api_auth
def PUT(self, app_id, auth_user=None, auth_app_id=None):
'''
Update the given app, and returns a JSON object containing the new
timestamp.
'''
try:
app_id = ObjectId(app_id)
except Exception, e:
logging.error(e)
return error_response(400, 'Not a valid app id')
try:
data = decode_json(web.ctx.data)
App.validate(data)
except Exception, e:
return error_response(400, 'Data did not pass validation')
try:
app = App.collection.find_one({'_id': app_id, 'deleted': False})
except Exception, e:
logging.error(e)
return error_response(500, 'Server Error')
if not app:
message = 'App does not exist'
logging.warn(message)
return error_response(404, message)
if not app.user_can_update(auth_user):
message = 'App cannot be accessed by the user'
logging.warn(message)
return error_response(403, message)
try:
app.update(data)
app.save()
update_json = {'updated': app.updated_timestamp()}
return encode_json(update_json)
except Exception, e:
logging.error(e)
return error_response(500)
@public_api_auth
def DELETE(self, app_id, auth_user=None, auth_app_id=None):
'''
Delete the given app, and returns a JSON object containing a
success notification.
'''
try:
app_id = ObjectId(app_id)
except Exception, e:
return error_response(400, 'Not a valid app id')
try:
app = App.collection.find_one({'_id': app_id, 'deleted': False})
except Exception, e:
logging.error(e)
return error_response(500, 'Server Error')
if not app:
message = 'App does not exist'
logging.warn(message)
return error_response(404, message)
if not app.user_can_update(auth_user):
message = 'App cannot be modified by the user'
logging.warn(message)
return error_response(403, message)
try:
app.delete()
app.save()
update_json = {'success': True}
return encode_json(update_json)
except Exception, e:
logging.error(e)
return error_response(500)