1 From de97f3ccde6527ac63bde701ae759291d93d1aa4 Mon Sep 17 00:00:00 2001
2 From: Ian Weller <ian@ianweller.org>
3 Date: Fri, 4 Feb 2011 23:58:26 -0600
4 Subject: Refactor normalize_api_url
7 simplemediawiki.py | 42 +++++++++++++++++++++++-------------------
8 1 files changed, 23 insertions(+), 19 deletions(-)
10 diff --git a/simplemediawiki.py b/simplemediawiki.py
11 index ff879ed..e123a5b 100644
12 --- a/simplemediawiki.py
13 +++ b/simplemediawiki.py
14 @@ -101,7 +101,28 @@ class MediaWiki():
15 returns that URL, while also helpfully setting this object's API URL to
16 it. If it can't magically conjure an API endpoint, it returns False.
18 - data, data_json = self._normalize_api_url_tester(self._api_url)
19 + def tester(self, api_url):
21 + Attempts to fetch general information about the MediaWiki instance
22 + in order to test whether the given URL will return JSON.
24 + data = self._fetch_http(api_url, {'action': 'query',
26 + 'siprop': 'general',
29 + data_json = json.loads(data)
30 + # may as well set the version
32 + version_string = data_json['query']['general']['generator']
33 + self._mediawiki_version = version_string.split(' ', 1)[1]
36 + return (data, data_json)
40 + data, data_json = tester(self, self._api_url)
44 @@ -109,30 +130,13 @@ class MediaWiki():
45 if 'index.php' in self._api_url:
46 test_api_url = self._api_url.split('index.php')[0] + 'api.php'
48 - test_data, test_data_json = \
49 - self._normalize_api_url_tester(test_api_url)
50 + test_data, test_data_json = tester(self, test_api_url)
51 print (test_data, test_data_json)
53 self._api_url = test_api_url
57 - def _normalize_api_url_tester(self, api_url):
58 - data = self._fetch_http(api_url, {'action': 'query',
60 - 'siprop': 'general',
63 - data_json = json.loads(data)
64 - # may as well set the version
66 - version_string = data_json['query']['general']['generator']
67 - self._mediawiki_version = version_string.split(' ', 1)[1]
70 - return (data, data_json)
74 def login(self, user, passwd, token=None):