Skip to content

Commit 828b359

Browse files
authored
Added Python 2 unicode support
- In Python 2, `get_everything()` and `get_top_headlines()` calls can now accept unicode strings as keywords
1 parent 0eee3c8 commit 828b359

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

newsapi/newsapi_client.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from newsapi.newsapi_auth import NewsApiAuth
33
from newsapi import const
44
from newsapi.newsapi_exception import NewsAPIException
5+
from sys import version_info
56

67

78
class NewsApiClient(object):
@@ -45,10 +46,17 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
4546

4647
# Keyword/Phrase
4748
if q is not None:
48-
if type(q) == str:
49+
if version_info[0] == 3:
50+
q_is_str = isinstance(q, str)
51+
elif version_info[0] == 2:
52+
q_is_str = isinstance(q, basestring)
53+
else:
54+
raise SystemError("unsupported version of python detected (supported versions: 2, 3)")
55+
56+
if q_is_str:
4957
payload['q'] = q
5058
else:
51-
raise TypeError('keyword/phrase q param should be a of type str')
59+
raise TypeError('keyword/phrase q param should be of type str')
5260

5361
# Sources
5462
if (sources is not None) and ((country is not None) or (category is not None)):
@@ -160,7 +168,14 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
160168

161169
# Keyword/Phrase
162170
if q is not None:
163-
if type(q) == str:
171+
if version_info[0] == 3:
172+
q_is_str = isinstance(q, str)
173+
elif version_info[0] == 2:
174+
q_is_str = isinstance(q, basestring)
175+
else:
176+
raise SystemError("unsupported version of python detected (supported versions: 2, 3)")
177+
178+
if q_is_str:
164179
payload['q'] = q
165180
else:
166181
raise TypeError('keyword/phrase q param should be of type str')

0 commit comments

Comments
 (0)