You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add proxies or requests session to constructor #71
The constructor for CurrencyRates should have an optional argument of a requests session to be used for all API calls. This may speed up code that have a large number of API calls, and would allow users to specify proxy settings.
I'd mainly just want options to specify proxy settings and turn of SSL verification. Those could instead be optional arguments to the CurrencyRates constructor, without having to switch to requests sessions.
Modify CurrencyRates in the forex_python.converter module:
importrequestsfromforex_python.converterimportCurrencyRatesasBaseCurrencyRatesclassCurrencyRates(BaseCurrencyRates):
def__init__(self, session=None, proxies=None, verify_ssl=True):
super().__init__()
ifsessionisNone:
session=requests.Session()
ifproxiesisnotNone:
session.proxies=proxiessession.verify=verify_sslself.session=session# ... rest of the class methods ...# This import ensures that the class is available when the module is importedCurrencyRates()
Usage:
fromforex_python.converterimportCurrencyRates# Create a session with proxy settings and SSL verification turned offproxies= {
'http': 'http://proxy.example.com:8080',
'https': 'https://proxy.example.com:8080'
}
currency_rates=CurrencyRates(proxies=proxies, verify_ssl=False)
# Now you can use the `currency_rates` object for your API calls
Keep in mind that this approach involves modifying the library's code directly. If you choose to go this route, make sure to take care and consider the impact on future library updates.
Alternatively, if modifying the library directly isn't desired, you could create a wrapper class that inherits from CurrencyRates and adds the desired functionality. This would allow you to achieve the desired behavior without modifying the original library code.
Modify CurrencyRates in the forex_python.converter module:
importrequestsfromforex_python.converterimportCurrencyRatesasBaseCurrencyRatesclassCurrencyRates(BaseCurrencyRates):
def__init__(self, session=None, proxies=None, verify_ssl=True):
super().__init__()
ifsessionisNone:
session=requests.Session()
ifproxiesisnotNone:
session.proxies=proxiessession.verify=verify_sslself.session=session# ... rest of the class methods ...# This import ensures that the class is available when the module is importedCurrencyRates()
Usage:
fromforex_python.converterimportCurrencyRates# Create a session with proxy settings and SSL verification turned offproxies= {
'http': 'http://proxy.example.com:8080',
'https': 'https://proxy.example.com:8080'
}
currency_rates=CurrencyRates(proxies=proxies, verify_ssl=False)
# Now you can use the `currency_rates` object for your API calls
Keep in mind that this approach involves modifying the library's code directly. If you choose to go this route, make sure to take care and consider the impact on future library updates.
Alternatively, if modifying the library directly isn't desired, you could create a wrapper class that inherits from CurrencyRates and adds the desired functionality. This would allow you to achieve the desired behavior without modifying the original library code.
Activity
[-]Add requests session to constructor[/-][+]Add proxies or requests session to constructor[/+]ljluestc commentedon Aug 27, 2023
CurrencyRatesin theforex_python.convertermodule:Keep in mind that this approach involves modifying the library's code directly. If you choose to go this route, make sure to take care and consider the impact on future library updates.
Alternatively, if modifying the library directly isn't desired, you could create a wrapper class that inherits from
CurrencyRatesand adds the desired functionality. This would allow you to achieve the desired behavior without modifying the original library code.amarvin commentedon Aug 27, 2023
Great workaround, thanks!