Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow subcommands to execute, more data returned

  • Loading branch information...
commit d1e433331f25a2c4bfdd0878f63a545a3b4ad541 1 parent 8611ddf
jaymz campbell authored
Showing with 51 additions and 6 deletions.
  1. +7 −2 activecollab/constants.py
  2. +26 −3 activecollab/library.py
  3. +18 −1 demo.py
9 activecollab/constants.py
View
@@ -1,17 +1,22 @@
# These are the 'top level' commands available
AC_COMMANDS = ('projects', 'people', 'discussions', 'calendar')
+AC_SUBCOMMAND = ('tickets', 'milestones', 'files', 'pages')
# The following is a list of sub elements of the main
# command XML which is returned
-AC_SUBCOMMAND = {
+AC_COMMAND_ELEMENT = {
'projects': 'project',
'people': 'company',
'discussions': 'topic',
+ 'tickets': 'ticket',
}
# These fields are common throughout XML responses so we
# grab these for all items as a good start
-AC_BASE_FIELDS = ('id', 'name')
+AC_BASE_FIELDS = ('id', 'name', 'permalink')
+AC_SUB_FIELDS = {
+ 'tickets': ('ticket_id',)
+}
# Used in printing output
AC_FIELD_SEP = ': '
29 activecollab/library.py
View
@@ -9,15 +9,22 @@ class ACRequest(object):
commands with a given API key. The returned XML is
then parsed and returned in usable form """
- def __init__(self, command, **kwargs):
+ def __init__(self, command, item_id=None, subcommand=None, sub_id=None, **kwargs):
if (command not in AC_COMMANDS):
raise ACCommandException('Not a valid command')
self.command = command
+ self.item_id = item_id
+ self.sub_id = sub_id
+ self.subcommand = subcommand
self.api_key = kwargs.get('api_key', API_KEY)
self.ac_url = kwargs.get('ac_url', AC_URL)
self.params = urllib.urlencode(kwargs.get('params', dict()))
+ self.valid_fields = AC_BASE_FIELDS
+ if self.subcommand:
+ self.valid_fields += AC_SUB_FIELDS[self.subcommand]
+
@property
def base_url(self):
""" Build our base API request URL"""
@@ -28,7 +35,19 @@ def base_url(self):
def command_url(self):
""" This url is the base of all executed commands """
url = self.base_url + self.command
+
+ if self.item_id:
+ # A particular project/person/comany etc id
+ url += '/' + str(self.item_id)
+ if self.subcommand:
+ # This is used to get tickets or milestones for example
+ url += '/' + self.subcommand
+ if self.sub_id:
+ url += '/' + self.sub_id
+
if self.params:
+ # Extra parameters via a dict which may be passed
+ # outside of our base command url
return '%s&%s' % (url, self.params)
else:
return url
@@ -41,12 +60,16 @@ def execute(self):
raise ACCommandException('Could not execute command')
xml = parse(raw_xml)
- items = xml.getElementsByTagName(AC_SUBCOMMAND[self.command])
+
+ if self.subcommand:
+ items = xml.getElementsByTagName(AC_COMMAND_ELEMENT[self.subcommand])
+ else:
+ items = xml.getElementsByTagName(AC_COMMAND_ELEMENT[self.command])
for item in items:
output = ''
for node in item.childNodes:
- if node.localName in AC_BASE_FIELDS:
+ if node.localName in self.valid_fields:
output += node.childNodes[0].nodeValue + AC_FIELD_SEP
if output:
19 demo.py
View
@@ -1,6 +1,23 @@
from activecollab.library import ACRequest
+from sys import argv
-req = ACRequest('people')
+try:
+ item_id = argv[1]
+except IndexError:
+ item_id = None
+
+try:
+ subcommand = argv[2]
+except IndexError:
+ subcommand = None
+
+try:
+ sub_id = argv[3]
+except IndexError:
+ sub_id = None
+
+
+req = ACRequest('projects', item_id=item_id, subcommand=subcommand, sub_id=sub_id)
print req.command_url
req.execute()
Please sign in to comment.
Something went wrong with that request. Please try again.