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

Proxy option improved; Docs added #71

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ coverage.xml
# Sphinx documentation
docs/_build/

# Jetbrains
.idea/*
28 changes: 24 additions & 4 deletions fredapi/fred.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
urlencode = url_parse.urlencode
HTTPError = url_error.HTTPError


class Fred:
earliest_realtime_start = '1776-07-04'
latest_realtime_end = '9999-12-31'
Expand All @@ -28,12 +27,22 @@ class Fred:

def __init__(self,
api_key=None,
api_key_file=None):
api_key_file=None,
proxies=None):
"""
Initialize the Fred class that provides useful functions to query the Fred dataset. You need to specify a valid
API key in one of 3 ways: pass the string via api_key, or set api_key_file to a file with the api key in the
first line, or set the environment variable 'FRED_API_KEY' to the value of your api key. You can sign up for a
free api key on the Fred website at http://research.stlouisfed.org/fred2/
first line, or set the environment variable 'FRED_API_KEY' to the value of your api key.

Parameters
----------
api_key : str
API key. A free api key can be obtained on the Fred website at http://research.stlouisfed.org/fred2/.
api_key_file : str
Path to a file containing the api key.
proxies : dict
Proxies specifications: a dictionary mapping protocol names (e.g. 'http', 'https') to proxy URLs. If not provided, environment variables 'HTTP_PROXY', 'HTTPS_PROXY' are used.

"""
self.api_key = None
if api_key is not None:
Expand All @@ -55,6 +64,17 @@ def __init__(self,
api key. You can sign up for a free api key on the Fred
website at http://research.stlouisfed.org/fred2/"""))

if not proxies:
http_proxy, https_proxy = os.getenv('HTTP_PROXY'), os.getenv('HTTPS_PROXY')
if http_proxy or https_proxy:
proxies = {'http': http_proxy, 'https': https_proxy}

self.proxies = proxies

if self.proxies:
opener = url_request.build_opener(url_request.ProxyHandler(self.proxies))
url_request.install_opener(opener)

def __fetch_data(self, url):
"""
helper function for fetching data given a request URL
Expand Down
2 changes: 1 addition & 1 deletion fredapi/tests/test_fred.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def setUp(self):
run as part of automated continuous integration (e.g. travis-ci.org).

"""
self.fred = fredapi.Fred(api_key=fred_api_key)
self.fred = fredapi.Fred(api_key=fred_api_key, proxies=None)
self.fake_fred_call = fake_fred_call
self.__original_urlopen = fredapi.fred.urlopen

Expand Down
14 changes: 14 additions & 0 deletions fredapi/tests/test_with_proxies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
""" Created on 15/04/2024::
------------- test_with_proxies.py -------------

**Authors**: L. Mingarelli
"""
import os
import fredapi
PROXIES = {'http': 'xxx',
'https': 'xxx'}

fred = fredapi.Fred(api_key=os.environ['FRED_API_KEY'], proxies=PROXIES)

data = fred.get_series('SP500')

Loading