예제 #1
0
    def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
                allow_redirects=False):
        """
        Make an HTTP Request with parameters provided.

        :param str method: The HTTP method to use
        :param str url: The URL to request
        :param dict params: Query parameters to append to the URL
        :param dict data: Parameters to go in the body of the HTTP request
        :param dict headers: HTTP Headers to send with the request
        :param tuple auth: Basic Auth arguments
        :param float timeout: Socket/Read timeout for the request
        :param boolean allow_redirects: Whether or not to allow redirects
        See the requests documentation for explanation of all these parameters

        :return: An http response
        :rtype: A :class:`Response <twilio.rest.http.response.Response>` object
        """
        if timeout is not None and timeout <= 0:
            raise ValueError(timeout)

        kwargs = {
            'method': method.upper(),
            'url': url,
            'params': params,
            'data': data,
            'headers': headers,
            'auth': auth,
            'hooks': self.request_hooks
        }

        if params:
            self.logger.info('{method} Request: {url}?{query}'.format(query=urlencode(params), **kwargs))
            self.logger.info('PARAMS: {params}'.format(**kwargs))
        else:
            self.logger.info('{method} Request: {url}'.format(**kwargs))
        if data:
            self.logger.info('PAYLOAD: {data}'.format(**kwargs))

        self.last_response = None
        session = self.session or Session()
        if self.proxy:
            session.proxies = self.proxy
        request = Request(**kwargs)
        self.last_request = TwilioRequest(**kwargs)

        prepped_request = session.prepare_request(request)
        response = session.send(
            prepped_request,
            allow_redirects=allow_redirects,
            timeout=timeout if timeout is not None else self.timeout,
        )

        self.logger.info('{method} Response: {status} {text}'.format(
            method=method, status=response.status_code, text=response.text)
        )

        self.last_response = Response(int(response.status_code), response.text, response.headers)

        return self.last_response
예제 #2
0
    def request(self,
                method,
                url,
                params=None,
                data=None,
                headers=None,
                auth=None,
                timeout=None,
                allow_redirects=False):
        """
        Make an HTTP Request with parameters provided.

        :param str method: The HTTP method to use
        :param str url: The URL to request
        :param dict params: Query parameters to append to the URL
        :param dict data: Parameters to go in the body of the HTTP request
        :param dict headers: HTTP Headers to send with the request
        :param tuple auth: Basic Auth arguments
        :param float timeout: Socket/Read timeout for the request
        :param boolean allow_redirects: Whether or not to allow redirects
        See the requests documentation for explanation of all these parameters

        :return: An http response
        :rtype: A :class:`Response <twilio.rest.http.response.Response>` object
        """

        self.last_response = None
        session = self.session or Session()
        request = Request(method.upper(),
                          url,
                          params=params,
                          data=data,
                          headers=headers,
                          auth=auth)
        self.last_request = TwilioRequest(method.upper(),
                                          url,
                                          auth=auth,
                                          params=params,
                                          data=data,
                                          headers=headers)

        prepped_request = session.prepare_request(request)
        response = session.send(
            prepped_request,
            allow_redirects=allow_redirects,
            timeout=timeout,
        )

        self.last_response = Response(int(response.status_code), response.text)

        return self.last_response