def delegate(request): # the json body params = json.loads(request.body) # get api method method = params['method'] del params['method'] # regex pattern for the method formatting = r'^(?:([a-z][a-z0-9]*)\.)*([a-z][a-z0-9]*)$' # the method applied to the regex pattern match = re.search(formatting, method) # if the method is poorly formatted throw an error if not match: return response.throw(0) # parse the different parts of the method (separated by a '.') groups = [group for group in match.groups() if group] # get for the api method api = MethodsClass for part in groups: if not hasattr(api, part): # the api method does not exist return response.throw(1) api = getattr(api, part) # run the method output = api(params) # check for a good response if 'stat' in output and output['stat'] == 'fail': # return failure return output # return a success return response.reply(output)
def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP, additionalPayload={}): ### Load the dictionary and the method in that dictionary from the Permissions Map ### Webapp2Instance.response.headers['Content-Type'] = "application/javascript" # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType( method, dictionary(), dictionary ) # bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get': # 'get' dictionary is exclusively used for GET requests result = response.throw(103) else: from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance result = func(payload) if result == False: return result = response.reply() if result == None else result else: if DictionaryName == 'get': # if there is a callback function specified, use it result = func(Webapp2Instance) if result == False: return result = response.reply() if result == None else result result = dict(result.items() + additionalPayload.items()) if Webapp2Instance.request.get( 'callback' ) != None and Webapp2Instance.request.get( 'callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get( 'callback'), response.compile(result)) else: data = response.compile(result) Webapp2Instance.response.out.write(data) return result = response.throw(102) else: result = response.throw(100, (DictionaryName, MethodName)) else: result = response.throw(101, DictionaryName) result = dict(additionalPayload.items() + result.items()) Webapp2Instance.response.out.write(response.compile(result))
def comment(self, payload): try: identifier = int(payload['post']) except: return response.throw(201) from ..blog import posts post = posts.get(identifier) if post == None: return response.throw(200, data_struct=identifier) post.comment(payload['content'], handle=None if not 'handle' in payload else payload['handle'])
def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP): # set the response as a JSON <or javascript> Webapp2Instance.response.headers['Content-Type'] = "application/json" ### Load the dictionary and the method in that dictionary from the Permissions Map ### # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType( method, dictionary(), dictionary ) # bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get': # 'get' dictionary is exclusively used for GET requests Webapp2Instance.response.out.write( response.throw(17, compiled=True)) return from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance Webapp2Instance.response.out.write( response.compile(func(payload))) return if DictionaryName == 'get': # if there is a callback function specified, use it if Webapp2Instance.request.get( 'callback') != None and Webapp2Instance.request.get( 'callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get( 'callback'), response.compile(func())) else: data = response.compile(func()) Webapp2Instance.response.out.write(data) return Webapp2Instance.response.out.write( response.throw(16, compiled=True)) return else: Webapp2Instance.response.out.write( response.throw(14, (DictionaryName, MethodName), compiled=True)) return else: Webapp2Instance.response.out.write( response.throw(15, DictionaryName, compiled=True)) return
def changepassword(self, payload): user = payload['__Webapp2Instance__'].user worked = user.changePassword(payload['old_password'], payload['password']) if not worked: return response.throw(201) user.unlock() return response.reply({'passlocked':False})
def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP, additionalPayload={}): ### Load the dictionary and the method in that dictionary from the Permissions Map ### Webapp2Instance.response.headers['Content-Type'] = "application/javascript" # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType(method, dictionary(), dictionary)# bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get':# 'get' dictionary is exclusively used for GET requests result = response.throw(103) else: from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance result = func(payload) if result == False: return result = response.reply() if result == None else result else: if DictionaryName == 'get': # if there is a callback function specified, use it result = func(Webapp2Instance) if result == False: return result = response.reply() if result == None else result result = dict(result.items() + additionalPayload.items()) if Webapp2Instance.request.get('callback') != None and Webapp2Instance.request.get('callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get('callback'), response.compile(result)) else: data = response.compile(result) Webapp2Instance.response.out.write(data) return result = response.throw(102) else: result = response.throw(100, (DictionaryName, MethodName)) else: result = response.throw(101, DictionaryName) result = dict(additionalPayload.items() + result.items()) Webapp2Instance.response.out.write(response.compile(result))
def login(self, payload): from ..sealeduser.model import SealedUser user = SealedUser.login( payload['email'], payload['password'], timezone = None if not 'timezone' in payload else payload['timezone'] ) if user == SealedUser.USER_DOESNT_EXIST: return response.throw(203) elif user == SealedUser.INCORRECT_LOGIN: return response.throw(201) elif user == SealedUser.BRUTE_SUSPECTED: return response.throw(202) else: return response.reply({ 'setsession': True, 'session': user.session.toDict(), 'user': user.toDict() })
def signup(self, payload): from ..sealeduser.model import SealedUser user = SealedUser.create(payload['email'], payload['password']); if user == SealedUser.EMAIL_IS_USED: return response.throw(200) return response.reply({ 'setsession': True, 'session': user.session.toDict(), 'user': user.toDict() })
def wrapper(self, payload): # enforce required arguments payload['self'] = self for arg in inspect.getargspec(function).args: if not arg in payload: return response.throw(2, arg) # remove extra, provided arguments for arg in payload.copy(): if not arg in inspect.getargspec(function).args: del payload[arg] # call the function return function(**payload)
def unlock(self, payload): user = payload['__Webapp2Instance__'].user status = user.unlockSession( payload['email'], payload['password'] ) if status == user.INCORRECT_LOGIN: return response.throw(201) else: return response.reply({ 'setsession': True, 'session': user.session.toDict() })
def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP): # set the response as a JSON <or javascript> Webapp2Instance.response.headers['Content-Type'] = "application/json" ### Load the dictionary and the method in that dictionary from the Permissions Map ### # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType(method, dictionary(), dictionary)# bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get':# 'get' dictionary is exclusively used for GET requests Webapp2Instance.response.out.write(response.throw(17, compiled=True)) return from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance Webapp2Instance.response.out.write(response.compile(func(payload))) return if DictionaryName == 'get': # if there is a callback function specified, use it if Webapp2Instance.request.get('callback') != None and Webapp2Instance.request.get('callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get('callback'), response.compile(func())) else: data = response.compile(func()) Webapp2Instance.response.out.write(data) return Webapp2Instance.response.out.write(response.throw(16, compiled=True)) return else: Webapp2Instance.response.out.write(response.throw(14, (DictionaryName, MethodName), compiled=True)) return else: Webapp2Instance.response.out.write(response.throw(15, DictionaryName, compiled=True)) return
def disconnect(self, payload): if not 'identifier' in payload or not 'device' in payload: return response.throw(24) return System.User.Host.disconnectDevice(payload['identifier'], payload['device'])
def senddata(self, payload): if not 'data' in payload or not 'identifier' in payload: return response.throw(24) return System.User.Mobile.sendDataTo(payload['identifier'], payload['data'])
def connect(self, payload): if not 'pin' in payload or not 'identifier' in payload or not 'host' in payload: return response.throw(24) return System.User.Host.connectTo(payload['host'], payload['identifier'], payload['pin'])
def hostdisconnect(self, payload): if not 'identifier' in payload: return response.throw(24) return System.User.Host.disconnect(payload['identifier'])
def logout(self, payload): if not 'identifier' in payload: return response.throw(24) return System.User.Mobile.delete(payload['identifier'])
def mobile(self, payload): if not 'pin' in payload or not 'name' in payload or not 'model' in payload: return response.throw(24) return System.User.Mobile.create(payload['__Webapp2Instance__'], payload['name'], payload['model'], payload['pin'])
def throw(self, code): self.response.headers['Content-Type'] = 'application/json' self.response.out.write(response.throw(code, compiled=True))
def update(self, payload): from ..blog import posts post = posts.get(payload['identifier']) if post == None: return response.throw(200, data_struct=identifier) post.edit(payload['content'], author=payload['author'], title=payload['title'])
def delete(self, payload): from ..blog import posts post = posts.get(payload['identifier']) if post == None: return response.throw(200, data_struct=identifier) post.delete()
def reciever(self, payload): for key in keys: if not key in payload: return response.throw(001) return funct(self, payload)
def setemail(self, payload): user = payload['__Webapp2Instance__'].user if user.changeEmail(payload['email']) == user.EMAIL_IS_USED: return response.throw(200)
def default(cls, self, dictionary, method): self.response.out.write(response.throw(002, compiled=True))
def signup(self, payload): from ..testing.alphas import emails status = emails.add(payload['email']) if status == emails.EMAIL_IS_USED: return response.throw(200)