Skip to content

Commit

Permalink
Starting the "rewrite" branch.
Browse files Browse the repository at this point in the history
Committing basic HTTP and API access classes as well as a unit test.

https://mediawiki.org/wiki/Special:Code/pywikipedia/4502
  • Loading branch information
Misza13 committed Nov 4, 2007
0 parents commit d2135c8
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pywikibot/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
"""
The "data" module provides several physical layers of data access to the wiki.
"""
#
# (C) Pywikipedia bot team, 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'

#Import the classes exposed by this module:
from http import HTTP
from api import API
29 changes: 29 additions & 0 deletions pywikibot/data/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""
Interface functions to Mediawiki's api.php
"""
#
# (C) Pywikipedia bot team, 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'


import urllib
import http


class API:

def __init__(self, site):
self.site = site

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)

return http.HTTP(None).GET(address) #TODO: Use site's HTTP object instead
35 changes: 35 additions & 0 deletions pywikibot/data/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
Basic HTTP access interface (GET/POST wrappers).
"""
#
# (C) Pywikipedia bot team, 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'


import httplib


class HTTP:

def __init__(self, site):
self.site = site
self.useragent = 'PythonWikipediaBot/2.0'
#TODO: Initiate persistent connection here?


def GET(self, address):
#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.putheader('User-agent',self.useragent)
conn.endheaders()
conn.send('')

response = conn.getresponse()
data = response.read()

return response.status, data
41 changes: 41 additions & 0 deletions pywikibot/data/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
Set of test suites for the data module.
"""
#
# (C) Pywikipedia bot team, 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'


import unittest
import http, api


class HTTPTest(unittest.TestCase):

def setUp(self):
self.HTTP = http.HTTP(None) #TODO: Replace None with an actual Site object once implemented

def testGETMainPage(self):
"""GETting the Main Page should give a HTTP 200 response."""
status, data = self.HTTP.GET('/w/index.php?title=Main_Page')
self.assertEqual(status, 200)


class APITest(unittest.TestCase):

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."""
status, data = self.API.query()
self.assertEqual(status, 200)
self.assertEqual(data, '<?xml version="1.0" encoding="utf-8"?><api />')


if __name__ == '__main__':
unittest.main()

0 comments on commit d2135c8

Please sign in to comment.