Overwrite a module type definition

Viewed 31

Say I was using a 3rd party API that used the requests module. Like:

import requests

class ThirdPartyAPI(object):
    '''
    A pretend API
    '''
    def __init__(self):
        self._session = requests.Session()

And I wanted said API to use the FuturesSession form the requets-futures module with out editing the API source code

Would I have to subclass ThirdPartyAPI like so?

from requests_futures.sessions import FuturesSession

class SubAPI(ThirdPartyAPI):
    '''
    Dependency inject a FuturesRequets session
    '''
    def __init__(self):
        super().__init__()
        self._session = FuturesSession()

This doesn't seem to be the best way. As in the above example _session gets written to twice even though I only want the object that is assigned the second time.

How is this normally dealt with? Or the best way to do it?

P.S: I'm aware this is just the tip of the ice-berg for implementing async-requsts into a third party API. As all responses would then become future objects. (one step at a tine)

1 Answers
Related