rbw / aiosnow

Asynchronous ServiceNow Library
MIT License
73 stars 12 forks source link

How to use proxy with aiosnow? #103

Open SergioBlnc opened 3 years ago

SergioBlnc commented 3 years ago

I guess I have to configure it with aiohttp, but I don't see the option in aiosnow client module

rbw commented 3 years ago

Currently, you'll need to use a custom aiohttp.ClientSession. Something like this should work:

class ProxySession(aiohttp.ClientSession):
    def __init__(self, *args, **kwargs):
        super(ProxySession, self).__init__(*args, trust_env=True, **kwargs)

client = aiosnow.Client(session_cls=ProxySession)
...

The proxy information is then taken from the HTTP_PROXY or HTTPS_PROXY environment variables.

If authentication is required, credentials are read from the ~/.netrc file: https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html

I'll look into simplifying this.

SergioBlnc commented 3 years ago

Many thanks Rob!!

jonn26 commented 3 years ago

I found the following works if you only want to use a proxy for aiosnow and not set the HTTP_PROXY or HTTPS_PROXY environment variables:

class ProxySession(aiohttp.ClientSession):
    async def _request(self, *args, **kwargs):
        return await super()._request(*args, proxy="http://proxy.com", **kwargs)

client = aiosnow.Client(session_cls=ProxySession)
...