-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.py
132 lines (102 loc) · 4.29 KB
/
rpc.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
# coding: utf-8
import appengine_config
import datetime
import json
import logging
import webapp2
import GlobalUtilities as tools
class RPCHandler(webapp2.RequestHandler):
def __init__(self, request, response):
self.initialize(request, response)
self.methods = RPCMethods()
def get(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
authenticated = tools.RPCcheckAuthentication(self, True)
# authenticated = True
func = None
action = self.request.get('action')
if action:
logging.info("Action: " + str(action))
if action[0] == '_':
self.error(403) # access denied
return
elif action[0:4] == 'pub_':
func = getattr(self.methods, action, None)
elif action[0:5] == "semi_":
if authenticated == "semi" or authenticated == True:
func = getattr(self.methods, action, None)
else:
self.error(401)
self.response.out.write("I'm not authorized to give you that information until you log in. Sorry!")
else:
if authenticated == True:
func = getattr(self.methods, action, None)
else:
self.error(401)
self.response.out.write("I'm not authorized to give you that information until you log in. Sorry!")
if not func:
self.error(404) # method not found
return
args = ()
while True:
key = 'arg%d' % len(args)
val = self.request.get(key)
if val:
args += (json.loads(val),)
else:
break
result = func(*args)
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
self.response.out.write(json.dumps(result, default=dthandler))
def post(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
authenticated = tools.RPCcheckAuthentication(self, True)
args = json.loads(self.request.body)
logging.info(args)
func, args = args[0], args[1:]
if func[0] == '_':
self.error(403) # access denied
return
elif func[0:4] == "pub_":
func = getattr(self.methods, func, None)
elif func[0:5] == "semi_":
if authenticated == "semi" or authenticated == True:
func = getattr(self.methods, func, None)
else:
self.error(401)
self.response.out.write("I'm not authorized to give you that information until you log in. Sorry!")
else:
if authenticated == True:
func = getattr(self.methods, func, None)
else:
self.error(401)
self.response.out.write("I'm not authorized to give you that information until you log in. Sorry!")
if not func:
self.error(404) # file not found
return
result = func(*args)
self.response.out.write(json.dumps(result))
## -- Set of functions that returns data to above GET request -- ##
class RPCMethods:
# Every function here gives a success/fail and a response.
# Even though there's no response needed, we have to return
# something so it integrates with other functions
# that do have a value to return. The response in that case is None
#### ---- Globalhopeindia.org Utility Functions ---- ####
def pub_allTeams(self, settings):
# This returns a json list of teams
s = tools.getKey(settings).get()
return [[t.name, t.key.urlsafe()] for t in s.data.display_teams]
def pub_individualInfo(self, team_key, individual_key):
i = tools.getKey(individual_key).get()
t_key = tools.getKey(team_key)
return i.data.info(t_key)
def pub_teamInfo(self, team_key):
# This returns a simplejson list of team members' names, hosted link to picture, and description
# Response parsed by Javascript on main GHI site
t = tools.getKey(team_key).get()
return t.data.public_members_list
app = webapp2.WSGIApplication([
('/rpc', RPCHandler),
], debug=True)
app = appengine_config.recording_add_wsgi_middleware(app)