Skip to content

Commit 29d4c2c

Browse files
author
igniteflow
committed
Updated README and refactoring
1 parent f70ff05 commit 29d4c2c

5 files changed

Lines changed: 96 additions & 71 deletions

File tree

‎README.md‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
Codebase API Python Client
22
==========================
33

4-
A Python client providing read/write access to the [Codebase API](http://support.codebasehq.com/kb). See client.py for the full list of available methods.
4+
A Python client providing read/write access to the [Codebase API](http://support.codebasehq.com/kb) with a small, but powerful, CLI.
55

66
Install
77
-------
88

99
pip install -U git+git://github.com/igniteflow/codebase-python-api-client
1010

11-
Use
11+
CLI
1212
---
1313

14+
To use the command-line interface, create a file called .codebase in your home directory and add your Codebase username and apikey (found in your Codebase settings page) in the following JSON format:
15+
16+
{
17+
"CODEBASE_USERNAME": "foo/bar",
18+
"CODEBASE_APIKEY": "4ofh783o4hf78o4fh4o"
19+
}
20+
21+
You can now explore the API. To see the available methods, just type:
22+
23+
codebase
24+
25+
To view all categories for a project and see the response in the terminal just enter:
26+
27+
codebase [project] categories
28+
29+
Or search:
30+
31+
codebase [project] search [search term]
32+
33+
The CLI can call any function in the CodeBaseAPI class using the syntax:
34+
35+
codebase [project] [function name] *[args]
36+
37+
38+
Use the client in your code
39+
---------------------------
40+
1441
from codebase.client import CodeBaseAPI
15-
42+
1643
codebase = CodeBaseAPI(username='walter-white', apikey='84gf6479gf674gf', project='MyProject')
1744

1845
# get all notes for a ticket
@@ -23,28 +50,15 @@ Use
2350
'ticket_note': {
2451
u'content': u'This is the note comment',
2552
u'changes': {
26-
u'status_id': u'1631923',
53+
u'status_id': u'1631923',
2754
},
2855
},
2956
}
3057
codebase.add_note(ticket_id=1, data=note_data)
31-
58+
3259
Debugging
3360
---------
3461

35-
By default, data is given and returned as Python dicts. To get the raw Requests Response object, just set CodeBaseAPI.DEBUG to True.
36-
37-
CLI
38-
---
62+
By default, data is given and returned as Python dicts. To get the raw requests.Response object, just set CodeBaseAPI.DEBUG to True.
3963

40-
There a simple command-line interface to explore the API. For example, to view all catagories and see the response in the terminal just enter:
41-
42-
./cli.py [project] categories
43-
44-
Or search:
45-
46-
./cli.py [project] search [search term]
47-
48-
The CLI can call any function in the CodeBaseAPI class using the syntax:
4964

50-
./cli.py [project] [function name] *[args]

‎bin/codebase‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
#!/usr/bin/env python
22

3+
import pprint
4+
import sys
5+
6+
from codebase.client import CodeBaseAPI
7+
8+
39
if __name__ == "__main__":
4-
from codebase.cli import main
5-
main()
10+
"""
11+
Simple command-line interface to the Codebase API. Example usage:
12+
13+
codebase myproject all_notes 6
14+
15+
Arguments:
16+
1. project name
17+
2. api function name
18+
3. args to pass to the api function
19+
"""
20+
if len(sys.argv) == 1:
21+
# if no args then print available API methods
22+
for name in dir(CodeBaseAPI):
23+
if name[0] != '_' and name[0].islower():
24+
print name
25+
else:
26+
# make the API call
27+
project = sys.argv[1]
28+
command = sys.argv[2]
29+
args = sys.argv[3:]
30+
31+
codebase = CodeBaseAPI(
32+
project=project,
33+
)
34+
35+
response = getattr(codebase, command)(*args)
36+
pprint.pprint(response)

‎codebase/cli.py‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎codebase/client.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ class Auth(object):
1010
API_ENDPOINT = 'http://api3.codebasehq.com'
1111
DEBUG = False
1212

13-
def __init__(self, project, **kwargs):
14-
super(Auth, self).__init__(**kwargs)
13+
def _default_settings(self):
1514
settings = Settings()
1615
self.username = settings.CODEBASE_USERNAME
1716
self.apikey = settings.CODEBASE_APIKEY
17+
18+
def __init__(self, project, username=None, apikey=None, debug=False, **kwargs):
19+
super(Auth, self).__init__(**kwargs)
20+
21+
if username and apikey:
22+
self.username = username
23+
self.apikey = apikey
24+
else:
25+
self._default_settings()
26+
1827
self.project = project
28+
self.DEBUG = debug
1929

2030
self.HEADERS = {
2131
"Content-type": "application/json",

‎codebase/tests.py‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import unittest
22

33
from client import CodeBaseAPI
4-
from settings import CODEBASE_USERNAME, CODEBASE_APIKEY, PROJECT
54

65

76
class CodebaseAPITest(unittest.TestCase):
8-
9-
def setUp(self):
10-
self.codebase = CodeBaseAPI(
11-
username=CODEBASE_USERNAME,
12-
apikey=CODEBASE_APIKEY,
13-
project=PROJECT,
14-
debug=True
15-
)
167

17-
def test_statuses(self):
18-
self.assertEqual(self.codebase.statuses().status_code, 200)
8+
PROJECT = 'foo'
199

20-
def test_priorities(self):
21-
self.assertEqual(self.codebase.priorities().status_code, 200)
10+
def setUp(self):
11+
self.codebase = CodeBaseAPI(
12+
project=self.PROJECT,
13+
debug=True
14+
)
2215

23-
def test_categories(self):
24-
self.assertEqual(self.codebase.categories().status_code, 200)
16+
def test_statuses(self):
17+
self.assertEqual(self.codebase.statuses().status_code, 200)
2518

26-
def test_milestones(self):
27-
self.assertEqual(self.codebase.milestones().status_code, 200)
19+
def test_priorities(self):
20+
self.assertEqual(self.codebase.priorities().status_code, 200)
2821

29-
def test_search(self):
30-
self.assertEqual(self.codebase.search('one').status_code, 200)
22+
def test_categories(self):
23+
self.assertEqual(self.codebase.categories().status_code, 200)
24+
25+
def test_milestones(self):
26+
self.assertEqual(self.codebase.milestones().status_code, 200)
27+
28+
def test_search(self):
29+
self.assertEqual(self.codebase.search('one').status_code, 200)
3130

3231
if __name__ == '__main__':
3332
unittest.main()

0 commit comments

Comments
 (0)