Beispiel #1
0
    def request(self, method, url, post_data=None):
        s = util.StringIO.StringIO()
        curl = pycurl.Curl()

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER,
                    ['%s: %s' % (k, v) for k, v in self.headers.iteritems()])
        if self._verify_ssl_certs:
            curl.setopt(pycurl.CAINFO, certs_path())
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error:
            e = util.exception_as()
            self._handle_request_error(e)
        rbody = s.getvalue()
        rcode = curl.getinfo(pycurl.RESPONSE_CODE)
        return rbody, rcode
Beispiel #2
0
    def request(self, method, url, post_data=None):
        s = util.StringIO.StringIO()
        curl = pycurl.Curl()

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER, ['%s: %s' % (k, v)
                    for k, v in self.headers.iteritems()])
        if self._verify_ssl_certs:
            curl.setopt(pycurl.CAINFO, certs_path())
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error:
            e = util.exception_as()
            self._handle_request_error(e)
        rbody = s.getvalue()
        rcode = curl.getinfo(pycurl.RESPONSE_CODE)
        return rbody, rcode
Beispiel #3
0
    def request(self, method, url, post_data=None):
        kwargs = {}

        if self._verify_ssl_certs:
            kwargs['verify'] = certs_path()
        else:
            kwargs['verify'] = False

        try:
            try:
                result = requests.request(method,
                                          url,
                                          headers=self.headers,
                                          data=post_data,
                                          timeout=80,
                                          **kwargs)
            except TypeError:
                e = util.exception_as()
                raise TypeError(
                    'Warning: It looks like your installed version of the '
                    '"requests" library is not compatible with Stripe\'s '
                    'usage thereof. (HINT: The most likely cause is that '
                    'your "requests" library is out of date. You can fix '
                    'that by running "pip install -U requests".) The '
                    'underlying error was: %s' % (e, ))

            # This causes the content to actually be read, which could cause
            # e.g. a socket timeout. TODO: The other fetch methods probably
            # are succeptible to the same and should be updated.
            content = result.content
            status_code = result.status_code
        except Exception:
            # Would catch just requests.exceptions.RequestException, but can
            # also raise ValueError, RuntimeError, etc.
            e = util.exception_as()
            self._handle_request_error(e)
        if sys.version_info >= (3, 0):
            content = content.decode('utf-8')
        return content, status_code
Beispiel #4
0
    def request(self, method, url, post_data=None):
        kwargs = {}

        if self._verify_ssl_certs:
            kwargs['verify'] = certs_path()
        else:
            kwargs['verify'] = False

        try:
            try:
                result = requests.request(method,
                                          url,
                                          headers=self.headers,
                                          data=post_data,
                                          timeout=80,
                                          **kwargs)
            except TypeError:
                e = util.exception_as()
                raise TypeError(
                    'Warning: It looks like your installed version of the '
                    '"requests" library is not compatible with Stripe\'s '
                    'usage thereof. (HINT: The most likely cause is that '
                    'your "requests" library is out of date. You can fix '
                    'that by running "pip install -U requests".) The '
                    'underlying error was: %s' % (e,))

            # This causes the content to actually be read, which could cause
            # e.g. a socket timeout. TODO: The other fetch methods probably
            # are succeptible to the same and should be updated.
            content = result.content
            status_code = result.status_code
        except Exception:
            # Would catch just requests.exceptions.RequestException, but can
            # also raise ValueError, RuntimeError, etc.
            e = util.exception_as()
            self._handle_request_error(e)
        if sys.version_info >= (3, 0):
            content = content.decode('utf-8')
        return content, status_code
Beispiel #5
0
    def request(self, method, url, post_data=None):
        if sys.version_info >= (3, 0) and isinstance(post_data, str):
            post_data = post_data.encode('utf-8')

        req = urllib_request.Request(url, post_data, self.headers)

        if method not in ('get', 'post'):
            req.get_method = lambda: method.upper()

        try:
            response = urllib_request.urlopen(req)
            rbody = response.read()
            rcode = response.code
        except urllib_request.HTTPError:
            e = util.exception_as()
            rcode = e.code
            rbody = e.read()
        except (urllib_request.URLError, ValueError, httplib.HTTPException):
            e = util.exception_as()
            self._handle_request_error(e)
        if sys.version_info >= (3, 0):
            rbody = rbody.decode('utf-8')
        return rbody, rcode
Beispiel #6
0
    def request(self, method, url, post_data=None):
        if sys.version_info >= (3, 0) and isinstance(post_data, str):
            post_data = post_data.encode('utf-8')

        req = urllib_request.Request(url, post_data, self.headers)

        if method not in ('get', 'post'):
            req.get_method = lambda: method.upper()

        try:
            response = urllib_request.urlopen(req)
            rbody = response.read()
            rcode = response.code
        except urllib_request.HTTPError:
            e = util.exception_as()
            rcode = e.code
            rbody = e.read()
        except (urllib_request.URLError, ValueError):
            e = util.exception_as()
            self._handle_request_error(e)
        if sys.version_info >= (3, 0):
            rbody = rbody.decode('utf-8')
        return rbody, rcode
Beispiel #7
0
 def fetch_s3_resource(self, url):
     try:
         content, status_code = self.http_client.request("get", url)
     except error.HTTPConnectionError:
         err = util.exception_as()
         msg = ("There was an error while connecting to fetch "
                "the url %s. Please check your connectivity. If there "
                "continues to be an issue, please let us know at "
                "[email protected]. The specific error is:\n" % (url,))
         raise error.StripeError(msg + str(err))
     if status_code == 200:
         return content
     elif status_code == 403:
         msg = ("We received a 403 while fetching the url %s. "
                "This probably means that you are trying to get "
                "something that doesn't actually exist." % (url,))
         raise error.StripeError(msg)
     else:
         msg = ("We received the unexpected response code %i while "
                "fetching the url %s." % (status_code, url,))
         raise error.StripeError(msg)