def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) type = pkt.data['catalog_name'] n = NetworkPacket() if type in self.CATALOGS.keys(): func = pkt.data['func'] message = pkt.data['message'] if func in self.FUNCS: if func == 'get_catalog': offset = message['offset'] limit = message['limit'] n.data['status'] = 'OK' n.data['message'] = toJson(self.CATALOGS[type], offset, limit) else: ApiWorker.send_error(ch, method, props, body, 'Invalid func field') self.db.close() else: ApiWorker.send_error(ch, method, props, body, 'Invalid catalog name') self.db.close() return ApiWorker.send_reply(ch, method, props, n.toJson()) self.db.close() return
def send_error(ch, method, props, msg): n = NetworkPacket() n.data['status'] = 'ERROR' n.data['message'] = msg payload = n.toJson() if props.reply_to != None: ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id=props.correlation_id), body=payload) ch.basic_nack(delivery_tag=method.delivery_tag, multiple=False, requeue=False) # print " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload Log().send(type = "info", msg = " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload)
def handle_addaccount(self, ch, method, props, pkt): try: uuid = pkt.data['uuid'] medium = pkt.data['message']['medium_type'] value = pkt.data['message']['medium_data'] # find user user = User.get(User.uuid == uuid) if medium in VALID_MEDIUM_TYPES: createdSocial = None userSocialData = None if medium in ['vk','fb','gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=value) uuid = self.generate_uuid(value) if createdSocial == True: userSocialData.user = user userSocialData.data = pkt.data['message']['medium_data'] userSocialData.save() user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} n.data['message']['uuid'] = uuid ApiWorker.send_reply(ch, method, props, n.toJson()) else: ApiWorker.send_error(ch, method, props, 'Social account is linked to another User') else: ApiWorker.send_error(ch, method, props, 'Invalid login medium') except KeyError as e: ApiWorker.send_error(ch, method, props, 'Have you forgotten some field? ' + str(e.message)) except DoesNotExist as e: ApiWorker.send_error(ch, method, props, 'User with this UUID does not exist.' + str(e.message))
def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) uuid = pkt.data['uuid'] cli_utc = pkt.data['message']['timestamp'] try: user = User.select().where(User.uuid == uuid).get() except DoesNotExist, e: ApiWorker.send_error(ch, method, props, "User does not Exist") return
class SyncApi(): def __init__(self): pass def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) uuid = pkt.data['uuid'] cli_utc = pkt.data['message']['timestamp'] try: user = User.select().where(User.uuid == uuid).get() except DoesNotExist, e: ApiWorker.send_error(ch, method, props, "User does not Exist") return if user.db != None: srv_utc = user.timestamp else: srv_utc = 0 # print srv_utc, cli_utc if cli_utc > srv_utc: # print byteify(json.dumps(pkt.data['message']['db'])) user.db = byteify(json.dumps(pkt.data['message']['db'])) user.timestamp = cli_utc user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = "Thank you" ApiWorker.send_reply(ch, method, props, n.toJson()) else: # print byteify(json.loads(user.db)) n = NetworkPacket() n.data['status'] = "UPDATE" n.data['message'] = byteify(json.loads(user.db)) # print n.data['message'] ApiWorker.send_reply(ch, method, props, n.toJson())
def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) uuid = pkt.data['uuid'] type = pkt.data['func'] # check if uuid valid try: user = User.select().where(User.uuid == uuid).get() except DoesNotExist, e: ApiWorker.send_error(ch, method, props, "User does not Exist") return
def send_error(ch, method, props, msg): n = NetworkPacket() n.data['status'] = 'ERROR' n.data['message'] = msg payload = n.toJson() if props.reply_to != None: ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties( correlation_id=props.correlation_id), body=payload) ch.basic_nack(delivery_tag=method.delivery_tag, multiple=False, requeue=False) # print " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload Log().send(type="info", msg=" ~~ Error msg sent to " + str(props.reply_to) + ": " + payload)
def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) type = pkt.data['func'] #check for valid request if type in VALID_REQUEST_TYPES: if type in ['login', 'register']: self.handle_login(ch, method, props, pkt) if type == 'add_account': self.handle_addaccount(ch, method, props, pkt) else: ApiWorker.send_error(ch, method, props, 'Invalid request field type')
def handle_addaccount(self, ch, method, props, pkt): try: uuid = pkt.data['uuid'] medium = pkt.data['message']['medium_type'] value = pkt.data['message']['medium_data'] # find user user = User.get(User.uuid == uuid) if medium in VALID_MEDIUM_TYPES: createdSocial = None userSocialData = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=value) uuid = self.generate_uuid(value) if createdSocial == True: userSocialData.user = user userSocialData.data = pkt.data['message']['medium_data'] userSocialData.save() user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} n.data['message']['uuid'] = uuid ApiWorker.send_reply(ch, method, props, n.toJson()) else: ApiWorker.send_error( ch, method, props, 'Social account is linked to another User') else: ApiWorker.send_error(ch, method, props, 'Invalid login medium') except KeyError as e: ApiWorker.send_error( ch, method, props, 'Have you forgotten some field? ' + str(e.message)) except DoesNotExist as e: ApiWorker.send_error( ch, method, props, 'User with this UUID does not exist.' + str(e.message))
def main(): # import pprint # pp = pprint.PrettyPrinter(indent=4) # pp.pprint(n.data) n = NetworkPacket() n.data['api'] = 'auth' n.data['func'] = 'login' n.data['message'] = {} n.data['message']['medium_type'] = 'email' n.data['message']['medium_data'] = {} n.data['message']['medium_data']['login'] = '******' n.data['message']['medium_data']['password'] = '******' n.data['message']['medium_data']['action'] = 'register' n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(), "\n" # { 'accounts': [{ 'data': u'korshakv', 'id': 1, 'medium': u'vk'}], # 'db': None, # 'devices': [ { 'device_id': u'test_id', # 'device_name': u'test_name', # 'id': 1}], # 'fb': None, # 'gp': None, # 'id': 1, # 'phone': None, # 'uuid': '13ab9ad4-4904-5fa7-91d6-b5808245b46b', # 'vk': u'korshakv'} # {"message": {"medium_type": "email", "device_data": {"device_name": "test_name", "device_id": "test_id"}, "medium_data": "*****@*****.**"}, "api": "auth", "func": "login"} # {"message": {"medium_type": "fb", "device_data": {"timestamp": 1439448064478, "device_name": "LGE Nexus 4", "device_id": "9a9622ef5c84229"}, "medium_data": {"name": "Alex Yermolenko", "DOB": "", "gender": "male", "url": "https://www.facebook.com/app_scoped_user_id/10202972046261630/", "email": "*****@*****.**", "fotourl": "https://graph.facebook.com/10202972046261630/picture?type=large"}}, "api": "auth", "func": "login"} n.data['api'] = 'auth' n.data['func'] = 'add_account' n.data['uuid'] = '488416f4-fcaf-5027-8c63-0105cfa213ea' n.data['message'] = {} n.data['message']['medium_type'] = 'fb' n.data['message']['medium_data'] = {'url': 'www.google.com'} n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(), "\n"
def handle_login(self, ch, method, props, pkt): # lets check his account information first # medium: phone, vk, fb, gp, loginpass medium = pkt.data['message']['medium_type'] if medium == 'guest': userDevice, createdDevice = Device.get_or_create( device_id=pkt.data['message']['device_data']['device_id'], device_name=pkt.data['message']['device_data']['device_name']) userDevice.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = None ApiWorker.send_reply(ch, method, props, n.toJson()) return value = pkt.data['message']['medium_data'] if medium not in VALID_MEDIUM_TYPES: ApiWorker.send_error(ch, method, props, 'Invalid login medium') return else: userSocialData = None createdSocial = None uuid = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=value) uuid = self.generate_uuid(value) if medium in ['email']: userSocialData, createdSocial = self.handle_email(ch, method, props, pkt) uuid = uuid = self.generate_uuid(value['login']) if userSocialData is not None: if createdSocial is False: # user exists if have not created a social account for him user = userSocialData.user print " -- user account exists " else: print " -- new account is going to be created" # we have a new incoming user trying to login user = self.create_new_user(userSocialData, uuid, pkt) # prepare the reply packet n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} if user.db is not None: n.data['message']['db'] = byteify(json.loads(user.db)) else: n.data['message']['db'] = None n.data['message']['uuid'] = user.uuid ApiWorker.send_reply(ch, method, props, n.toJson()) return
def main(): # import pprint # pp = pprint.PrettyPrinter(indent=4) # pp.pprint(n.data) n = NetworkPacket() n.data['api'] = 'auth' n.data['func'] = 'login' n.data['message'] = {} n.data['message']['medium_type'] = 'email' n.data['message']['medium_data'] = {} n.data['message']['medium_data']['login'] = '******' n.data['message']['medium_data']['password'] = '******' n.data['message']['medium_data']['action'] = 'register' n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(),"\n" # { 'accounts': [{ 'data': u'korshakv', 'id': 1, 'medium': u'vk'}], # 'db': None, # 'devices': [ { 'device_id': u'test_id', # 'device_name': u'test_name', # 'id': 1}], # 'fb': None, # 'gp': None, # 'id': 1, # 'phone': None, # 'uuid': '13ab9ad4-4904-5fa7-91d6-b5808245b46b', # 'vk': u'korshakv'} # {"message": {"medium_type": "email", "device_data": {"device_name": "test_name", "device_id": "test_id"}, "medium_data": "*****@*****.**"}, "api": "auth", "func": "login"} # {"message": {"medium_type": "fb", "device_data": {"timestamp": 1439448064478, "device_name": "LGE Nexus 4", "device_id": "9a9622ef5c84229"}, "medium_data": {"name": "Alex Yermolenko", "DOB": "", "gender": "male", "url": "https://www.facebook.com/app_scoped_user_id/10202972046261630/", "email": "*****@*****.**", "fotourl": "https://graph.facebook.com/10202972046261630/picture?type=large"}}, "api": "auth", "func": "login"} n.data['api'] = 'auth' n.data['func'] = 'add_account' n.data['uuid'] = '488416f4-fcaf-5027-8c63-0105cfa213ea' n.data['message'] = {} n.data['message']['medium_type'] = 'fb' n.data['message']['medium_data'] = {'url':'www.google.com'} n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(),"\n"
def handle_login(self, ch, method, props, pkt): # lets check his account information first # medium: phone, vk, fb, gp, loginpass medium = pkt.data['message']['medium_type'] if medium == 'guest': userDevice, createdDevice = Device.get_or_create( device_id=pkt.data['message']['device_data']['device_id'], device_name=pkt.data['message']['device_data']['device_name']) userDevice.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = None ApiWorker.send_reply(ch, method, props, n.toJson()) return value = pkt.data['message']['medium_data'] if medium not in VALID_MEDIUM_TYPES: ApiWorker.send_error(ch, method, props, 'Invalid login medium') return else: userSocialData = None createdSocial = None uuid = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=value) uuid = self.generate_uuid(value) if medium in ['email']: userSocialData, createdSocial = self.handle_email( ch, method, props, pkt) uuid = uuid = self.generate_uuid(value['login']) if userSocialData is not None: if createdSocial is False: # user exists if have not created a social account for him user = userSocialData.user print " -- user account exists " else: print " -- new account is going to be created" # we have a new incoming user trying to login user = self.create_new_user(userSocialData, uuid, pkt) # prepare the reply packet n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} if user.db is not None: n.data['message']['db'] = byteify(json.loads(user.db)) else: n.data['message']['db'] = None n.data['message']['uuid'] = user.uuid ApiWorker.send_reply(ch, method, props, n.toJson()) return
class NotificationApi(): def __init__(self): pass def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) uuid = pkt.data['uuid'] type = pkt.data['func'] # check if uuid valid try: user = User.select().where(User.uuid == uuid).get() except DoesNotExist, e: ApiWorker.send_error(ch, method, props, "User does not Exist") return # check for valid request if type in VALID_REQUEST_TYPES: if type in ['push']: rcv = pkt.data['message']['receiver'] snd = pkt.data['uuid'] payload = pkt.data['message']['body'] type = pkt.data['message']['type'] rcv_user = None snd_user = None if rcv['email'] is not None: rcv_user = SocialData.select().where( SocialData.medium == 'email' and SocialData.value == rcv['email']) if rcv['phone'] is not None and rcv_user is None: rcv_user = SocialData.select().where( SocialData.medium == 'phone' and SocialData.value == rcv['phone']) if snd['email'] is not None: snd_user = SocialData.select().where( SocialData.medium == 'email' and SocialData.value == snd['email']) if snd['phone'] is not None and snd_user is None: snd_user = SocialData.select().where( SocialData.medium == 'phone' and SocialData.value == snd['phone']) if rcv_user is None or snd_user is None: ApiWorker.send_error( ch, method, props, 'Receiver or Sender user could not be found') else: # save notification in db notification = Notification() notification.receiver = rcv_user notification.sender = snd_user notification.message = payload notification.type = type notification.status = False # send push all connected devices based on uuid of receiver ch.basic_publish(exchange='push' + VERSION_PREFIX, routing_key=rcv_user.user.uuid + ".#", body=payload) # send reply to client n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = None ApiWorker.send_reply(ch, method, props, n.toJson()) if type in ['pull']: # get notification for the last 3 days user_notifications = Notification.select()\ .where(Notification.receiver == uuid | Notification.created_date >= date.today() - timedelta(days=3)) n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} for each in user_notifications: n.data['message']['notifications'].append(each) ApiWorker.send_reply(ch, method, props, n.toJson())