4 from .collections import (DeletedRevisions, Pages, RecentChanges, Revisions,
5 SiteInfo, UserContribs, Users)
6 from .errors import APIError, AuthenticationError, MalformedResponse
8 logger = logging.getLogger("mw.api.session")
10 DEFAULT_USER_AGENT = "MediaWiki-Utilities"
12 The default User-Agent to be sent with requests to the API.
15 class Session(api.Session):
17 Represents a connection to a MediaWiki API.
19 Cookies and other session information is preserved.
23 The base URI for the API to use. Usually ends in "api.php"
25 The User-Agent to be sent with requests. Will raise a warning if
26 left to default value.
29 def __init__(self, uri, *args, user_agent=DEFAULT_USER_AGENT, **kwargs):
31 Constructs a new :class:`Session`.
34 if user_agent == DEFAULT_USER_AGENT:
35 logger.warning("Sending requests with default User-Agent. " +
36 "Set 'user_agent' on api.Session to quiet this " +
39 if 'headers' in kwargs:
40 kwargs['headers']['User-Agent'] = str(user_agent)
42 kwargs['headers'] = {'User-Agent': str(user_agent)}
44 super().__init__(uri, *args, **kwargs)
46 self.pages = Pages(self)
48 An instance of :class:`mw.api.Pages`.
51 self.revisions = Revisions(self)
53 An instance of :class:`mw.api.Revisions`.
56 self.recent_changes = RecentChanges(self)
58 An instance of :class:`mw.api.RecentChanges`.
61 self.site_info = SiteInfo(self)
63 An instance of :class:`mw.api.SiteInfo`.
66 self.user_contribs = UserContribs(self)
68 An instance of :class:`mw.api.UserContribs`.
71 self.users = Users(self)
73 An instance of :class:`mw.api.Users`.
76 self.deleted_revisions = DeletedRevisions(self)
78 An instance of :class:`mw.api.DeletedRevisions`.
81 def login(self, username, password, token=None):
83 Performs a login operation. This method usually makes two requests to
84 API -- one to get a token and one to use the token to log in. If
85 authentication fails, this method will throw an
86 :class:`.errors.AuthenticationError`.
95 The response in a json :py:class:`dict`
102 'lgpassword': password,
103 'lgtoken': token, # If None, we'll be getting a token
109 if doc['login']['result'] == "Success":
111 elif doc['login']['result'] == "NeedToken":
113 if token is not None:
114 # Woops. We've been here before. Better error out.
115 raise AuthenticationError(doc)
117 token = doc['login']['token']
118 return self.login(username, password, token=token)
120 raise AuthenticationError(doc)
122 except KeyError as e:
123 raise MalformedResponse(e.message, doc)
126 def request(self, type, params, **kwargs):
127 params.update({'format': "json"})
129 doc = super().request(type, params, **kwargs).json()