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

Changed client API to support proxies. #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions newsapi/newsapi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ class NewsApiClient(object):
:type session: `requests.Session <https://2.python-requests.org/en/master/user/advanced/#session-objects>`_ or None
"""

def __init__(self, api_key, session=None):
def __init__(self, api_key, session=None, proxy=None):
self.auth = NewsApiAuth(api_key=api_key)
if session is None:
self.request_method = requests
else:
self.request_method = session
self.proxy = proxy

def send_request(self, url, payload):
# Helper method to send requests with proxy support if available
if self.proxy:
# Set up proxy configuration for the request
proxies = {
"http": self.proxy,
"https": self.proxy,
}
r = self.request_method.get(url, auth=self.auth, timeout=30, params=payload, proxies=proxies)
else:
r = self.request_method.get(url, auth=self.auth, timeout=30, params=payload)
return r

def get_top_headlines( # noqa: C901
self, q=None, qintitle=None, sources=None, language="en", country=None, category=None, page_size=None, page=None
Expand Down Expand Up @@ -164,7 +178,7 @@ def get_top_headlines( # noqa: C901
raise TypeError("page param should be an int")

# Send Request
r = self.request_method.get(const.TOP_HEADLINES_URL, auth=self.auth, timeout=30, params=payload)
r = self.send_request(const.TOP_HEADLINES_URL, payload)

# Check Status of Request
if r.status_code != requests.codes.ok:
Expand Down Expand Up @@ -329,7 +343,7 @@ def get_everything( # noqa: C901
raise TypeError("page param should be an int")

# Send Request
r = self.request_method.get(const.EVERYTHING_URL, auth=self.auth, timeout=30, params=payload)
r = self.send_request(const.EVERYTHING_URL, payload)

# Check Status of Request
if r.status_code != requests.codes.ok:
Expand Down Expand Up @@ -392,7 +406,7 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
raise TypeError("category param should be of type str")

# Send Request
r = self.request_method.get(const.SOURCES_URL, auth=self.auth, timeout=30, params=payload)
r = self.send_request(const.SOURCES_URL, payload)

# Check Status of Request
if r.status_code != requests.codes.ok:
Expand Down