|
1 | 1 | import requests
|
2 | 2 |
|
| 3 | + |
3 | 4 | class DiffbotClient(object):
|
4 | 5 | def __init__(self):
|
5 |
| - pass |
| 6 | + self.base_url = "http://api.diffbot.com/" |
6 | 7 |
|
7 | 8 | def request(self, url, token, api, fields=[], version=2):
|
8 | 9 | """
|
9 | 10 | Returns a python object containing the requested resource from the diffbot api
|
10 | 11 | """
|
11 | 12 | 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) |
13 | 14 | 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 | + """ |
14 | 24 | if fields:
|
15 |
| - obj = dict( (x, obj[x]) for x in fields) |
| 25 | + obj = dict((x, obj[x]) for x in fields) |
16 | 26 | return obj
|
17 | 27 |
|
18 |
| - def get_url(self, api, version): |
| 28 | + def compose_url(self, api, version_number): |
19 | 29 | """
|
20 | 30 | Returns the uri for an endpoint as a string
|
21 | 31 | """
|
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 |
25 | 34 | 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