Skip to content

Commit abab823

Browse files
committed
Some refactoring to client.py
1 parent 29e0a26 commit abab823

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

client.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
import requests
22

3+
34
class DiffbotClient(object):
45
def __init__(self):
5-
pass
6+
self.base_url = "http://api.diffbot.com/"
67

78
def request(self, url, token, api, fields=[], version=2):
89
"""
910
Returns a python object containing the requested resource from the diffbot api
1011
"""
1112
params = {"url": url, "token": token}
12-
response = requests.get(self.get_url(api, version), params=params)
13+
response = requests.get(self.compose_url(api, version), params=params)
1314
obj = response.json()
15+
obj = self.select_fields_from_response(obj, fields)
16+
return obj
17+
18+
@staticmethod
19+
def select_fields_from_response(obj, fields):
20+
"""
21+
Returns the response object with the specified fields or all fields if
22+
the fields list is empty
23+
"""
1424
if fields:
15-
obj = dict( (x, obj[x]) for x in fields)
25+
obj = dict((x, obj[x]) for x in fields)
1626
return obj
1727

18-
def get_url(self, api, version):
28+
def compose_url(self, api, version_number):
1929
"""
2030
Returns the uri for an endpoint as a string
2131
"""
22-
base_url = "http://api.diffbot.com/"
23-
version = "v" + str(version)
24-
url = base_url + version + "/" + api
32+
version = self.format_version_string(version_number)
33+
url = self.base_url + version + "/" + api
2534
return url
35+
36+
@staticmethod
37+
def format_version_string(version_number):
38+
"""
39+
Returns a string representation of the API version
40+
"""
41+
version = "v" + str(version_number)
42+
return version

0 commit comments

Comments
 (0)