# Path: roulier/exception.py
# class CarrierError(Exception):
#     """Error from WS.
# 
#     Use this class in your application to manage
#     exception with the carrier WS
#     """
# 
#     def __init__(self, response, msg=None):
#         if msg is None:
#             msg = "An error occured with WS"
#         super(CarrierError, self).__init__(msg)
#         self.response = response
#         if self.response:
#             if hasattr(self.response, "text"):
#                 log.debug(response.text)
#             else:
#                 log.debug(response)


from abc import ABC, abstractmethod
from .exception import CarrierError
import requests
import logging
    def _get_requests_url(self, payload=None):
        if self.config.is_test and self.config.ws_test_url:
            return self.config.ws_test_url
        return self.config.ws_url

    def send_request(self, body, url, auth=None, headers=None, method="post", **kwargs):
        send = getattr(requests, method)
        return send(url, headers=headers, auth=auth, data=body, **kwargs)

    @abstractmethod
    def handle_200(self, response):
        """
        Handle response type 200 (success).
        But it still may contain errors from the carrier webservice.
        """
        pass

    @abstractmethod
    def handle_500(self, response):
        """Handle reponse in case of ERROR 500 type."""
        pass

    def handle_response(self, response):
        """Handle response of webservice."""
        str_status_code = "%s" % response.status_code
        if hasattr(self, "handle_%d" % response.status_code):
            handle = getattr(self, "handle_%d" % response.status_code)
        elif hasattr(self, "handle_%sXX" % str_status_code[0]):
            handle = getattr(self, "handle_%sXX" % str_status_code[0])
        else:

# Path: roulier/exception.py
# class CarrierError(Exception):
#     """Error from WS.
# 
#     Use this class in your application to manage
#     exception with the carrier WS
#     """
# 
#     def __init__(self, response, msg=None):
#         if msg is None:
#             msg = "An error occured with WS"
#         super(CarrierError, self).__init__(msg)
#         self.response = response
#         if self.response:
#             if hasattr(self.response, "text"):
#                 log.debug(response.text)
#             else:
#                 log.debug(response)


# Path: roulier/transport.py
from abc import ABC, abstractmethod
from .exception import CarrierError
import requests
import logging

    def _get_requests_url(self, payload=None):
        if self.config.is_test and self.config.ws_test_url:
            return self.config.ws_test_url
        return self.config.ws_url

    def send_request(self, body, url, auth=None, headers=None, method="post", **kwargs):
        send = getattr(requests, method)
        return send(url, headers=headers, auth=auth, data=body, **kwargs)

    @abstractmethod
    def handle_200(self, response):
        """
        Handle response type 200 (success).
        But it still may contain errors from the carrier webservice.
        """
        pass

    @abstractmethod
    def handle_500(self, response):
        """Handle reponse in case of ERROR 500 type."""
        pass

    def handle_response(self, response):
        """Handle response of webservice."""
        str_status_code = "%s" % response.status_code
        if hasattr(self, "handle_%d" % response.status_code):
            handle = getattr(self, "handle_%d" % response.status_code)
        elif hasattr(self, "handle_%sXX" % str_status_code[0]):
            handle = getattr(self, "handle_%sXX" % str_status_code[0])
        else:

            raise CarrierError(