Exemplo n.º 1
0
def make_twilio_request(method, uri, **kwargs):
    """
    Make a request to Twilio. Throws an error
    """
    headers = kwargs.get("headers", {})
    headers["User-Agent"] = "twilio-python/%s" % twilio.__version__

    if method == "POST" and "Content-Type" not in headers:
        headers["Content-Type"] = "application/x-www-form-urlencoded"

    kwargs["headers"] = headers

    if "Accept" not in headers:
        headers["Accept"] = "application/json"
        uri = uri + ".json"

    resp = make_request(method, uri, **kwargs)

    if not resp.ok:
        try:
            error = json.loads(resp.content)
            message = "%s: %s" % (error["code"], error["message"])
        except:
            message = resp.content

        raise TwilioRestException(resp.status_code, resp.url, message)

    return resp
Exemplo n.º 2
0
def make_twilio_request(method, uri, **kwargs):
    """
    Make a request to Twilio. Throws an error
    """
    headers = kwargs.get("headers", {})
    headers["User-Agent"] = "twilio-python/%s" % twilio.__version__
    headers["Accept-Charset"] = "utf-8"

    if method == "POST" and "Content-Type" not in headers:
        headers["Content-Type"] = "application/x-www-form-urlencoded"

    kwargs["headers"] = headers

    if "Accept" not in headers:
        headers["Accept"] = "application/json"
        uri += ".json"

    resp = make_request(method, uri, **kwargs)

    if not resp.ok:
        try:
            error = json.loads(resp.content)
            code = error["code"]
            message = "%s: %s" % (code, error["message"])
        except:
            code = None
            message = resp.content

        raise TwilioRestException(resp.status_code, resp.url, message, code)

    return resp
Exemplo n.º 3
0
    def request(self, method, uri, **kwargs):
        """
        Send an HTTP request to the resource.

        Raise a TwilioRestException
        """
        resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)

        logging.debug(resp.content)

        if method == "DELETE":
            return resp, {}
        else:
            return resp, json.loads(resp.content)
Exemplo n.º 4
0
    def request(self, method, uri, **kwargs):
        """
        Send an HTTP request to the resource.

        Raise a TwilioRestException
        """
        resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)

        logging.debug(resp.content)

        if method == "DELETE":
            return resp, {}
        else:
            return resp, json.loads(resp.content)
Exemplo n.º 5
0
    def request(self, method, uri, **kwargs):
        """
        Send an HTTP request to the resource.

        :raises: a :exc:`~twilio.TwilioRestException`
        """
        if 'timeout' not in kwargs and self.timeout is not UNSET_TIMEOUT:
            kwargs['timeout'] = self.timeout
        resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)

        logging.debug(resp.content)

        if method == "DELETE":
            return resp, {}
        else:
            return resp, json.loads(resp.content)
Exemplo n.º 6
0
    def request(self, method, uri, **kwargs):
        """
        Send an HTTP request to the resource.

        Raise a TwilioRestException
        """
        if 'timeout' not in kwargs and self.timeout is not UNSET_TIMEOUT:
            kwargs['timeout'] = self.timeout
        resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)

        logging.debug(resp.content)

        if method == "DELETE":
            return resp, {}
        else:
            return resp, json.loads(resp.content)
Exemplo n.º 7
0
def make_twilio_request(method, uri, **kwargs):
    """
    Make a request to Twilio. Throws an error

    :return: a requests-like HTTP response
    :rtype: :class:`RequestsResponse`
    :raises TwilioRestException: if the response is a 400
        or 500-level response.
    """
    headers = kwargs.get("headers", {})
    headers["User-Agent"] = "twilio-python/%s" % twilio.__version__
    headers["Accept-Charset"] = "utf-8"

    if method == "POST" and "Content-Type" not in headers:
        headers["Content-Type"] = "application/x-www-form-urlencoded"

    kwargs["headers"] = headers

    if "Accept" not in headers:
        headers["Accept"] = "application/json"
        uri += ".json"

    resp = make_request(method, uri, **kwargs)

    if not resp.ok:
        try:
            error = json.loads(resp.content)
            code = error["code"]
            message = "%s: %s" % (code, error["message"])
        except:
            code = None
            message = resp.content

        def red(msg):
            return u("\033[31m\033[49m%s\033[0m") % msg

        def white(msg):
            return u("\033[37m\033[49m%s\033[0m") % msg

        def blue(msg):
            return u("\033[34m\033[49m%s\033[0m") % msg

        def orange(msg):
            return u("\033[33m\033[49m%s\033[0m") % msg

        def teal(msg):
            return u("\033[36m\033[49m%s\033[0m") % msg

        # If it makes sense to print a human readable error message, try to do
        # it. The one problem is that someone might catch this error and try to
        # display the message from it to an end user.
        if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
            msg = red("\nHTTP Error. ")
            msg += white("Your request was:\n\n")
            msg += teal("%s %s" % (method, uri))
            msg += white("\n\nTwilio returned the following information:")
            msg += blue("\n\n" + str(message) + "\n")
            if code:
                msg += white("\nMore information may be available here:\n\n")
                msg += blue("https://www.twilio.com/docs/errors/%s" % code)
                msg += "\n\n"
        else:
            msg = message

        raise TwilioRestException(resp.status_code, resp.url, msg, code)

    return resp
Exemplo n.º 8
0
def make_twilio_request(method, uri, **kwargs):
    """
    Make a request to Twilio. Throws an error

    :return: a requests-like HTTP response
    :rtype: :class:`RequestsResponse`
    :raises TwilioRestException: if the response is a 400
        or 500-level response.
    """
    headers = kwargs.get("headers", {})

    user_agent = "twilio-python/%s (Python %s)" % (
        twilio.__version__,
        platform.python_version(),
    )
    headers["User-Agent"] = user_agent
    headers["Accept-Charset"] = "utf-8"

    if method == "POST" and "Content-Type" not in headers:
        headers["Content-Type"] = "application/x-www-form-urlencoded"

    kwargs["headers"] = headers

    if "Accept" not in headers:
        headers["Accept"] = "application/json"
        uri += ".json"

    resp = make_request(method, uri, **kwargs)

    if not resp.ok:
        try:
            error = json.loads(resp.content)
            code = error["code"]
            message = "%s: %s" % (code, error["message"])
        except:
            code = None
            message = resp.content

        def red(msg):
            return u("\033[31m\033[49m%s\033[0m") % msg

        def white(msg):
            return u("\033[37m\033[49m%s\033[0m") % msg

        def blue(msg):
            return u("\033[34m\033[49m%s\033[0m") % msg

        def orange(msg):
            return u("\033[33m\033[49m%s\033[0m") % msg

        def teal(msg):
            return u("\033[36m\033[49m%s\033[0m") % msg

        # If it makes sense to print a human readable error message, try to do
        # it. The one problem is that someone might catch this error and try to
        # display the message from it to an end user.
        if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
            msg = red("\nHTTP Error. ")
            msg += white("Your request was:\n\n")
            msg += teal("%s %s" % (method, uri))
            msg += white("\n\nTwilio returned the following information:")
            msg += blue("\n\n" + str(message) + "\n")
            if code:
                msg += white("\nMore information may be available here:\n\n")
                msg += blue("https://www.twilio.com/docs/errors/%s" % code)
                msg += "\n\n"
        else:
            msg = message

        raise TwilioRestException(resp.status_code, resp.url, msg, code)

    return resp