-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from openzim/api-login
Refactor api module and retry login if Unathenticated error is thrown
- Loading branch information
Showing
4 changed files
with
68 additions
and
15 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 |
---|---|---|
@@ -1,11 +1,35 @@ | ||
import logging | ||
|
||
import mwclient | ||
|
||
logger = logging.getLogger(__name__) | ||
_ua = 'WP1.0Bot/3.0. Run by User:Audiodude. Using mwclient/0.9.1' | ||
|
||
try: | ||
from wp1.credentials import API_CREDS | ||
site = mwclient.Site('en.wikipedia.org', | ||
clients_useragent=_ua) | ||
site.login(API_CREDS['user'], API_CREDS['pass']) | ||
except ImportError: | ||
site = None | ||
site = mwclient.Site('en.wikipedia.org', clients_useragent=_ua) | ||
def login(): | ||
try: | ||
from wp1.credentials import API_CREDS | ||
site.login(API_CREDS['user'], API_CREDS['pass']) | ||
except mwclient.errors.LoginError as e: | ||
logger.exception(e[1]['result']) | ||
except ImportError: | ||
# No credentials, probably in development environment. | ||
pass | ||
|
||
|
||
# Global login on startup. | ||
login() | ||
|
||
|
||
def save_page(page, wikicode, msg): | ||
if not site: | ||
logger.error('Could not save page %s because api site is not defined', page) | ||
return False | ||
|
||
try: | ||
page.save(wikicode, msg) | ||
except mwclient.errors.AssertUserFailedError as e: | ||
logger.warn('Got login exception, retrying login') | ||
login() | ||
page.save(wikicode, msg) | ||
return True |
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 @@ | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
import mwclient | ||
|
||
from wp1 import api | ||
|
||
class ApiTest(unittest.TestCase): | ||
def setUp(self): | ||
self.page = MagicMock() | ||
|
||
def test_save_page(self): | ||
api.save_page(self.page, '<code>', 'edit summary') | ||
self.assertEqual(1, len(self.page.save.call_args_list)) | ||
self.assertEqual(('<code>', 'edit summary'), self.page.save.call_args[0]) | ||
|
||
@patch('wp1.api.login') | ||
def test_save_page_tries_login_on_exception(self, patched_login): | ||
api.site = MagicMock() | ||
self.page.save.side_effect = mwclient.errors.AssertUserFailedError() | ||
with self.assertRaises(mwclient.errors.AssertUserFailedError): | ||
actual = api.save_page(self.page, '<code>', 'edit summary') | ||
self.assertTrue(actual) | ||
self.assertEqual(1, len(patched_login.call_args_list)) | ||
|
||
def test_save_page_skips_login_on_none_site(self): | ||
api.site = None | ||
actual = api.save_page(self.page, '<code>', 'edit summary') | ||
self.assertFalse(actual) |
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
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