-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |