FogBugz » FogBugzPy A Python wrapper for the FogBugz API
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.9.1, 0.9.2, and 0.9.3

First addition of the FB API. Doesn't really work yet, but will soon.

Changeset 6276ba5e64de

by Profile picture of User 10Tyler G. Hicks-Wright <tghw@fogcreek.com>

Changes to one file · Browse files at 6276ba5e64de Diff from another changeset...

Change 1 of 1 Show Entire File fogbugz.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
 
@@ -1,0 +1,253 @@
+import urllib2 + +from BeautifulSoup import BeautifulSoup + +class FogBugzAPIError(Exception): + def __init__(self, response): + self.response = response + self.message = response.error.string + self.error_code = int(response.error['code']) + +class FogBugzConnectionError(Exception): + pass + +class FBType: + """ + Base FogBugz object from which all other objects inherit. + """ + def __init__(self, fb): + self._fb = fb + + def __new__(cls, name, bases, dct): + return type.__new__(cls, name, bases, dict) + +class FBFilter: + TYPE_BUILTIN = 1 + TYPE_SAVED = 2 + TYPE_SHARED = 3 + + def __init__(self, fb, filter): + FBObject.__init__(self, fb) + + id = filter['sfilter'] + name = filter.string + current = filter.get('status', None) == 'current' + + if filter['type'] == 'builtin': + self.type = TYPE_BUILTIN + if filter['type'] == 'saved': + self.type = TYPE_SAVED + if filter['type'] == 'shared': + self.type = TYPE_SHARED + + def setCurrent(self): + self._fb.setCurrentFilter(self.id) + + def listCases(self): + currentFilter = self._fb.currentFilter + response = self.makeRequest('search') + + +class FogBugz: + def __init__(self, url): + if not url.endswith('/'): + url += '/' + + self._token = None + self._opener = urllib2.build_opener() + try: + soup = BeautifulSoup(self._opener.open(url + 'api.xml')) + except URLError: + raise FogBugzConnectionError("Library could not connect to the FogBugz API. Either this installation of FogBugz does not support the API, or the url, %s, is incorrect." % (self._url,)) + self._url = url + soup.response.url.string + self.currentFilter = None + + def makeRequest(self, cmd, **kwargs): + data = 'cmd=%s' % (cmd,) + for k in kwargs.keys(): + data += '&%s=%s' % (k, kwargs[k],) + if self._token: + data += '&token=%s' % (self._token,) + try: + response = BeautifulSoup(self._opener.open(self._url, data)).response + except URLError, e: + raise FogBugzConnectionError(e) + if response.error: + raise FogBugzAPIError(response) + print self._url, data + print response + return response + + def logon(self, username, password): + """ + Logs the user on to FogBugz. + + Returns None for a successful login, otherwise returns a list + of logins to choose from if the username provided is + ambiguous. + """ + if self._token: + logoff() + try: + response = self.makeRequest('logon', email=username, password=password) + except FogBugzAPIError, e: + if e.error_code == 2: + return [person.string for person in response.people.findall('person')] + else: + raise e + self._token = response.token.string + return None + + def logoff(self): + """ + Logs off the current user. + """ + self.makeRequest('logoff') + self._token = None + + def listFilters(self): + """ + Returns a list of FBFilters. + """ + response = self.makeRequest('listFilters') + filters = [] + for filter in response.filters.findAll(name='filter'): + filters.append(FBFilter(self, filter)) + if current: + self.currentFilter = id + return filters + + def setCurrentFilter(self, id): + self.makeRequest('saveFilter', sFilter=self.id) + + def search(self): + response = self.makeRequest('search') + + def new(self): + response = self.makeRequest('new') + + def edit(self): + response = self.makeRequest('edit') + + def assign(self): + response = self.makeRequest('assign') + + def reactivate(self): + response = self.makeRequest('reactivate') + + def reopen(self): + response = self.makeRequest('reopen') + + def resolve(self): + response = self.makeRequest('resolve') + + def close(self): + response = self.makeRequest('close') + + def email(self): + response = self.makeRequest('email') + + def reply(self): + response = self.makeRequest('reply') + + def forward(self): + response = self.makeRequest('forward') + + def listMailboxes(self): + response = self.makeRequest('listMailboxes') + + def listProjects(self): + response = self.makeRequest('listProjects') + + def listAreas(self): + response = self.makeRequest('listAreas') + + def listCategories(self): + response = self.makeRequest('listCategories') + + def listPriorities(self): + response = self.makeRequest('listPriorities') + + def listPeople(self): + response = self.makeRequest('listPeople') + + def listStatuses(self): + response = self.makeRequest('listStatuses') + + def listFixFors(self): + response = self.makeRequest('listFixFors') + + def viewProject(self): + response = self.makeRequest('viewProject') + + def viewArea(self): + response = self.makeRequest('viewArea') + + def viewCategory(self): + response = self.makeRequest('viewCategory') + + def viewPriority(self): + response = self.makeRequest('viewPriority') + + def viewPerson(self): + response = self.makeRequest('viewPerson') + + def viewStatus(self): + response = self.makeRequest('viewStatus') + + def viewFixFor(self): + response = self.makeRequest('viewFixFor') + + def viewMailbox(self): + response = self.makeRequest('viewMailbox') + + def listWorkingSchedule(self): + response = self.makeRequest('listWorkingSchedule') + + def wsDateFromHours(self): + response = self.makeRequest('wsDateFromHours') + + def startWork(self): + response = self.makeRequest('startWork') + + def stopWork(self): + response = self.makeRequest('stopWork') + + def newInterval(self): + response = self.makeRequest('newInterval') + + def listIntervals(self): + response = self.makeRequest('listIntervals') + + def newCheckin(self): + response = self.makeRequest('newCheckin') + + def listCheckins(self): + response = self.makeRequest('listCheckins') + + def listDiscussGroups(self): + response = self.makeRequest('listDiscussGroups') + + def listDiscussion(self): + response = self.makeRequest('listDiscussion') + + def listDiscussTopic(self): + response = self.makeRequest('listDiscussTopic') + + def listScoutCase(self): + response = self.makeRequest('listScoutCase') + + def subscribe(self): + response = self.makeRequest('subscribe') + + def unsubscribe(self): + response = self.makeRequest('unsubscribe') + + def view(self): + response = self.makeRequest('view') + + def viewSettings(self): + response = self.makeRequest('viewSettings') + + def list(self): + response = self.makeRequest('list') \ No newline at end of file