Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/201159

Change subject: Detect and add the appropriate tokens to API requests
......................................................................

Detect and add the appropriate tokens to API requests

Bug: T78393
Change-Id: I9e93ca978f67eb7c2517bd32f2efeb664833dbb3
---
M pywikibot/data/api.py
M tests/api_tests.py
2 files changed, 23 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/59/201159/1

diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 70e512b..669fb3f 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1390,6 +1390,17 @@
         """Return internal representation."""
         return "%s.%s<%s->%r>" % (self.__class__.__module__, 
self.__class__.__name__, self.site, str(self))
 
+    @property
+    def _tokens(self):
+        return dict((param['name'], param['tokentype'])
+                    for param in 
self.site._paraminfo[self.action]['parameters']
+                    if 'tokentype' in param)
+
+    def _add_tokens(self):
+        for name, tokentype in self._tokens.items():
+            if name not in self._params:
+                self[name] = self.site.tokens[tokentype]
+
     def _simulate(self, action):
         """Simualte action."""
         if action and config.simulate and (self.write or action in 
config.actions_to_block):
@@ -1501,6 +1512,8 @@
 
         """
         self._add_defaults()
+        if self.write:
+            self._add_tokens()
         if (not config.enable_GET_without_SSL and
                 self.site.protocol() != 'https'):
             use_get = False
diff --git a/tests/api_tests.py b/tests/api_tests.py
index d827f9c..9acea4b 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -57,6 +57,16 @@
         for item in req.items():
             self.assertEqual(len(item), 2, item)
 
+    def test_token_detection(self):
+        """Test the Request._add_tokens() method."""
+        mysite = self.get_site()
+        req = api.Request(site=mysite, action='edit')
+        self.assertIn('action', req)
+        self.assertNotIn('token', req)
+        req._add_tokens()
+        self.assertIn('action', req)
+        self.assertIn('token', req)
+
 
 class TestParamInfo(DefaultSiteTestCase):
 

-- 
To view, visit https://gerrit.wikimedia.org/r/201159
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9e93ca978f67eb7c2517bd32f2efeb664833dbb3
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to