def load_vumi_message(self, channel_id, message_id): '''Retrieves the stored vumi message, given its unique id''' key = self.get_key(channel_id, message_id) msg_json = yield self.load_property(key, 'message') if msg_json is None: returnValue(None) returnValue(TransportUserMessage.from_json(msg_json))
def load_vumi_message(self, channel_id, message_id): '''Retrieves the stored vumi message, given its unique id''' key = self.get_key(channel_id, message_id) msg_json = yield self.load_property(key, 'message') if msg_json is None: returnValue(None) returnValue(TransportUserMessage.from_json(msg_json))
def cb(request): self.assertEqual(request.getUser(), 'username') self.assertEqual(request.getPassword(), 'password') msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['message_id'], 'abc') self.assertEqual(msg['content'], 'hello world') self.assertEqual(msg['from_addr'], '+41791234567') return 'OK'
def cb(request): self.assertEqual(request.getUser(), "username") self.assertEqual(request.getPassword(), "password") msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg["message_id"], "abc") self.assertEqual(msg["content"], "hello world") self.assertEqual(msg["from_addr"], "+41791234567") return "OK"
def cb(request): self.assertEqual(request.getUser(), 'username') self.assertEqual(request.getPassword(), 'password') msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['message_id'], 'abc') self.assertEqual(msg['content'], 'hello world') self.assertEqual(msg['from_addr'], '+41791234567') return 'OK'
def test_send_message_storing(self): """Inbound messages should be stored in the InboundMessageStore""" msg = TransportUserMessage.send(to_addr="+1234", content="testcontent") yield self.worker.consume_user_message(msg) redis = self.worker.redis key = "%s:inbound_messages:%s" % (self.worker.config["transport_name"], msg["message_id"]) msg_json = yield redis.hget(key, "message") self.assertEqual(TransportUserMessage.from_json(msg_json), msg)
def test_store_vumi_message(self): '''Stores the vumi message.''' store = yield self.create_store() vumi_msg = TransportUserMessage.send(to_addr='+213', content='foo') yield store.store_vumi_message('channel_id', vumi_msg) msg = yield self.redis.hget( 'channel_id:inbound_messages:%s' % vumi_msg.get('message_id'), 'message') self.assertEqual(vumi_msg, TransportUserMessage.from_json(msg))
def test_store_vumi_message(self): '''Stores the vumi message.''' store = yield self.create_store() vumi_msg = TransportUserMessage.send(to_addr='+213', content='foo') yield store.store_vumi_message('channel_id', vumi_msg) msg = yield self.redis.hget( 'channel_id:inbound_messages:%s' % vumi_msg.get('message_id'), 'message') self.assertEqual(vumi_msg, TransportUserMessage.from_json(msg))
def test_send_message_storing(self): '''Inbound messages should be stored in the InboundMessageStore''' msg = TransportUserMessage.send(to_addr='+1234', content='testcontent') yield self.worker.consume_user_message(msg) redis = self.worker.redis key = '%s:inbound_messages:%s' % ( self.worker.config['transport_name'], msg['message_id']) msg_json = yield redis.hget(key, 'message') self.assertEqual(TransportUserMessage.from_json(msg_json), msg)
def test_send_message_storing(self): '''Inbound messages should be stored in the InboundMessageStore''' msg = TransportUserMessage.send(to_addr='+1234', content='testcontent') yield self.worker.consume_user_message(msg) redis = self.worker.redis key = '%s:inbound_messages:%s' % (self.worker.config['transport_name'], msg['message_id']) msg_json = yield redis.hget(key, 'message') self.assertEqual(TransportUserMessage.from_json(msg_json), msg)
def test_post_inbound_message(self): msg_d = self.app_helper.make_dispatch_inbound( 'in 1', message_id='1', conv=self.conversation) req = yield self.push_calls.get() posted_json = req.content.read() self.assertEqual( req.requestHeaders.getRawHeaders('content-type'), ['application/json; charset=utf-8']) req.finish() msg = yield msg_d posted_msg = TransportUserMessage.from_json(posted_json) self.assertEqual(posted_msg['message_id'], msg['message_id'])
def test_post_inbound_message(self): msg_d = self.app_helper.make_dispatch_inbound('in 1', message_id='1', conv=self.conversation) req = yield self.push_calls.get() posted_json = req.content.read() self.assertEqual(req.requestHeaders.getRawHeaders('content-type'), ['application/json; charset=utf-8']) req.finish() msg = yield msg_d posted_msg = TransportUserMessage.from_json(posted_json) self.assertEqual(posted_msg['message_id'], msg['message_id'])
def _respond_with_pending_messages(self, msginfo, message_id, **kw): """Gathers pending messages and sends a response including them.""" outbound_ids = [] outbound_messages = [] account_key = self.key_for_account(msginfo.account_id) while True: msg_json = yield self.redis.lpop(account_key) if msg_json is None: break msg = TransportUserMessage.from_json(msg_json) outbound_ids.append(msg["message_id"]) outbound_messages.append({"to": msg["to_addr"], "message": msg["content"] or ""}) yield self._send_response(message_id, messages=outbound_messages, **kw) for outbound_id in outbound_ids: yield self.publish_ack(user_message_id=outbound_id, sent_message_id=outbound_id)
def _respond_with_pending_messages(self, msginfo, message_id, **kw): """Gathers pending messages and sends a response including them.""" outbound_ids = [] outbound_messages = [] account_key = self.key_for_account(msginfo.account_id) while True: msg_json = yield self.redis.lpop(account_key) if msg_json is None: break msg = TransportUserMessage.from_json(msg_json) outbound_ids.append(msg['message_id']) outbound_messages.append({'to': msg['to_addr'], 'message': msg['content'] or ''}) yield self._send_response(message_id, messages=outbound_messages, **kw) for outbound_id in outbound_ids: yield self.publish_ack(user_message_id=outbound_id, sent_message_id=outbound_id)
def test_post_inbound_message(self): # Set the URL so stuff is HTTP Posted instead of streamed. self.conversation.config['http_api'].update({ 'push_message_url': self.mock_push_server.url, }) yield self.conversation.save() msg_d = self.app_helper.make_dispatch_inbound( 'in 1', message_id='1', conv=self.conversation) req = yield self.push_calls.get() posted_json_data = req.content.read() req.finish() msg = yield msg_d posted_msg = TransportUserMessage.from_json(posted_json_data) self.assertEqual(posted_msg['message_id'], msg['message_id'])
def assertJSONResultEqual(self, json_blob, messages): """ Asserts that the JSON response we're getting back is the same as the list of messages provided. There are easier ways to do this by comparing bigger JSON blogs but then debugging the huge strings would be a pain. """ dictionaries = json.loads(json_blob) self.assertEqual(len(dictionaries), len(messages), 'Unequal amount of dictionaries and messages') for dictionary, message in zip(dictionaries, messages): # The json dumping & reloading happening here is required to have # the timestamp fields be parsed properly. This is an unfortunate # side effect of how how timestamps are currently stored as # datetime() instances in the payload instead of plain strings. self.assertEqual( TransportUserMessage(_process_fields=False, **message.payload), TransportUserMessage.from_json(json.dumps(dictionary)))
def assertJSONResultEqual(self, json_blob, messages): """ Asserts that the JSON response we're getting back is the same as the list of messages provided. There are easier ways to do this by comparing bigger JSON blogs but then debugging the huge strings would be a pain. """ dictionaries = json.loads(json_blob) self.assertEqual(len(dictionaries), len(messages), 'Unequal amount of dictionaries and messages') for dictionary, message in zip(dictionaries, messages): # The json dumping & reloading happening here is required to have # the timestamp fields be parsed properly. This is an unfortunate # side effect of how how timestamps are currently stored as # datetime() instances in the payload instead of plain strings. self.assertEqual( TransportUserMessage(_process_fields=False, **message.payload), TransportUserMessage.from_json(json.dumps(dictionary)))
def test_post_inbound_message(self): # Set the URL so stuff is HTTP Posted instead of streamed. self.conversation.config['http_api'].update({ 'push_message_url': self.mock_push_server.url, }) yield self.conversation.save() msg_d = self.app_helper.make_dispatch_inbound('in 1', message_id='1', conv=self.conversation) req = yield self.push_calls.get() posted_json_data = req.content.read() req.finish() msg = yield msg_d posted_msg = TransportUserMessage.from_json(posted_json_data) self.assertEqual(posted_msg['message_id'], msg['message_id'])
def send_outbound(self, phone): queue_key = self.r_key(self.redis_outbound_queue) sent_messages = [] while self.r_server.llen(queue_key): json_data = self.r_server.lpop(queue_key) message = TransportUserMessage.from_json(json_data) log.msg('Sending SMS to %s' % (message['to_addr'],)) # keep track of which Gammu messages were sent out for # what Vumi message gammu_messages = {} try: for gammu_message in self.construct_gammu_messages(message): overrides = { 'Number': message['to_addr'], # Send using the Phone's known SMSC 'SMSC': { 'Location': 1 }, # this will create submit message with request # for delivery report 'Type': 'Status_Report', } gammu_message.update(overrides) # keep the message reference for delivery reports send_sms_response = yield deferToThread(phone.SendSMS, gammu_message) gammu_messages[send_sms_response] = gammu_message # multiparts result in multiple acks yield self.publish_ack( user_message_id=message['message_id'], sent_message_id=send_sms_response) # collect for audit trail sent_messages.append((message, gammu_messages)) except gammu.GSMError, e: failure = Failure(e) self.send_failure(message, failure.value, failure.getTraceback())
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['content'], 'hello world') self.assertEqual(msg['from_addr'], '+41791234567') return 'OK'
def _load_message(self, message_id): d = self.redis.get(self._msg_key(message_id)) d.addCallback(lambda r: r and TransportUserMessage.from_json(r)) return d
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg["content"], u"h\xc6llo") return "OK"
def get_cached_message(self, message_id): d = self.redis.get(message_key(message_id)) d.addCallback(lambda json_data: ( TransportUserMessage.from_json(json_data) if json_data else None)) return d
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['content'], u'h\xc6llo') return 'OK'
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['content'], 'hello world') self.assertEqual(msg['from_addr'], '+41791234567') return 'OK'
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg['content'], u'h\xc6llo') return 'OK'
def get_cached_message(self, message_id): d = self.redis.get(message_key(message_id)) d.addCallback(lambda json_data: ( TransportUserMessage.from_json(json_data) if json_data else None)) return d
def cb(request): msg = TransportUserMessage.from_json(request.content.read()) self.assertEqual(msg["content"], "hello world") self.assertEqual(msg["from_addr"], "+41791234567") return "OK"