# Path: roulier/exception.py
# class InvalidApiInput(Exception):
#     """Bad input.
# 
#     Use this class in your application to manage
#     exception with api call
#     """
# 
# Path: roulier/codec.py
# class Encoder(ABC):
#     def __init__(self, config_object):
#         self.config = config_object
# 
#     def _extra_input_data_processing(self, input_payload, data):
#         return data
# 
#     @abstractmethod
#     def transform_input_to_carrier_webservice(self, data):
#         pass
# 
#     def encode(self, input_payload):
#         """Transform input from external app to compatible input for carrier webservice."""
#         validator = self.config.api(self.config)
#         if not validator.validate(input_payload):
#             _logger.warning("Laposte api call exception:")
#             raise InvalidApiInput(
#                 {"api_call_exception": validator.errors(input_payload)}
#             )
#         # Set isTest in config if it is the case so the system choose the test url
#         # if any
#         if input_payload.get("auth", {}).get("isTest"):
#             self.config.is_test = True
#         data = validator.normalize(input_payload)
#         data = self._extra_input_data_processing(input_payload, data)
#         return self.transform_input_to_carrier_webservice(data)
# 
# Path: roulier/carriers/laposte_fr/api.py
# LAPOSTE_LABEL_FORMAT = (
#     "ZPL_10x15_203dpi",
#     "ZPL_10x15_300dpi",
#     "DPL_10x15_203dpi",
#     "DPL_10x15_300dpi",
#     "PDF_10x15_300dpi",
#     "PDF_A4_300dpi",
# )


from copy import deepcopy
from jinja2 import Environment, PackageLoader
from roulier.exception import InvalidApiInput
from roulier.codec import Encoder
from .api import LAPOSTE_LABEL_FORMAT
import logging
    def _extra_input_data_processing(self, input_payload, data):
        data["service"]["labelFormat"] = self.lookup_label_format(
            data["service"]["labelFormat"]
        )
        data["service"]["shippingDate"] = data["service"]["shippingDate"].isoformat()
        # Since multi parcels is not managed for la poste, some informations are expected
        # in service by laposte but are in parcel in roulier.
        parcel = data["parcels"][0]
        if parcel.get("totalAmount"):
            data["service"]["totalAmount"] = parcel["totalAmount"]
        return data

    def transform_input_to_carrier_webservice(self, data):
        transformed_input = super().transform_input_to_carrier_webservice(data)
        transformed_input["output_format"] = data["service"]["labelFormat"]
        return transformed_input

    def lookup_label_format(self, label_format="ZPL"):
        """Get a Laposte compatible format of label.

        args:
            label_format: (str) ZPL or ZPL_10x15_300dpi
        return
            a value taken in LAPOSTE_LABEL_FORMAT
        """
        lookup = {
            "ZPL": "ZPL_10x15_300dpi",
            "DPL": "DPL_10x15_300dpi",
            "PDF": "PDF_10x15_300dpi",
        }

# Path: roulier/exception.py
# class InvalidApiInput(Exception):
#     """Bad input.
# 
#     Use this class in your application to manage
#     exception with api call
#     """
# 
# Path: roulier/codec.py
# class Encoder(ABC):
#     def __init__(self, config_object):
#         self.config = config_object
# 
#     def _extra_input_data_processing(self, input_payload, data):
#         return data
# 
#     @abstractmethod
#     def transform_input_to_carrier_webservice(self, data):
#         pass
# 
#     def encode(self, input_payload):
#         """Transform input from external app to compatible input for carrier webservice."""
#         validator = self.config.api(self.config)
#         if not validator.validate(input_payload):
#             _logger.warning("Laposte api call exception:")
#             raise InvalidApiInput(
#                 {"api_call_exception": validator.errors(input_payload)}
#             )
#         # Set isTest in config if it is the case so the system choose the test url
#         # if any
#         if input_payload.get("auth", {}).get("isTest"):
#             self.config.is_test = True
#         data = validator.normalize(input_payload)
#         data = self._extra_input_data_processing(input_payload, data)
#         return self.transform_input_to_carrier_webservice(data)
# 
# Path: roulier/carriers/laposte_fr/api.py
# LAPOSTE_LABEL_FORMAT = (
#     "ZPL_10x15_203dpi",
#     "ZPL_10x15_300dpi",
#     "DPL_10x15_203dpi",
#     "DPL_10x15_300dpi",
#     "PDF_10x15_300dpi",
#     "PDF_A4_300dpi",
# )


# Path: roulier/carriers/laposte_fr/encoder.py
from copy import deepcopy
from jinja2 import Environment, PackageLoader
from roulier.exception import InvalidApiInput
from roulier.codec import Encoder
from .api import LAPOSTE_LABEL_FORMAT
import logging

    def _extra_input_data_processing(self, input_payload, data):
        data["service"]["labelFormat"] = self.lookup_label_format(
            data["service"]["labelFormat"]
        )
        data["service"]["shippingDate"] = data["service"]["shippingDate"].isoformat()
        # Since multi parcels is not managed for la poste, some informations are expected
        # in service by laposte but are in parcel in roulier.
        parcel = data["parcels"][0]
        if parcel.get("totalAmount"):
            data["service"]["totalAmount"] = parcel["totalAmount"]
        return data

    def transform_input_to_carrier_webservice(self, data):
        transformed_input = super().transform_input_to_carrier_webservice(data)
        transformed_input["output_format"] = data["service"]["labelFormat"]
        return transformed_input

    def lookup_label_format(self, label_format="ZPL"):
        """Get a Laposte compatible format of label.

        args:
            label_format: (str) ZPL or ZPL_10x15_300dpi
        return
            a value taken in LAPOSTE_LABEL_FORMAT
        """
        lookup = {
            "ZPL": "ZPL_10x15_300dpi",
            "DPL": "DPL_10x15_300dpi",
            "PDF": "PDF_10x15_300dpi",
        }

        if label_format in LAPOSTE_LABEL_FORMAT: