Skip to content

Commit c5b580e

Browse files
committed
initial commit
0 parents  commit c5b580e

File tree

8 files changed

+308
-0
lines changed

8 files changed

+308
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
.DS_Store
3+
*.pyc

INSTALL.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Installation
2+
============
3+
4+
To install simply (preferably in a virtualenv):
5+
6+
$ pip install -r requirements.txt
7+
8+
To run the example:
9+
10+
$ python example.py

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Diffbot Python API Client
2+
=========
3+
4+
Currently supports calls to the article endpoint.
5+
6+
7+
Installation
8+
-----------
9+
To install simply (preferably in a virtualenv):
10+
11+
$ pip install -r requirements.txt
12+
13+
To run the example:
14+
15+
$ python example.py
16+
17+
Example use
18+
--------------
19+
An example of how to use the client in your code:
20+
21+
```
22+
diffbot = DiffbotClient()
23+
url = "http://shichuan.github.io/javascript-patterns/"
24+
token = "3c66f28f72ea40c1b02e6a4cc195b07e"
25+
api = "article"
26+
version = 2
27+
28+
response = diffbot.request(url, token, api, version=2)
29+
```

client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import requests
2+
3+
class DiffbotClient:
4+
def __init__(self):
5+
pass
6+
7+
def request(self, url, token, api, fields=[], version=2):
8+
"""
9+
Returns a python object containing the requested resource from the diffbot api
10+
"""
11+
params = {"url": url, "token": token}
12+
response = requests.get(self.get_url(api, version), params=params)
13+
obj = response.json()
14+
if fields:
15+
obj = dict( (x, obj[x]) for x in fields)
16+
return obj
17+
18+
def get_url(self, api, version):
19+
"""
20+
Returns the uri for an endpoint as a string
21+
"""
22+
base_url = "http://api.diffbot.com/"
23+
version = "v" + str(version)
24+
url = base_url + version + "/" + api
25+
return url

example.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from client import DiffbotClient
2+
import pprint
3+
4+
diffbot = DiffbotClient()
5+
url = "http://shichuan.github.io/javascript-patterns/"
6+
token = "3c66f28f72ea40c1b02e6a4cc195b07e"
7+
api = "article"
8+
version = 2
9+
10+
print "Calling article API endpoint on the url: http://shichuan.github.io/javascript-patterns/...\n"
11+
response = diffbot.request(url, token, api, version=2)
12+
print "\nPrinting response:\n"
13+
pp = pprint.PrettyPrinter(indent=4)
14+
print pp.pprint(response)

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock==1.0.1
2+
nose==1.3.0
3+
requests==2.1.0
4+
simplejson==3.3.1
5+
wsgiref==0.1.2

test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import unittest
3+
from client import DiffbotClient
4+
from mock import patch
5+
import simplejson
6+
7+
def fake_request(client, api):
8+
resource_file = os.path.normpath('test_fixtures/%s.json' % api)
9+
with open(resource_file, mode='rb') as file:
10+
json = file.read()
11+
return simplejson.loads(json)
12+
13+
14+
class TestDiffbotClient(unittest.TestCase):
15+
16+
def setUp(self):
17+
self.patcher = patch('client.DiffbotClient.request', fake_request)
18+
self.patcher.start()
19+
self.client = DiffbotClient()
20+
21+
def tearDown(self):
22+
self.patcher.stop()
23+
24+
def test_request_for_article(self):
25+
api = "article"
26+
response = self.client.request(api)
27+
self.assertIn('text', response)
28+
self.assertEqual(response['meta']['title'], 'JavaScript Patterns')
29+
30+
if __name__ == '__main__':
31+
unittest.main()

test_fixtures/article.json

Lines changed: 191 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)