Skip to content

Commit 27a9d3c

Browse files
author
Yousef Sultan
authored
Merge pull request #3 from BradleyShaw/master
Update to work with the new API
2 parents 8cd0fee + d23dc21 commit 27a9d3c

File tree

7 files changed

+101
-58
lines changed

7 files changed

+101
-58
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ This is obviously still in development.
44

55
## Example:
66
```python
7-
import codebottle
7+
from codebottle import CodeBottle
88

9-
#Results of a search
10-
search = codebottle.search(keywords="java").results
9+
cb = CodeBottle()
1110

12-
#Get a snippet
13-
snippet = codebottle.get(id="373dcc67").data
11+
# Results of a search
12+
search = cb.snippets.get(keywords='java').data
1413

15-
#Browse
16-
browse = codebottle.browse(limit=10).results
14+
# Get a snippet
15+
snippet = cb.snippets.get('91f98993c8').data
1716

18-
#Verify secure
19-
secure = codebottle.verifysecure(secure_token="Some type of token here")
17+
# Get newest snippets
18+
browse = cb.snippets.new().data
2019
```
2120

2221
## Installing

codebottle/__init__.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,2 @@
1-
import requests
2-
3-
api_version = "v1"
4-
api_url = "https://codebottle.io/api/{0}".format(api_version)
5-
6-
api_search_url = "{0}/search.php".format(api_url)
7-
api_get_snippet_url = "{0}/get.php".format(api_url)
8-
api_browse_url = "{0}/browse.php".format(api_url)
9-
api_verifysecure_url = "{0}/verifysecure.php".format(api_url)
10-
11-
12-
class CodebottleError(Exception):
13-
"""Exception to represent a error sent from the API"""
14-
pass
15-
16-
class Result(object):
17-
"""A class with attributes of a codebottle API result"""
18-
def __init__(self, r):
19-
self.json = r.json()
20-
if self.json["error"]:
21-
raise CodebottleError(self.json["error"])
22-
23-
for k, v in self.json.items():
24-
setattr(self, k, v)
25-
26-
#Functions
27-
def search(**kwargs):
28-
result = requests.get(api_search_url, params=kwargs)
29-
return Result(result)
30-
31-
def get(**kwargs):
32-
result = requests.get(api_get_snippet_url, params=kwargs)
33-
return Result(result)
34-
35-
def browse(**kwargs):
36-
result = requests.get(api_browse_url, params=kwargs)
37-
return Result(result)
38-
39-
def verifysecure(**kwargs):
40-
result = requests.get(api_verifysecure_url, params=kwargs)
41-
return Result(result)
1+
from .client import CodeBottle
2+
from .exceptions import CodebottleError

codebottle/auth.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from requests.auth import AuthBase
2+
3+
4+
class Token(AuthBase):
5+
6+
def __init__(self, token):
7+
self.token = token
8+
9+
def __call__(self, r):
10+
if self.token:
11+
r.headers['Authorization'] = 'Bearer {0}'.format(self.token)
12+
return r

codebottle/client.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import requests
2+
3+
from .exceptions import CodebottleError
4+
from .auth import Token
5+
6+
7+
class Result(object):
8+
'''A class with attributes of a codebottle API result'''
9+
def __init__(self, r):
10+
self.json = r.json()
11+
if self.json.get('error'):
12+
raise CodebottleError(self.json['error'])
13+
14+
for k, v in self.json.items():
15+
setattr(self, k, v)
16+
17+
18+
class CodeBottle(object):
19+
20+
def __init__(self, token=None):
21+
self.api_url = 'https://api.codebottle.io'
22+
self.api_accept = 'application/vnd.codebottle.v1+json'
23+
self.token = token
24+
self.snippets = self.Snippets(self)
25+
26+
def _build_url(self, endpoint, *args):
27+
url = '{0}/{1}'.format(self.api_url, endpoint)
28+
if len(args) > 0:
29+
args = [str(a) for a in args]
30+
url = '{0}/{1}'.format(url, '/'.join(args))
31+
return url
32+
33+
def _get(self, endpoint, *args, **kwargs):
34+
url = self._build_url(endpoint, *args)
35+
r = requests.get(url, params=kwargs, auth=Token(self.token),
36+
headers={'Accept': self.api_accept})
37+
return Result(r)
38+
39+
def _post(self, endpoint, *args, **kwargs):
40+
url = self._build_url(endpoint, *args)
41+
r = requests.post(url, data=kwargs, auth=Token(self.token),
42+
headers={'Accept': self.api_accept})
43+
return Result(r)
44+
45+
def languages(self, *args):
46+
return self._get('languages', *args)
47+
48+
def categories(self, *args):
49+
return self._get('categories', *args)
50+
51+
class Snippets(object):
52+
53+
def __init__(self, parent):
54+
self.parent = parent
55+
56+
def get(self, *args, **kwargs):
57+
return self.parent._get('snippets', *args, **kwargs)
58+
59+
def new(self):
60+
return self.get('new')
61+
62+
def create(self, **kwargs):
63+
return self.parent._post('snippets', **kwargs)
64+
65+
def vote(self, *args, **kwargs):
66+
return self.parent._post('snippets', *args, **kwargs)

codebottle/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class CodebottleError(Exception):
2+
'''Exception to represent a error sent from the API'''
3+
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='codebottle',
4-
version='0.0.1',
4+
version='0.1.0',
55
description="A Python library to interact with CodeBottle's API",
66
url='https://github.com/codebottle-io/codebottle-python',
77
author='Luke J.',

tests/travis.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import codebottle
1+
from codebottle import CodeBottle
22

3-
#Results of a search
4-
search = codebottle.search(keywords="java").results
3+
cb = CodeBottle()
54

6-
#Get a snippet
7-
snippet = codebottle.get(id="373dcc67").data
5+
# Results of a search
6+
search = cb.snippets.get(keywords='java').data
87

9-
#Browse
10-
browse = codebottle.browse(limit=10).results
8+
# Get a snippet
9+
snippet = cb.snippets.get('91f98993c8').data
10+
11+
# Browse
12+
browse = cb.snippets.new().data
1113

1214
print(search)
1315
print(snippet)

0 commit comments

Comments
 (0)