Skip to content

Commit 42e7d9b

Browse files
authored
Merge pull request #11 from bytesview/new_updates
added option to get response headers
2 parents 0b51aa3 + 86bff7f commit 42e7d9b

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

newsdataapi/newsdataapi_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class NewsDataApiClient(FileHandler):
1313
def __init__(
1414
self, apikey:str, session:bool= False, max_retries:int= constants.DEFAULT_MAX_RETRIES, retry_delay:int= constants.DEFAULT_RETRY_DELAY,
1515
proxies:Optional[dict]=None, request_timeout:int= constants.DEFAULT_REQUEST_TIMEOUT,max_result:int=10**10, debug:Optional[bool]=False,
16-
folder_path:str=None
16+
folder_path:str=None,include_headers:bool=False
1717
):
1818
"""Initializes newsdata client object for access Newsdata APIs."""
1919
self.apikey = apikey
@@ -24,6 +24,7 @@ def __init__(
2424
self.proxies = proxies
2525
self.request_timeout = request_timeout
2626
self.is_debug = debug
27+
self.include_headers = include_headers
2728
self.set_base_url()
2829
super().__init__(folder_path=folder_path)
2930

@@ -114,13 +115,15 @@ def __get_feeds(self,url:str,retry_count:int=None)-> dict:
114115
raise NewsdataException('Maximum retry limit reached. For more information use debug parameter while initializing NewsDataApiClient.')
115116

116117
response = self.request_method.get(url=url,proxies=self.proxies,timeout=self.request_timeout)
118+
headers = dict(response.headers)
117119

118120
if self.is_debug == True:
119-
headers = response.headers
120121
print(f'Debug | {self.get_current_dt()} | x_rate_limit_remaining: {headers.get("x_rate_limit_remaining")} | x_api_limit_remaining: {headers.get("x_api_limit_remaining")}')
121122

122123
feeds_data:dict = response.json()
123-
124+
if self.include_headers == True:
125+
feeds_data.update({'response_headers':headers})
126+
124127
if response.status_code != 200:
125128

126129
if response.status_code == 500:
@@ -182,6 +185,8 @@ def __get_feeds_all(self,url:str,max_result:int)-> dict:
182185
results = response.get('results')
183186
data['results'].extend(results)
184187
data['nextPage'] = response.get('nextPage')
188+
if self.include_headers:
189+
data['response_headers'] = response.get('response_headers')
185190
feeds_count+=len(results)
186191
if self.is_debug == True:
187192
print(f"Debug | {self.get_current_dt()} | total results: {data['totalResults']} | extracted: {feeds_count}")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='newsdataapi',
9-
version='0.1.21',
9+
version='0.1.22',
1010
packages=['newsdataapi'],
1111
description='Python library for newsdata client-API Call',
1212
long_description=long_description,

0 commit comments

Comments
 (0)