Exemple #1
0
    def _invoke_request(self, p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        if base_origin:
            url = p_options.pn_config.scheme(
            ) + "://" + base_origin + e_options.path
        else:
            url = e_options.path

        if e_options.request_headers:
            p_options.update(e_options.request_headers)

        args = {
            "method": e_options.method_string,
            "headers": p_options.headers,
            "url": url,
            "params": e_options.query_string,
            "timeout": (e_options.connect_timeout, e_options.request_timeout),
            "allow_redirects": e_options.allow_redirects
        }

        if e_options.is_post() or e_options.is_patch():
            args["data"] = e_options.data
            args["files"] = e_options.files
            logger.debug(
                "%s %s %s" %
                (e_options.method_string,
                 utils.build_url(p_options.pn_config.scheme(), base_origin,
                                 e_options.path,
                                 e_options.query_string), e_options.data))
        else:
            logger.debug(
                "%s %s" %
                (e_options.method_string,
                 utils.build_url(p_options.pn_config.scheme(), base_origin,
                                 e_options.path, e_options.query_string)))

        try:
            res = self.session.request(**args)
            logger.debug("GOT %s" % res.text)
        except requests.exceptions.ConnectionError as e:
            raise PubNubException(pn_error=PNERR_CONNECTION_ERROR,
                                  errormsg=str(e))
        except requests.exceptions.HTTPError as e:
            raise PubNubException(pn_error=PNERR_HTTP_ERROR, errormsg=str(e))
        except requests.exceptions.Timeout as e:
            raise PubNubException(pn_error=PNERR_CLIENT_TIMEOUT,
                                  errormsg=str(e))
        except requests.exceptions.TooManyRedirects as e:
            raise PubNubException(pn_error=PNERR_TOO_MANY_REDIRECTS_ERROR,
                                  errormsg=str(e))
        except Exception as e:
            raise PubNubException(pn_error=PNERR_UNKNOWN_ERROR,
                                  errormsg=str(e))

        return res
Exemple #2
0
    def test_build_url(self):
        def match(expected_str, actual_str):
            expected = urlparse(expected_str)
            actual = urlparse(actual_str)
            assert expected.scheme == actual.scheme
            assert expected.netloc == actual.netloc
            assert expected.path == actual.path
            self.assertEqual(parse_qs(expected.query), parse_qs(actual.query))

        match("http://ex.com/news?a=2&b=qwer",
              build_url("http", "ex.com", "/news", "a=2&b=qwer"))
        match("https://ex.com/?a=2&b=qwer",
              build_url("https", "ex.com", "/", "a=2&b=qwer"))
Exemple #3
0
    def test_build_url(self):
        def match(expected_str, actual_str):
            expected = urlparse(expected_str)
            actual = urlparse(actual_str)
            assert expected.scheme == actual.scheme
            assert expected.netloc == actual.netloc
            assert expected.path == actual.path
            self.assertEqual(parse_qs(expected.query), parse_qs(actual.query))

        match("http://ex.com/news?a=2&b=qwer",
              build_url("http", "ex.com", "/news", "a=2&b=qwer"))
        match("https://ex.com/?a=2&b=qwer",
              build_url("https", "ex.com", "/", "a=2&b=qwer"))
Exemple #4
0
    def _invoke_request(p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        url = utils.build_url(p_options.pn_config.scheme(), base_origin,
                              e_options.path, e_options.query_string)

        args = {
            "method": e_options.method_string,
            'headers': p_options.headers,
            "url": url,
            'params': e_options.query_string,
            'timeout': (e_options.connect_timeout, e_options.request_timeout)
        }

        if e_options.is_post():
            args['data'] = e_options.data
            logger.debug("%s %s %s" % (e_options.method_string, url, e_options.data))
        else:
            logger.debug("%s %s" % (e_options.method_string, url))

        try:
            req = urllib.request.Request(url, e_options.data, p_options.headers)
            res = urllib.request.urlopen(req)
        except urllib.error.URLError as e:
            # For Python 2.6
            if isinstance(e.reason, socket.timeout):
                raise PubNubException(
                    pn_error=PNERR_CLIENT_TIMEOUT,
                    errormsg=str(e)
                )
            else:
                # TODO: wrap
                raise

        except urllib.error.HTTPError as e:
            raise PubNubException(
                pn_error=PNERR_HTTP_ERROR,
                errormsg=str(e)
            )
        except socket.timeout as e:
            raise PubNubException(
                pn_error=PNERR_CLIENT_TIMEOUT,
                errormsg=str(e)
            )
        except Exception as e:
            raise PubNubException(
                pn_error=PNERR_UNKNOWN_ERROR,
                errormsg=str(e)
            )

        return res
Exemple #5
0
    def _invoke_request(p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        url = utils.build_url(p_options.pn_config.scheme(), base_origin,
                              e_options.path, e_options.query_string)

        args = {
            "method": e_options.method_string,
            'headers': p_options.headers,
            "url": url,
            'params': e_options.query_string,
            'timeout': (e_options.connect_timeout, e_options.request_timeout)
        }

        if e_options.is_post():
            args['data'] = e_options.data
            logger.debug("%s %s %s" % (e_options.method_string, url, e_options.data))
        else:
            logger.debug("%s %s" % (e_options.method_string, url))

        try:
            req = urllib.request.Request(url, e_options.data, p_options.headers)
            res = urllib.request.urlopen(req)
        except urllib.error.URLError as e:
            # For Python 2.6
            if isinstance(e.reason, socket.timeout):
                raise PubNubException(
                    pn_error=PNERR_CLIENT_TIMEOUT,
                    errormsg=str(e)
                )
            else:
                # TODO: wrap
                raise

        except urllib.error.HTTPError as e:
            raise PubNubException(
                pn_error=PNERR_HTTP_ERROR,
                errormsg=str(e)
            )
        except socket.timeout as e:
            raise PubNubException(
                pn_error=PNERR_CLIENT_TIMEOUT,
                errormsg=str(e)
            )
        except Exception as e:
            raise PubNubException(
                pn_error=PNERR_UNKNOWN_ERROR,
                errormsg=str(e)
            )

        return res
Exemple #6
0
    def _invoke_request(self, p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        url = p_options.pn_config.scheme() + "://" + base_origin + e_options.path

        args = {
            "method": e_options.method_string,
            'headers': p_options.headers,
            "url": url,
            'params': e_options.query_string,
            'timeout': (e_options.connect_timeout, e_options.request_timeout)
        }

        if e_options.is_post():
            args['data'] = e_options.data
            logger.debug("%s %s %s" % (
                e_options.method_string,
                utils.build_url(
                    p_options.pn_config.scheme(),
                    base_origin,
                    e_options.path,
                    e_options.query_string), e_options.data))
        else:
            logger.debug("%s %s" % (
                e_options.method_string,
                utils.build_url(
                    p_options.pn_config.scheme(),
                    base_origin,
                    e_options.path,
                    e_options.query_string)))

        try:
            res = self.session.request(**args)
            logger.debug("GOT %s" % res.text)
        except requests.exceptions.ConnectionError as e:
            raise PubNubException(
                pn_error=PNERR_CONNECTION_ERROR,
                errormsg=str(e)
            )
        except requests.exceptions.HTTPError as e:
            raise PubNubException(
                pn_error=PNERR_HTTP_ERROR,
                errormsg=str(e)
            )
        except requests.exceptions.Timeout as e:
            raise PubNubException(
                pn_error=PNERR_CLIENT_TIMEOUT,
                errormsg=str(e)
            )
        except requests.exceptions.TooManyRedirects as e:
            raise PubNubException(
                pn_error=PNERR_TOO_MANY_REDIRECTS_ERROR,
                errormsg=str(e)
            )
        except Exception as e:
            raise PubNubException(
                pn_error=PNERR_UNKNOWN_ERROR,
                errormsg=str(e)
            )

        return res