Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxies or requests session to constructor #71

Open
amarvin opened this issue Aug 27, 2019 · 2 comments
Open

Add proxies or requests session to constructor #71

amarvin opened this issue Aug 27, 2019 · 2 comments
Assignees

Comments

@amarvin
Copy link

amarvin commented Aug 27, 2019

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.

@amarvin amarvin changed the title Add requests session to constructor Add proxies or requests session to constructor Aug 29, 2019
@ljluestc
Copy link

  1. Modify CurrencyRates in the forex_python.converter module:
import requests
from forex_python.converter import CurrencyRates as BaseCurrencyRates

class CurrencyRates(BaseCurrencyRates):
    def __init__(self, session=None, proxies=None, verify_ssl=True):
        super().__init__()
        if session is None:
            session = requests.Session()
        if proxies is not None:
            session.proxies = proxies
        session.verify = verify_ssl
        self.session = session

    # ... rest of the class methods ...

# This import ensures that the class is available when the module is imported
CurrencyRates()
  1. Usage:
from forex_python.converter import CurrencyRates

# Create a session with proxy settings and SSL verification turned off
proxies = {
    '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.

@amarvin
Copy link
Author

amarvin commented Aug 27, 2023

  1. Modify CurrencyRates in the forex_python.converter module:
import requests
from forex_python.converter import CurrencyRates as BaseCurrencyRates

class CurrencyRates(BaseCurrencyRates):
    def __init__(self, session=None, proxies=None, verify_ssl=True):
        super().__init__()
        if session is None:
            session = requests.Session()
        if proxies is not None:
            session.proxies = proxies
        session.verify = verify_ssl
        self.session = session

    # ... rest of the class methods ...

# This import ensures that the class is available when the module is imported
CurrencyRates()
  1. Usage:
from forex_python.converter import CurrencyRates

# Create a session with proxy settings and SSL verification turned off
proxies = {
    '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.

Great workaround, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants