nautobot / pynautobot

Nautobot Python SDK
https://pynautobot.readthedocs.io/en/latest/index.html
Apache License 2.0
38 stars 33 forks source link

Proxy feature #201

Open TitouanS31 opened 5 months ago

TitouanS31 commented 5 months ago

It would be great to have a proxy option for api calls. We could hence do something like this.

proxies = {
    "http": "http://my_super_proxy.com:1234",
    "https": "https://my_super_proxy.com:1234",
}

nautobot = pynautobot.api(
    url = "https://demo.nautobot.com",
    token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    proxies = proxies
)
glennmatthews commented 5 months ago

As a workaround, you can probably set the standard HTTP_PROXY and HTTPS_PROXY environment variables.

joewesch commented 5 months ago

This should already be possible by modifying the http session directly after you instantiate the API class in the same way you modify headers or other items.

nautobot = pynautobot.api(
    url = "https://demo.nautobot.com",
    token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
nautobot.http_session.proxies = {
    "http": "http://my_super_proxy.com:1234",
    "https": "https://my_super_proxy.com:1234",
}
TitouanS31 commented 5 months ago

I though modifying the http session after the API instanciation would do the trick... but the api validation fails before exiting the call. I could catch the error and patch the session after all but I think that is not very clean. For now, I have just added 2 lines to the API __init__ function to handle the proxy.

joewesch commented 5 months ago

Yes, I thought of that after giving you that update. We really need to change that auto-validation to happen during your first API call rather than on instantiation.