# Path: autopush/constants.py
# DEFAULT_ROUTER_TIMEOUT = 3
# 
# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs


import json
import treq
from oauth2client.service_account import ServiceAccountCredentials
from twisted.logger import Logger
from twisted.internet.error import (ConnectError, TimeoutError)
from autopush.constants import DEFAULT_ROUTER_TIMEOUT
from autopush.exceptions import RouterException

    def _build_message(self, token, notif):
        msg = {
            "token": token,
            # Specify the various formats (we use android only)
            "android": {
                # TTL is a duration string e.g. ("60s")
                "ttl": str(int(notif.get("ttl", 0)))+"s",
                "data": notif.get("data_message")
            },
        }
        # Wrap up the whole thing in a "message" tag.
        return {"message": msg}

    def process(self, response, payload=None):
        if response.code == 401:
            raise FCMAuthenticationError("Authentication Error")

        result = Result(response)

        d = response.text()
        d.addCallback(result.parse_response)
        return d

    def error(self, failure):
        if isinstance(failure.value,
                      (FCMAuthenticationError, FCMNotFoundError,
                       TimeoutError, ConnectError)):
            raise failure.value
        self.logger.error("FCMv1Client failure: {}".format(failure.value))

# Path: autopush/constants.py
# DEFAULT_ROUTER_TIMEOUT = 3
# 
# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs


# Path: autopush/router/fcmv1client.py
import json
import treq
from oauth2client.service_account import ServiceAccountCredentials
from twisted.logger import Logger
from twisted.internet.error import (ConnectError, TimeoutError)
from autopush.constants import DEFAULT_ROUTER_TIMEOUT
from autopush.exceptions import RouterException


    def _build_message(self, token, notif):
        msg = {
            "token": token,
            # Specify the various formats (we use android only)
            "android": {
                # TTL is a duration string e.g. ("60s")
                "ttl": str(int(notif.get("ttl", 0)))+"s",
                "data": notif.get("data_message")
            },
        }
        # Wrap up the whole thing in a "message" tag.
        return {"message": msg}

    def process(self, response, payload=None):
        if response.code == 401:
            raise FCMAuthenticationError("Authentication Error")

        result = Result(response)

        d = response.text()
        d.addCallback(result.parse_response)
        return d

    def error(self, failure):
        if isinstance(failure.value,
                      (FCMAuthenticationError, FCMNotFoundError,
                       TimeoutError, ConnectError)):
            raise failure.value
        self.logger.error("FCMv1Client failure: {}".format(failure.value))

        raise RouterException("Server error: {}".format(failure.value))