Exemplo n.º 1
0
def urlsplit(url):
    """Similar to stdlib urlparse.urlsplit, but splits the url into more parts.

    url -- string url to be parsed.
    return -- a yelp.uri.SplitResult
    """
    url = _urlparse.urlsplit(
        from_bytes(url) if isinstance(url, six.binary_type) else url
    )
    nl = netlocsplit(url.netloc)
    return SplitResult(url.scheme, nl.username, nl.password, nl.hostname, nl.port, url.path, url.query, url.fragment)
Exemplo n.º 2
0
def urlsplit(url):
    """Similar to stdlib urlparse.urlsplit, but splits the url into more parts.

    url -- string url to be parsed.
    return -- a yelp.uri.SplitResult
    """
    url = _urlparse.urlsplit(
        from_bytes(url) if isinstance(url, six.binary_type) else url)
    nl = netlocsplit(url.netloc)
    return SplitResult(url.scheme, nl.username, nl.password, nl.hostname,
                       nl.port, url.path, url.query, url.fragment)
Exemplo n.º 3
0
def _encode(string, encoding='UTF-8', expected='', quoted=True):
    if string is None:
        return string
    string = from_bytes(string)

    if encoding:
        string = string.encode(encoding)
        if quoted:
            string = quote(string, expected)
        string = string.decode('ASCII')

    return string
Exemplo n.º 4
0
def _encode(string, encoding='UTF-8', expected='', quoted=True):
    if string is None:
        return string
    string = from_bytes(string)

    if encoding:
        string = string.encode(encoding)
        if quoted:
            string = quote(string, expected)
        string = string.decode('ASCII')

    return string
Exemplo n.º 5
0
    def request(
        self,
        request_params: MutableMapping[str, Any],
        operation: Optional[Operation] = None,
        request_config: Optional[RequestConfig] = None,
    ) -> HttpFuture:
        """Sets up the request params for aiohttp and executes the request in the background.

        :param request_params: request parameters for the http request.
        :param operation: operation that this http request is for. Defaults
            to None - in which case, we're obviously just retrieving a Swagger
            Spec.
        :param request_config:RequestConfig request_config: Per-request config that is passed to
            :class:`bravado.http_future.HttpFuture`.

        :rtype: :class: `bravado_core.http_future.HttpFuture`
        """

        orig_data = request_params.get("data", {})
        if isinstance(orig_data, Mapping):
            data = FormData()
            for name, value in orig_data.items():
                str_value = (
                    str(value) if not is_list_like(value) else [str(v) for v in value]
                )
                data.add_field(name, str_value)
        else:
            data = orig_data

        if isinstance(data, FormData):
            for name, file_tuple in request_params.get("files", {}):
                stream_obj = file_tuple[1]
                data.add_field(name, stream_obj, filename=file_tuple[0])

        params = self.prepare_params(request_params.get("params"))

        connect_timeout = request_params.get("connect_timeout")  # type: Optional[float]
        request_timeout = request_params.get("timeout")  # type: Optional[float]
        # mypy thinks the type of total and connect is float, even though it is Optional[float]. Let's ignore the error.
        timeout = (
            aiohttp.ClientTimeout(total=request_timeout, connect=connect_timeout)
            if (connect_timeout or request_timeout)
            else None
        )

        follow_redirects = request_params.get("follow_redirects", False)

        # aiohttp always adds a Content-Type header, and this breaks some servers that don't
        # expect it for non-POST/PUT requests: https://github.com/aio-libs/aiohttp/issues/457
        skip_auto_headers = (
            ["Content-Type"]
            if request_params.get("method") not in ["POST", "PUT"]
            else None
        )

        coroutine = self.client_session.request(
            method=request_params.get("method") or "GET",
            url=cast(str, request_params.get("url", "")),
            params=params,
            data=data,
            headers={
                # Convert not string headers to string
                k: from_bytes(v) if isinstance(v, bytes) else str(v)
                for k, v in request_params.get("headers", {}).items()
            },
            allow_redirects=follow_redirects,
            skip_auto_headers=skip_auto_headers,
            timeout=timeout,
            **self._get_ssl_params()
        )

        future = self.run_coroutine_func(coroutine, loop=self.loop)

        return self.bravado_future_class(
            self.future_adapter(future),
            self.response_adapter(loop=self.loop),
            operation,
            request_config=request_config,
        )
Exemplo n.º 6
0
def test_windows_roundtrip(value):
    assert from_bytes(value, 'windows-1252') == to_bytes(
        value, 'windows-1252').decode('windows-1252')
Exemplo n.º 7
0
def test_utf8_roundtrip(value):
    assert from_bytes(value, 'utf8') == to_bytes(value, 'utf8').decode('utf8')
Exemplo n.º 8
0
def test_internet_roundtrip(value):
    assert from_bytes(value) == to_bytes(value).decode('internet')
Exemplo n.º 9
0
def test_from_bytes_with_win1252able_object():
    expected = UNICODE.win1252 if PY2 else repr(win1252able)
    assert expected == from_bytes(win1252able)
Exemplo n.º 10
0
def test_with_win1252():
    win1252 = UNICODE.utf8.encode('windows-1252', 'ignore')
    assert UNICODE.win1252.encode('windows-1252') == win1252
    assert UNICODE.win1252 == from_bytes(win1252)
Exemplo n.º 11
0
def test_from_bytes_with_win1252able_object():
    expected = UNICODE.win1252 if PY2 else repr(win1252able)
    assert expected == from_bytes(win1252able)
Exemplo n.º 12
0
def test_to_native_with_byte_string(value):  # pragma: no cover
    if PY2:
        assert to_native(value) == value
    else:
        assert to_native(value) == from_bytes(value)
Exemplo n.º 13
0
def test_internet_roundtrip(value):
    assert from_bytes(value) == to_bytes(value).decode('internet')
Exemplo n.º 14
0
def test_with_win1252():
    win1252 = UNICODE.utf8.encode('windows-1252', 'ignore')
    assert UNICODE.win1252.encode('windows-1252') == win1252
    assert UNICODE.win1252 == from_bytes(win1252)
Exemplo n.º 15
0
def test_to_native_with_byte_string(value):  # pragma: no cover
    if PY2:
        assert to_native(value) == value
    else:
        assert to_native(value) == from_bytes(value)
Exemplo n.º 16
0
def test_windows_roundtrip(value):
    assert from_bytes(value, 'windows-1252') == to_bytes(value, 'windows-1252').decode('windows-1252')
Exemplo n.º 17
0
def test_utf8_roundtrip(value):
    assert from_bytes(value, 'utf8') == to_bytes(value, 'utf8').decode('utf8')