Skip to content

Commit 9ec175a

Browse files
uallow user to change domains and version
1 parent 8c9daf5 commit 9ec175a

5 files changed

Lines changed: 167 additions & 61 deletions

File tree

currentsapi/client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ def get_message(self):
2727

2828
class CurrentsAPI():
2929

30-
def __init__(self, api_key):
30+
def __init__(self, api_key,
31+
domain=constants.DOMAIN, version=constants.VERSION, timeout=30):
3132
if not isinstance(api_key, str):
3233
raise ValueError('api_key must be string')
3334
self.api_key = ApiAuth(api_key)
34-
35+
self.latest_endpoint = constants.LATEST_NEWS_URL % (domain, version)
36+
self.search_endpoint = constants.SEARCH_URL % (domain, version)
37+
self.timeout = timeout
3538

3639
def latest_news(self):
37-
r = requests.get(constants.LATEST_NEWS_URL, auth=self.api_key, timeout=30)
40+
r = requests.get(self.latest_endpoint, auth=self.api_key, timeout=self.timeout)
3841
if r.status_code != requests.codes.ok:
3942
raise APIException(r.json())
4043
return r.json()
@@ -102,7 +105,9 @@ def search(self, country=None, language=None, keywords=None, category=None,
102105

103106
if has_description:
104107
payload['has_description'] = 'true' if has_description else 'false'
105-
r = requests.get(constants.SEARCH_URL, auth=self.api_key, timeout=30, params=payload)
108+
r = requests.get(self.search_endpoint, auth=self.api_key,
109+
timeout=self.timeout,
110+
params=payload)
106111

107112
if r.status_code != requests.codes.ok:
108113
raise APIException(r.json())

currentsapi/constants.py

Lines changed: 111 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,124 @@
1-
LATEST_NEWS_URL = 'https://api.currentsapi.services/v1/latest-news'
2-
SEARCH_URL = 'https://api.currentsapi.services/v1/search'
31

4-
AVAILABLE_COUNTRY = ["AS", "AT", "BD", "BR", "CA", "CN", "FI","FR", "DE", "HK", "IN", "ID", "IL", "JP", "PH", "MX", "MY", "NL", "NO", "NZ", "PT",
5-
"RU", "SA", "SG", "CH", "SK", "TW", "TH", "VIET", "AE", "US", "GB", "VEN", "ZW", "IT", "PSE"]
2+
VERSION = 'v1'
3+
4+
DOMAIN = 'api.currentsapi.services'
5+
6+
LATEST_NEWS_URL = 'https://%s/%s/latest-news'
7+
SEARCH_URL = 'https://%s/%s/search'
68

79
REGION_MAPPING = {
8-
"Australia": "AS",
9-
"Austria": "AT",
10-
"Bangladesh": "BD",
11-
"Brazil": "BR",
12-
"Canada": "CA",
13-
"China": "CN",
14-
"Finland": "FI",
15-
"France":"FR",
16-
"Denmark": "DE",
17-
"Hong Kong": "HK",
18-
"India": "IN",
19-
"Indonedia": "ID",
20-
"Israel": "IL",
21-
"Japan": "JP",
22-
"Philippines": "PH",
23-
"Mexico": "MX",
24-
"Malaysia": "MY",
25-
"Dutch": "NL",
26-
"Norway": "NO",
27-
"New Zealand": "NZ",
28-
"Portugal": "PT",
29-
"Russia": "RU",
30-
"Saudi Arabia": "SA",
31-
"Singapore": "SG",
32-
"Switzerland": "CH",
33-
"South Korea": "SK",
34-
"Taiwan": "TW",
35-
"Thailand": "TH",
36-
"Vietnam": "VIET",
37-
"United Arab Emirates": "AE",
38-
"United State": "US",
39-
"German": "GB",
40-
"Venuzeula": "VEN",
41-
"Zimbabwe": "ZW",
42-
"Italy": "IT",
43-
"Palestine": "PSE"
10+
"United State": "US",
11+
"Taiwan": "TW",
12+
"German": "DE",
13+
"United Kingdom": "GB",
14+
"China": "CN",
15+
"India": "IN",
16+
"Spain": "ES",
17+
"Italy": "IT",
18+
"Poland": "PL",
19+
"Australia": "AU",
20+
"Malaysia": "MY",
21+
"Singapore": "SG",
22+
"Canada": "CA",
23+
"South Korea": "KR",
24+
"Denmark": "DK",
25+
"France":"FR",
26+
"Belgium": "BE",
27+
"Japan": "JP",
28+
"Austria": "AT",
29+
"Portugal": "PT",
30+
"Philippines": "PH",
31+
"Hong Kong": "HK",
32+
"Argentina": "AR",
33+
"Venezuela": "VE",
34+
"Brazil": "BR",
35+
"Finland": "FI",
36+
"Indonedia": "ID",
37+
"Vietnam": "VN",
38+
"Mexico": "MX",
39+
"Greece": "GR",
40+
"Netherlands": "NL",
41+
"Norway": "NO",
42+
"New Zealand": "NZ",
43+
"Russia": "RU",
44+
"Saudi-Arabia": "SA",
45+
"Switzerland": "CH",
46+
"Thailand": "TH",
47+
"United Arab Emirates": "AE",
48+
"Ireland": "IE",
49+
"Iran": "IR",
50+
"Iraq": "IQ",
51+
"Romania": "RO",
52+
"Afghanistan": "AF",
53+
"Zimbabwe": "ZW",
54+
"Myanmar": "MM",
55+
"Sweden": "SE",
56+
"Peru": "PE",
57+
"Panama": "PA",
58+
"Egypt": "EG",
59+
"Turkey": "TR",
60+
"Israel": "IL",
61+
"Czech Republic": "CZ",
62+
"Bangladesh": "BD",
63+
"Nigeria": "NG",
64+
"Kenya": "KE",
65+
"Chile": "CL",
66+
"Uruguay": "UY",
67+
"Ecuador": "EC",
68+
"Serbia": "RS",
69+
"Hungary":"HU",
70+
"Slovenia": "SI",
71+
"Gahana": "GH",
72+
"Bolivia": "BO",
73+
"Pakistan":"PK",
74+
"Colombia": "CO",
75+
"North Korea": "NK",
76+
"Paraguay": "PY",
77+
"Palestine": "PS",
78+
"Estonia": "EE",
79+
"Lebanon": "LB",
80+
"Qatar": "QA",
81+
"Kuwait": "KW",
82+
"Cambodia": "KH",
83+
"Nepal": "NP",
84+
"Luxembourg": "LU",
85+
"Bosnia": "BA",
86+
"Europe": "EU",
87+
"Asia": "ASIA",
88+
"International": "INT"
4489
}
4590

91+
AVAILABLE_COUNTRY = list(REGION_MAPPING.keys())
4692

47-
AVAILABLE_LANGUAGE = [ "ar", "es", "de", "nl", "en", "hi","fi","fr", "it", "pt", "ja", "msa", "ru", "zh", "ko"]
4893

4994
LANGUAGE_MAPPING = {
50-
'Arabic':'ar',
51-
'Chinese':'zh',
52-
'Dutch':'nl',
53-
'English':'en',
54-
'Finnish':'fi',
55-
'French':'fr',
56-
'German':'de',
57-
'Hindi':'hi',
58-
'Italian':'it',
59-
'Japanese':'ja',
60-
'Korean':'ko',
61-
'Malay':'msa',
62-
'Portuguese':'pt',
63-
'Russian':'ru',
64-
'Spanish':'es',
95+
'Arabic':'ar',
96+
'Chinese':'zh',
97+
'Dutch':'nl',
98+
'English':'en',
99+
'Finnish':'fi',
100+
'French':'fr',
101+
'German':'de',
102+
'Hindi':'hi',
103+
'Italian':'it',
104+
'Japanese':'ja',
105+
'Korean':'ko',
106+
'Malay':'msa',
107+
'Portuguese':'pt',
108+
'Russian':'ru',
109+
'Spanish':'es',
110+
'Vietnamise': 'vi',
111+
'Danish': 'da',
112+
'Czech': 'cs',
113+
'Greek': 'el',
114+
'Hungarian': 'hu',
115+
'Serbian': 'sr',
116+
'Thai': 'th',
117+
'Turkish': 'tr',
65118
}
66119

120+
AVAILABLE_LANGUAGE = list(LANGUAGE_MAPPING.keys())
121+
67122
VALID_CATEGORIES = [
68123
'regional',
69124
'technology',

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setup(
1818
name='currentsapi',
19-
version='0.0.2',
19+
version='0.0.4',
2020
author='Currents Dev',
2121
author_email='ray@currentsapi.services',
2222
license='MIT',
@@ -44,5 +44,7 @@
4444
'Programming Language :: Python :: 3.5',
4545
'Programming Language :: Python :: 3.6',
4646
'Programming Language :: Python :: 3.7',
47+
'Programming Language :: Python :: 3.8',
48+
'Programming Language :: Python :: 3.9',
4749
],
4850
)

tests/basics.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
3+
Basic test
4+
5+
'''
6+
import unittest
7+
from currentsapi import CurrentsAPI
8+
from currentsapi import constants
9+
10+
11+
12+
class TestBasics(unittest.TestCase):
13+
14+
def test_invalid_api(self):
15+
with self.assertRaises(TypeError):
16+
CurrentsAPI()
17+
18+
with self.assertRaises(ValueError):
19+
CurrentsAPI(1)
20+
21+
with self.assertRaises(ValueError):
22+
CurrentsAPI(None)
23+
24+
def test_urls_setup(self):
25+
26+
api = CurrentsAPI('dummy_key')
27+
assert api.latest_endpoint == 'https://api.currentsapi.services/v1/latest-news'
28+
assert api.search_endpoint == 'https://api.currentsapi.services/v1/search'
29+
30+
api = CurrentsAPI('dummy_key', 'localhost', 'v0')
31+
assert api.latest_endpoint == 'https://localhost/v0/latest-news'
32+
assert api.search_endpoint == 'https://localhost/v0/search'
33+
34+
api = CurrentsAPI('dummy_key', 'localhost', 'v1.1')
35+
assert api.latest_endpoint == 'https://localhost/v1.1/latest-news'
36+
assert api.search_endpoint == 'https://localhost/v1.1/search'
37+
38+
39+
def test_key_setup(self):
40+
api = CurrentsAPI('dummy_key')
41+
assert api.api_key.api_key == 'dummy_key'

tests/dummy_server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
3+
FIXTURES = os.path.join('test', 'static')

0 commit comments

Comments
 (0)