def create_session():
    try:
        api_client = MessageBusAPIClient(api_key, uri=uri)
        results = api_client.create_session(channel=channel,
                                            session_name=session_name)
    except MessageBusResponseError, error:
        raise error
    def test_mb_send_207(self):
        expected_resp = json.dumps(
            {
                u"failure_count": 1,
                u"results": [
                    {
                        u"message_id": u"fbeeb6e0838911e289f5bc764e049d62",
                        u"message_status": 0,
                        u"to_email": u"*****@*****.**",
                    },
                    {
                        u"message_id": u"fbeeddf0838911e289f5bc764e049d62",
                        u"message_status": 1002,
                        u"to_email": u"usarin",
                    },
                ],
                u"status_code": 207,
                u"status_message": u"One or more emails not sent. See individual error messages.",
                u"status_time": u"2013-03-02T22:39:03.581Z",
                u"success_count": 1,
            },
            sort_keys=True,
        )

        mb = MessageBusAPIClient(self.api_key)
        mb._last_init_time = datetime.datetime.now()

        body = json.dumps({"messages": []})
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", constants.end_points["message_emails_send"], body, 207, expected_resp
        )

        received_resp = mb.send_messages([])
        self.assertEqual(json.dumps(received_resp, sort_keys=True), expected_resp)
        self.mocker.UnsetStubs()
Exemple #3
0
def send_emails():
    try:
        api_client = MessageBusAPIClient(api_key, uri=uri)
        message_jane = {
            'to_email': '*****@*****.**',
            'to_name': 'Jane Smith',
            'from_email': '*****@*****.**',
            'from_name': 'Example Corporation',
            'subject': 'Single Message Sample for Jane Smith',
            'return_path': '*****@*****.**',
            'plaintext_body': 'This message to Jane Smith is a test sent by the Python Message Bus client library.',
            'html_body': '<html><body>This message to Jane Smith is a test sent by the Python Message Bus sdk.</body></html>',
            'session_key': 'DEFAULT',
        }

        message_john = {
            'to_email': '*****@*****.**',
            'to_name': 'John Doe',
            'from_email': '*****@*****.**',
            'from_name': 'Example Corporation',
            'subject': 'Single Message Sample for John Doe',
            'return_path': '*****@*****.**',
            'plaintext_body': 'This message to John Doe is a test sent by the Python Message Bus client library.',
            'html_body': '<html><body>This message to John Doe is a test sent by the Python Message Bus sdk.</body></html>',
            'session_key': 'DEFAULT',
        }
        results = api_client.send_messages([message_jane, message_john])
    except MessageBusResponseError, error:
        print error.message
    def test_mb_rest_headers(self):
        mb = MessageBusAPIClient(self.api_key)

        base_headers = mb.__base_headers__()
        self.assertEquals(len(base_headers.items()), 2)
        self.assertEquals(base_headers["X-MessageBus-Key"], self.api_key)

        post_headers = mb.__post_headers__()
        self.assertEquals(len(post_headers.items()), 1)
        self.assertEquals(post_headers["Content-Type"], "application/json; charset=utf-8")
    def test_mb_flush_non_2xx(self):
        expected_resp = json.dumps(
            {u"status_code": 400, u"status_message": u"A non 2xx message", u"status_time": u"2013-03-02T22:39:03.581Z"}
        )
        mb = MessageBusAPIClient(self.api_key)
        mb._last_init_time = datetime.datetime.now()

        body = json.dumps({"messages": []})
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", constants.end_points["message_emails_send"], body, 400, expected_resp
        )

        # should raise exception
        self.assertRaises(MessageBusResponseError, mb.send_messages, [])
        self.mocker.UnsetStubs()
    def test_underscorify(self):
        input_camel = json.dumps(
            {
                u"stats": {
                    u"msgsAttemptedCount": 0,
                    u"openCount": 0,
                    u"unsubscribeCount": 0,
                    u"complaintCount": 0,
                    u"clickCount": 0,
                },
                u"smtp": {u"rejectCount": 0, u"bounceCount": 0, u"acceptCount": 0, u"deferralCount": 0},
                u"filter": {u"rcptBadMailboxCount": 0, u"rcptChannelBlockCount": 0},
                u"statusTime": u"2012-09-19T22:40:45.123Z",
                u"statusMessage": u"stats request succeeded",
                u"statusCode": 200,
            },
            sort_keys=True,
        )

        expected_resp_dict = {
            u"stats": {
                u"msgs_attempted_count": 0,
                u"open_count": 0,
                u"unsubscribe_count": 0,
                u"complaint_count": 0,
                u"click_count": 0,
            },
            u"smtp": {u"reject_count": 0, u"bounce_count": 0, u"accept_count": 0, u"deferral_count": 0},
            u"filter": {u"rcpt_bad_mailbox_count": 0, u"rcpt_channel_block_count": 0},
            u"status_time": u"2012-09-19T22:40:45.123Z",
            u"status_message": u"stats request succeeded",
            u"status_code": 200,
        }

        mb = MessageBusAPIClient("foo")

        converted_dict = mb.__camel_to_underscore__(input_camel)
        self.assertEquals(expected_resp_dict, converted_dict)

        _, convertedCamel = mb.__underscore_to_camel__(converted_dict)
        self.assertEquals(convertedCamel, input_camel)
    def test_version(self):
        expected_resp = json.dumps(
            {
                u"APIName": u"api",
                u"APIVersion": u"1.1.12.0-beta-201210081020",
                u"status_code": 200,
                u"status_message": u"API Version Lookup",
                u"status_time": u"2010-10-22T17:42:59.556Z",
            },
            sort_keys=True,
        )

        mb = MessageBusAPIClient(self.api_key)
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "GET", "/version", "", 200, expected_resp
        )

        received_resp = json.dumps(mb.api_version(), sort_keys=True)
        self._validate_results(expected_resp, received_resp)

        self.mocker.UnsetStubs()
    def test_session_create(self):
        expected_resp = json.dumps(
            {
                u"session_name": u"test session name",
                u"session_key": u"test_session_key",
                u"status_message": u"",
                u"status_time": u"2012-10-31T23:37:44.560Z",
                u"status_code": 202,
            },
            sort_keys=True,
        )

        mb = MessageBusAPIClient(self.api_key)
        channel = "test_channel"
        session_name = "test session name"
        path = "%s" % constants.end_points["channel_sessions"] % {"channel_key": channel}
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", path, json.dumps({"sessionName": session_name}, sort_keys=True), 200, expected_resp
        )
        received_resp = json.dumps(mb.create_session(channel, session_name), sort_keys=True)
        self._validate_results(expected_resp, received_resp)
        self.mocker.UnsetStubs()
    def test_channels(self):
        expected_resp = json.dumps({})
        mb = MessageBusAPIClient(self.api_key)
        channel = "test_channel"

        path = "%s" % constants.end_points["channels"]
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection("GET", path, "", 200, expected_resp)
        received_resp = json.dumps(mb.get_channels(), sort_keys=True)
        self._validate_results(expected_resp, received_resp)
        self.mocker.UnsetStubs()

        path = "%s" % constants.end_points["channels"]
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection("GET", path, "", 200, expected_resp)
        received_resp = json.dumps(mb.get_channels(), sort_keys=True)
        self._validate_results(expected_resp, received_resp)
        self.mocker.UnsetStubs()

        path = "%s" % constants.end_points["channel_config"] % {"channel_key": channel}
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection("GET", path, "", 200, expected_resp)
        received_resp = json.dumps(mb.get_channel_config("test_channel"), sort_keys=True)
        self._validate_results(expected_resp, received_resp)
        self.mocker.UnsetStubs()
def get_sessions():
    try:
        api_client = MessageBusAPIClient(api_key, uri=uri)
        results = api_client.get_channel_sessions(channel=channel)
    except MessageBusResponseError, error:
        raise error
def get_sessions():
    try:
        api_client = MessageBusAPIClient(api_key, uri=uri)
        results = api_client.get_channel_sessions(channel=channel)
    except MessageBusResponseError, error:
        raise error
def create_session():
    try:
        api_client = MessageBusAPIClient(api_key, uri=uri)
        results = api_client.create_session(channel=channel, session_name=session_name)
    except MessageBusResponseError, error:
        raise error
    def test_mb_simulate_sends(self):
        expected_resp_400 = json.dumps(
            {u"status_code": 400, u"status_message": u"A non 2xx message", u"status_time": u"2013-03-02T22:39:03.581Z"}
        )

        expected_resp_207 = json.dumps(
            {
                u"failure_count": 1,
                u"results": [
                    {
                        u"message_id": u"fbeeb6e0838911e289f5bc764e049d62",
                        u"message_status": 0,
                        u"to_email": u"*****@*****.**",
                    },
                    {u"message_id": u"fbeeddf0838911e289f5bc764e049d62", u"message_status": 1002, u"to_email": u"bob"},
                ],
                u"status_code": 207,
                u"status_message": u"One or more emails not sent. See individual error messages.",
                u"status_time": u"2013-03-02T22:39:03.581Z",
                u"success_count": 1,
            },
            sort_keys=True,
        )

        expected_resp_202 = json.dumps(
            {
                u"failure_count": 0,
                u"results": [
                    {
                        u"message_id": u"fbeeb6e0838911e289f5bc764e049d62",
                        u"message_status": 0,
                        u"to_email": u"*****@*****.**",
                    },
                    {
                        u"message_id": u"fbeeddf0838911e289f5bc764e049d62",
                        u"message_status": 0,
                        u"to_email": u"*****@*****.**",
                    },
                ],
                u"status_code": 202,
                u"status_message": u".",
                u"status_time": u"2013-03-02T22:39:03.581Z",
                u"success_count": 2,
            },
            sort_keys=True,
        )

        mb = MessageBusAPIClient(self.api_key)

        body = json.dumps({"messages": []})
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", constants.end_points["message_emails_send"], body, 400, expected_resp_400
        )
        self.assertRaises(MessageBusResponseError, mb.send_messages, [])
        self.mocker.UnsetStubs()

        body = json.dumps({"messages": []})
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", constants.end_points["message_emails_send"], body, 207, expected_resp_207
        )

        # should not raise exception
        received_resp = mb.send_messages([])

        self.assertEqual(json.dumps(received_resp, sort_keys=True), expected_resp_207)
        self.mocker.UnsetStubs()

        body = json.dumps({"messages": []})
        mb.__dict__["_MessageBusBase__connection"] = self._setup_mock_connection(
            "POST", constants.end_points["message_emails_send"], body, 202, expected_resp_202
        )

        # should not raise exception
        received_resp = mb.send_messages([])

        self.assertEqual(json.dumps(received_resp, sort_keys=True), expected_resp_202)
        self.mocker.UnsetStubs()