Revision: 4503 Author: misza13 Date: 2007-11-04 21:11:53 +0000 (Sun, 04 Nov 2007)
Log Message: ----------- Implemented remaining HTTP wrapper methods, switched API to communicate via POST with JSON as the default result format.
Modified Paths: -------------- branches/rewrite/pywikibot/data/api.py branches/rewrite/pywikibot/data/http.py branches/rewrite/pywikibot/data/test.py
Modified: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py 2007-11-04 20:24:44 UTC (rev 4502) +++ branches/rewrite/pywikibot/data/api.py 2007-11-04 21:11:53 UTC (rev 4503) @@ -22,8 +22,7 @@ def query(prop, **kwargs): params = dict(kwargs) params['action'] = 'query' - if not params.has_key('format'): #Most probably, we want the XML format - params['format'] = 'xml' - address = '/w/api.php?' + urllib.urlencode(params) + if not params.has_key('format'): #Most probably, we want the JSON format + params['format'] = 'json'
- return http.HTTP(None).GET(address) #TODO: Use site's HTTP object instead + return http.HTTP(None).POST('/w/api.php',params) #TODO: Use site's HTTP object instead
Modified: branches/rewrite/pywikibot/data/http.py =================================================================== --- branches/rewrite/pywikibot/data/http.py 2007-11-04 20:24:44 UTC (rev 4502) +++ branches/rewrite/pywikibot/data/http.py 2007-11-04 21:11:53 UTC (rev 4503) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -Basic HTTP access interface (GET/POST wrappers). +Basic HTTP access interface (GET/POST/HEAD wrappers). """ # # (C) Pywikipedia bot team, 2007 @@ -10,7 +10,7 @@ __version__ = '$Id: $'
-import httplib +import urllib, httplib
class HTTP: @@ -20,16 +20,26 @@ self.useragent = 'PythonWikipediaBot/2.0' #TODO: Initiate persistent connection here?
+ def GET(self, address, query={}): + return self._request('GET',address + '?' + urllib.urlencode(query))
- def GET(self, address): + def POST(self, address, query={}): + return self._request('POST',address,urllib.urlencode(query)) + + def HEAD(self, address, query={}): + return self._request('HEAD',address + '?' + urllib.urlencode(query)) + + def _request(self, method, address, data=''): #TODO: Resuse said connection. conn = httplib.HTTPConnection('en.wikipedia.org',80) #TODO: Obviously, get these from the site object (unimplemented yet) - conn.putrequest('GET',address) + conn.putrequest(method,address) conn.putheader('User-agent',self.useragent) + conn.putheader('Content-type','application/x-www-form-urlencoded') + conn.putheader('Content-Length',len(data)) conn.endheaders() - conn.send('') + conn.send(data)
response = conn.getresponse() - data = response.read() + rdata = response.read()
- return response.status, data + return response.status, rdata
Modified: branches/rewrite/pywikibot/data/test.py =================================================================== --- branches/rewrite/pywikibot/data/test.py 2007-11-04 20:24:44 UTC (rev 4502) +++ branches/rewrite/pywikibot/data/test.py 2007-11-04 21:11:53 UTC (rev 4503) @@ -21,7 +21,7 @@
def testGETMainPage(self): """GETting the Main Page should give a HTTP 200 response.""" - status, data = self.HTTP.GET('/w/index.php?title=Main_Page') + status, data = self.HTTP.GET('/w/index.php', {'title' : 'Main_Page'}) self.assertEqual(status, 200)
@@ -30,11 +30,11 @@ def setUp(self): self.API = api.API(None) #TODO: Replace None with an actual Site object once implemented
- def testGETMainPage(self): - """Querying for nothing should return an empty <api /> tag.""" + def testEmptyQuery(self): + """Querying for nothing should return an empty list.""" status, data = self.API.query() self.assertEqual(status, 200) - self.assertEqual(data, '<?xml version="1.0" encoding="utf-8"?><api />') + self.assertEqual(data, '[]')
if __name__ == '__main__':