cisco-en-programmability / dnacentersdk

Cisco DNA Center Python SDK
https://dnacentersdk.readthedocs.io/en/latest/
MIT License
70 stars 31 forks source link

Support injection of requests.Session object #149

Open andrewschenck opened 4 months ago

andrewschenck commented 4 months ago
from dnacentersdk import DNACenterAPI
from requests import session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

if __name__ == '__main__':

    retries = Retry(
        total=20,
        backoff_factor=0.1,
        status_forcelist=(400, 429, 500, 502, 503, 504),
    )
    http = session()
    http.mount('https://', HTTPAdapter(max_retries=retries))

    api = DNACenterAPI(
        base_url='https://dnac.mydomain.com',
        username=USERNAME,
        password=PASSWORD,
        session=http,
    )

    print(api.devices.get_device_list())

We need to be able to inject our own requests.Session object in order to tweak behavior, such as above. Right now, we're monkey-patching the Session object. It would be nice to support injection as a native feature.

I have staged you a pull request for tested code which supports this injection pattern; the snippet above works with the code included in https://github.com/cisco-en-programmability/dnacentersdk/pull/148