# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.http_validation_error import HttpValidationError
from ..types.response import Response
from ..types.response_list_proxy_mapping_route import ResponseListProxyMappingRoute
from ..types.response_list_str import ResponseListStr
from ..types.response_proxy_diagnose_result import ResponseProxyDiagnoseResult
from ..types.response_proxy_mapping_route import ResponseProxyMappingRoute
from ..types.response_proxy_upstream_info import ResponseProxyUpstreamInfo
from ..types.response_union_proxy_upstream_info_none_type import ResponseUnionProxyUpstreamInfoNoneType

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawProxyClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def list_mappings(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ResponseListProxyMappingRoute]:
        """
        List all proxy domain-to-port mappings.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseListProxyMappingRoute]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/mappings",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseListProxyMappingRoute,
                    parse_obj_as(
                        type_=ResponseListProxyMappingRoute,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def add_mapping(
        self, *, source: str, target: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ResponseProxyMappingRoute]:
        """
        Add or update a proxy mapping. Supports wildcards in host (*.example.com).

        Parameters
        ----------
        source : str
            Source pattern: [protocol://]host[:port][/path], supports wildcard *

        target : str
            Target address: [host:]port[/path]. Host defaults to 127.0.0.1

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseProxyMappingRoute]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/mappings",
            method="POST",
            json={
                "source": source,
                "target": target,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyMappingRoute,
                    parse_obj_as(
                        type_=ResponseProxyMappingRoute,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def remove_mapping(
        self, source: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[Response]:
        """
        Remove a proxy mapping by source pattern.

        Parameters
        ----------
        source : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[Response]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/proxy/mappings/{jsonable_encoder(source)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def list_excludes(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ResponseListStr]:
        """
        List all proxy bypass/exclude patterns.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseListStr]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseListStr,
                    parse_obj_as(
                        type_=ResponseListStr,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def add_exclude(
        self, *, pattern: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[Response]:
        """
        Add a bypass/exclude pattern. Matched domains connect directly.

        Parameters
        ----------
        pattern : str
            Bypass pattern: domain (*.example.com, .example.com) or CIDR (10.0.0.0/8)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[Response]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="POST",
            json={
                "pattern": pattern,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def remove_exclude(
        self, *, pattern: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[Response]:
        """
        Remove a bypass/exclude pattern.

        Parameters
        ----------
        pattern : str
            Bypass pattern: domain (*.example.com, .example.com) or CIDR (10.0.0.0/8)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[Response]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="DELETE",
            json={
                "pattern": pattern,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def diagnose(
        self, *, url: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ResponseProxyDiagnoseResult]:
        """
        Diagnose how a URL would be routed through the proxy.

        Parameters
        ----------
        url : str
            URL to diagnose routing for

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseProxyDiagnoseResult]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/diagnose",
            method="GET",
            params={
                "url": url,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyDiagnoseResult,
                    parse_obj_as(
                        type_=ResponseProxyDiagnoseResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_upstream(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ResponseUnionProxyUpstreamInfoNoneType]:
        """
        Get the current upstream proxy configuration.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseUnionProxyUpstreamInfoNoneType]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseUnionProxyUpstreamInfoNoneType,
                    parse_obj_as(
                        type_=ResponseUnionProxyUpstreamInfoNoneType,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def set_upstream(
        self,
        *,
        server: str,
        auth_cmd: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ResponseProxyUpstreamInfo]:
        """
        Set or update the upstream proxy. Supports user:pass@host:port format.

        Takes effect immediately — no browser restart needed.

        Parameters
        ----------
        server : str
            Upstream proxy server. Supports plain host:port or user:pass@host:port

        auth_cmd : typing.Optional[str]
            Optional shell command to obtain proxy credentials. The command stdout should be "username:password". When set, the result is injected into the server URL, replacing any inline credentials.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseProxyUpstreamInfo]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="PUT",
            json={
                "server": server,
                "auth_cmd": auth_cmd,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyUpstreamInfo,
                    parse_obj_as(
                        type_=ResponseProxyUpstreamInfo,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def remove_upstream(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[Response]:
        """
        Remove upstream proxy (switch to direct mode).

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[Response]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawProxyClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list_mappings(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ResponseListProxyMappingRoute]:
        """
        List all proxy domain-to-port mappings.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseListProxyMappingRoute]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/mappings",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseListProxyMappingRoute,
                    parse_obj_as(
                        type_=ResponseListProxyMappingRoute,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def add_mapping(
        self, *, source: str, target: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ResponseProxyMappingRoute]:
        """
        Add or update a proxy mapping. Supports wildcards in host (*.example.com).

        Parameters
        ----------
        source : str
            Source pattern: [protocol://]host[:port][/path], supports wildcard *

        target : str
            Target address: [host:]port[/path]. Host defaults to 127.0.0.1

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseProxyMappingRoute]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/mappings",
            method="POST",
            json={
                "source": source,
                "target": target,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyMappingRoute,
                    parse_obj_as(
                        type_=ResponseProxyMappingRoute,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def remove_mapping(
        self, source: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[Response]:
        """
        Remove a proxy mapping by source pattern.

        Parameters
        ----------
        source : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[Response]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/proxy/mappings/{jsonable_encoder(source)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def list_excludes(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ResponseListStr]:
        """
        List all proxy bypass/exclude patterns.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseListStr]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseListStr,
                    parse_obj_as(
                        type_=ResponseListStr,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def add_exclude(
        self, *, pattern: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[Response]:
        """
        Add a bypass/exclude pattern. Matched domains connect directly.

        Parameters
        ----------
        pattern : str
            Bypass pattern: domain (*.example.com, .example.com) or CIDR (10.0.0.0/8)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[Response]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="POST",
            json={
                "pattern": pattern,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def remove_exclude(
        self, *, pattern: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[Response]:
        """
        Remove a bypass/exclude pattern.

        Parameters
        ----------
        pattern : str
            Bypass pattern: domain (*.example.com, .example.com) or CIDR (10.0.0.0/8)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[Response]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/excludes",
            method="DELETE",
            json={
                "pattern": pattern,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def diagnose(
        self, *, url: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ResponseProxyDiagnoseResult]:
        """
        Diagnose how a URL would be routed through the proxy.

        Parameters
        ----------
        url : str
            URL to diagnose routing for

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseProxyDiagnoseResult]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/diagnose",
            method="GET",
            params={
                "url": url,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyDiagnoseResult,
                    parse_obj_as(
                        type_=ResponseProxyDiagnoseResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_upstream(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ResponseUnionProxyUpstreamInfoNoneType]:
        """
        Get the current upstream proxy configuration.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseUnionProxyUpstreamInfoNoneType]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseUnionProxyUpstreamInfoNoneType,
                    parse_obj_as(
                        type_=ResponseUnionProxyUpstreamInfoNoneType,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def set_upstream(
        self,
        *,
        server: str,
        auth_cmd: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ResponseProxyUpstreamInfo]:
        """
        Set or update the upstream proxy. Supports user:pass@host:port format.

        Takes effect immediately — no browser restart needed.

        Parameters
        ----------
        server : str
            Upstream proxy server. Supports plain host:port or user:pass@host:port

        auth_cmd : typing.Optional[str]
            Optional shell command to obtain proxy credentials. The command stdout should be "username:password". When set, the result is injected into the server URL, replacing any inline credentials.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseProxyUpstreamInfo]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="PUT",
            json={
                "server": server,
                "auth_cmd": auth_cmd,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseProxyUpstreamInfo,
                    parse_obj_as(
                        type_=ResponseProxyUpstreamInfo,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def remove_upstream(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[Response]:
        """
        Remove upstream proxy (switch to direct mode).

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[Response]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/proxy/upstream",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    Response,
                    parse_obj_as(
                        type_=Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
