changeset 5:ac540e90b1dd

now taking server response 'Date' http header into account
author Atul Varma <varmaa@toolness.com>
date Wed, 23 Dec 2009 12:13:36 -0800
parents 6a0ad0463a89
children c9a268d0fa74
files bzapi.py example.py
diffstat 2 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/bzapi.py	Wed Dec 23 11:35:29 2009 -0800
+++ b/bzapi.py	Wed Dec 23 12:13:36 2009 -0800
@@ -6,6 +6,9 @@
 import pymongo
 import simplejson as json
 
+def datetime_from_rfc1123(timestamp):
+    return datetime.strptime(timestamp, '%a, %d %b %Y %H:%M:%S GMT')
+
 def datetime_to_iso(dt):
     return "%sZ" % (dt.replace(microsecond=0).isoformat('T'))
 
@@ -42,18 +45,26 @@
         else:
             self.last_update = bugs[0]['last_change_time']
 
-    def update(self):
-        params = {}
-        params.update(self.options)
-        if self.last_update:
-            params['changed_after'] = self.last_update
-        bugs = self.api.get('/bug', **params)['bugs']
+    def _get_full_bugs(self, bugs):
+        params = {'id': ','.join(bugs),
+                  'id_mode': 'include',
+                  'comments': '1',
+                  'history': '1'}
+        bugs = self.api.get('/bug', **params)['data']['bugs']
         for bug in bugs:
             logging.debug('processing bug %s' % bug['id'])
             for name in ['last_change_time', 'creation_time']:
                 bug[name] = datetime_from_iso(bug[name])
             bug['_id'] = bug['id']
             self.bugs.save(bug)
+
+    def update(self):
+        params = {}
+        params.update(self.options)
+        if self.last_update:
+            params['changed_after'] = self.last_update
+        bugs = self.api.get('/bug', **params)['data']['bugs']
+        self._get_full_bugs([bug['id'] for bug in bugs])
         self._update_last_update()
 
 class BugzillaApi(object):
@@ -63,7 +74,7 @@
         self.password = password
         config = collection.find_one()
         if not config:
-            config = self.get('/configuration')
+            config = self.get('/configuration')['data']
             sanitize(config)
             collection.insert(config)
         self.config = config
@@ -106,4 +117,5 @@
 
         response = urllib2.urlopen(request)
 
-        return json.loads(response.read())
+        return {'data': json.loads(response.read()),
+                'date': datetime_from_rfc1123(response.info()['Date'])}
--- a/example.py	Wed Dec 23 11:35:29 2009 -0800
+++ b/example.py	Wed Dec 23 12:13:36 2009 -0800
@@ -15,19 +15,21 @@
     collection = db.api
     )
 
+#db.bugs.remove({})
+
 search = bzapi.CachedSearch(
     api = api,
     collection = db.bugs,
+    changed_after='2009-12-20T12:00:00Z',
     product='Mozilla Labs',
     component='Jetpack'
     )
 
 search.update()
+#print search.bugs.find_one({'id': '530169'})['history']
 
 #print len([bug for bug in search.bugs.itervalues()])
 
-print search.bugs.find_one({'id': '530169'})
-
 #print search.bugs.values()[12]
 
 #print api.get('/bug/510339/history')