Commits

Kevin Veroneau  committed 298102b

Cleaned up code, added ability to easily grab issues and view the contents.

  • Participants
  • Parent commits 382c1b7

Comments (0)

Files changed (1)

+# -*- coding: utf-8 -*-
 import cookielib
 import urllib2
 import base64
 import json
 from urllib import urlencode
 
-class Issue():
+class Issue(object):
     title = ""
     content = ""
     component = None
     priority = "major"
     status = "new"
     kind = "bug"
-    def __init__(self, api, username, repository):
+    def __init__(self, api, username, repository, data=None):
         self.api = api
-        self.username = username
-        self.repository = repository
         self.responsible = username
+        self.url = 'repositories/%s/%s/issues/' % (username, repository)
+        if data:
+	    self.url = 'repositories/%s/%s/issues/%s/' % (username, repository, data['local_id'])
+	    self.title = data['title']
+	    self.content = data['content']
+	    self.component = data['metadata']['component']
+	    self.milestone = data['metadata']['milestone']
+	    self.version = data['metadata']['version']
+	    self.responsible = data['responsible']['username']
+	    self.priority = data['priority']
+	    self.status = data['status']
+	    self.kind = data['metadata']['kind']
 
     def as_dict(self):
         data = {'title':self.title, 'content':self.content, 'priority':self.priority, 'status':self.status, 'kind':self.kind}
 	return data
 
     def save(self):
-        self.json = self.api.post(self.username, self.repository, self.as_dict())
+        self.json = self.api.post(self.url, self.as_dict())
         return self.json
 
-class API():
+class API(object):
     api_url = 'https://api.bitbucket.org/1.0/'
 
     def __init__(self, username, password, proxy=None):
             opener = urllib2.build_opener(cookie_handler)
         return opener
 
-    def post(self, username, repository, data):
-        query_url = self.api_url + 'repositories/%s/%s/issues/' % (username, repository)
+    def _request(self, url, data=None):
+        query_url = self.api_url + url
+        if data:
+	  data = urlencode(data)
         try:
-	    req = urllib2.Request(query_url, urlencode(data), {"Authorization": self._auth })
+	    req = urllib2.Request(query_url, data, {"Authorization": self._auth })
 	    handler = self._opener.open(req)
 	except urllib2.HTTPError, e:
 	    print e.headers
 	    raise e
 	return json.load(handler)
 
+    def post(self, url, data):
+        return self._request(url, data)
+
+    def get(self, url):
+        return self._request(url, None)
+    
     def get_issues(self, username, repository):
-        query_url = self.api_url + 'repositories/%s/%s/issues/' % (username, repository)
-        try:
-            req = urllib2.Request(query_url, None, {"Authorization": self._auth })
-            handler = self._opener.open(req)
-        except urllib2.HTTPError, e:
-            print e.headers
-            raise e
-        return json.load(handler)
+        json = self.get('repositories/%s/%s/issues/' % (username, repository))
+        issues = []
+        for i in json['issues']:
+	    issue = Issue(self, username, repository, i)
+	    issues.append(issue)
+	return issues
         
     def new_issue(self, username, repository):
         return Issue(self, username, repository)