from functools import partial

import pytest
from requests import Response
from tornado.web import Application, RequestHandler

from postmarker.tornado import PostmarkMixin


class BaseHandler(PostmarkMixin, RequestHandler):
    def get(self):
        self.write(str(self.get_value()))


class Handler(BaseHandler):
    def get_value(self):
        return self.postmark_client.server_token


class MaxRetriesHandler(BaseHandler):
    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):
    def get_value(self):
        return self.send(
            From="sender@example.com",
            To="receiver@example.com",
            Subject="Postmark test",
            HtmlBody="<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
        )["Message"]


class SendBatchHandler(BaseHandler):
    def get_value(self):
        return self.send_batch(
            {
                "From": "sender@example.com",
                "To": "receiver@example.com",
                "Subject": "Postmark test",
                "HtmlBody": "<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
            }
        )[0]["Message"]


class ReuseHandler(BaseHandler):
    def get_value(self):
        return self.postmark_client is self.postmark_client


@pytest.fixture
def app():
    return Application(
        [
            (r"/", Handler),
            (r"/send/", SendHandler),
            (r"/send_batch/", SendBatchHandler),
            (r"/reuse/", ReuseHandler),
            (r"/max_retries/", MaxRetriesHandler),
        ],
        postmark_server_token="Test token",
    )


@pytest.fixture
def postmark_request(postmark_request):
    postmark_request.return_value = Response()
    postmark_request.return_value.status_code = 200
    return postmark_request


@pytest.fixture
def http_client(http_client, base_url):
    """Makes original http_client synchronous, to gather coverage data."""
    original_fetch = http_client.fetch

    def _fetch(url):
        fetch = partial(original_fetch, base_url + url)
        return http_client.io_loop.run_sync(fetch)

    http_client.fetch = _fetch
    return http_client

e72cf77d40d8fa7fd6e80a60a9e89d38fc22450c
<mask token>


class MaxRetriesHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):

    def get_value(self):
        return self.send(From='sender@example.com', To=
            'receiver@example.com', Subject='Postmark test', HtmlBody=
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            )['Message']


class SendBatchHandler(BaseHandler):

    def get_value(self):
        return self.send_batch({'From': 'sender@example.com', 'To':
            'receiver@example.com', 'Subject': 'Postmark test', 'HtmlBody':
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            })[0]['Message']


class ReuseHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client is self.postmark_client


<mask token>

<mask token>


class Handler(BaseHandler):

    def get_value(self):
        return self.postmark_client.server_token


class MaxRetriesHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):

    def get_value(self):
        return self.send(From='sender@example.com', To=
            'receiver@example.com', Subject='Postmark test', HtmlBody=
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            )['Message']


class SendBatchHandler(BaseHandler):

    def get_value(self):
        return self.send_batch({'From': 'sender@example.com', 'To':
            'receiver@example.com', 'Subject': 'Postmark test', 'HtmlBody':
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            })[0]['Message']


class ReuseHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client is self.postmark_client


<mask token>

<mask token>


class BaseHandler(PostmarkMixin, RequestHandler):

    def get(self):
        self.write(str(self.get_value()))


class Handler(BaseHandler):

    def get_value(self):
        return self.postmark_client.server_token


class MaxRetriesHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):

    def get_value(self):
        return self.send(From='sender@example.com', To=
            'receiver@example.com', Subject='Postmark test', HtmlBody=
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            )['Message']


class SendBatchHandler(BaseHandler):

    def get_value(self):
        return self.send_batch({'From': 'sender@example.com', 'To':
            'receiver@example.com', 'Subject': 'Postmark test', 'HtmlBody':
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            })[0]['Message']


class ReuseHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client is self.postmark_client


<mask token>

<mask token>


class BaseHandler(PostmarkMixin, RequestHandler):

    def get(self):
        self.write(str(self.get_value()))


class Handler(BaseHandler):

    def get_value(self):
        return self.postmark_client.server_token


class MaxRetriesHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):

    def get_value(self):
        return self.send(From='sender@example.com', To=
            'receiver@example.com', Subject='Postmark test', HtmlBody=
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            )['Message']


class SendBatchHandler(BaseHandler):

    def get_value(self):
        return self.send_batch({'From': 'sender@example.com', 'To':
            'receiver@example.com', 'Subject': 'Postmark test', 'HtmlBody':
            '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'
            })[0]['Message']


class ReuseHandler(BaseHandler):

    def get_value(self):
        return self.postmark_client is self.postmark_client


<mask token>


@pytest.fixture
def http_client(http_client, base_url):
    """Makes original http_client synchronous, to gather coverage data."""
    original_fetch = http_client.fetch

    def _fetch(url):
        fetch = partial(original_fetch, base_url + url)
        return http_client.io_loop.run_sync(fetch)
    http_client.fetch = _fetch
    return http_client

from functools import partial

import pytest
from requests import Response
from tornado.web import Application, RequestHandler

from postmarker.tornado import PostmarkMixin


class BaseHandler(PostmarkMixin, RequestHandler):
    def get(self):
        self.write(str(self.get_value()))


class Handler(BaseHandler):
    def get_value(self):
        return self.postmark_client.server_token


class MaxRetriesHandler(BaseHandler):
    def get_value(self):
        return self.postmark_client.max_retries


class SendHandler(BaseHandler):
    def get_value(self):
        return self.send(
            From="sender@example.com",
            To="receiver@example.com",
            Subject="Postmark test",
            HtmlBody="<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
        )["Message"]


class SendBatchHandler(BaseHandler):
    def get_value(self):
        return self.send_batch(
            {
                "From": "sender@example.com",
                "To": "receiver@example.com",
                "Subject": "Postmark test",
                "HtmlBody": "<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
            }
        )[0]["Message"]


class ReuseHandler(BaseHandler):
    def get_value(self):
        return self.postmark_client is self.postmark_client


@pytest.fixture
def app():
    return Application(
        [
            (r"/", Handler),
            (r"/send/", SendHandler),
            (r"/send_batch/", SendBatchHandler),
            (r"/reuse/", ReuseHandler),
            (r"/max_retries/", MaxRetriesHandler),
        ],
        postmark_server_token="Test token",
    )


@pytest.fixture
def postmark_request(postmark_request):
    postmark_request.return_value = Response()
    postmark_request.return_value.status_code = 200
    return postmark_request


@pytest.fixture
def http_client(http_client, base_url):
    """Makes original http_client synchronous, to gather coverage data."""
    original_fetch = http_client.fetch

    def _fetch(url):
        fetch = partial(original_fetch, base_url + url)
        return http_client.io_loop.run_sync(fetch)

    http_client.fetch = _fetch
    return http_client
