@@ -22,30 +22,21 @@ class WAction:
22
22
"""Collection of functions which can perform write actions on a Wiki"""
23
23
24
24
@staticmethod
25
- def _post_action (wiki : Wiki , action : str , form : dict = None , apply_token : bool = True , timeout : int = 15 , extra_args : dict = None ) -> dict :
26
- """Convienence method, performs the actual POST of the action to the server .
25
+ def _action_and_validate (wiki : Wiki , action : str , form : dict = None , apply_token : bool = True , timeout : int = 15 , success_vals : tuple = ( "Success" ,) , extra_args : dict = None ) -> dict :
26
+ """Performs a `_post_action()` and checks the results for errors. If there is an error, it will be logged accordingly .
27
27
28
28
Args:
29
29
wiki (Wiki): The Wiki object to use
30
- action (str): The action to perform.
30
+ action (str): The id of the action to perform.
31
31
form (dict, optional): The parameters to POST to the server, if applicable. Defaults to None.
32
32
apply_token (bool, optional): Set `True` to also send the Wiki's csrf token in the POST. Defaults to True.
33
+ timeout (int, optional): The length of time (in seconds) to wait before marking the action as failed. Defaults to 15.
34
+ success_vals (tuple, optional): The keyword responses returned by the server which indicate a successful action. Optional, set `None` to skip this check. Defaults to ("Success",).
35
+ extra_args (dict, optional): Any `kwargs` that should be passed to the underlying requests Session object when performing a POST. Defaults to None.
33
36
34
37
Returns:
35
- dict: The response from the server. Empty dict if there was an error .
38
+ dict: The json response from the server, or `None` if something went wrong .
36
39
"""
37
- pl = make_params (action , form ) | ({"token" : wiki .csrf_token } if apply_token else {})
38
-
39
- try :
40
- return wiki .client .post (wiki .endpoint , data = pl , ** ({"timeout" : timeout } | (extra_args or {}))).json ()
41
- except Exception :
42
- log .error ("%s: Could not reach server or read response while performing %s with params %s" , wiki , action , pl , exc_info = True )
43
-
44
- return {}
45
-
46
- @staticmethod
47
- def _action_and_validate (wiki : Wiki , action : str , form : dict = None , apply_token : bool = True , timeout : int = 15 , success_vals : tuple = ("Success" ,), extra_args : dict = None ) -> dict :
48
-
49
40
if not (response := WAction ._post_action (wiki , action , form , apply_token , timeout , extra_args )):
50
41
log .error ("%s: No response from server while trying to perform action '%s'" , wiki , action )
51
42
log .debug ("Sent parameters: %s" , form )
@@ -56,12 +47,36 @@ def _action_and_validate(wiki: Wiki, action: str, form: dict = None, apply_token
56
47
log .debug (response )
57
48
return
58
49
59
- if (status := mine_for (response , action , "result" )) in success_vals :
50
+ if not success_vals or (status := mine_for (response , action , "result" )) in success_vals :
60
51
return response
61
52
62
53
log .error ("%s: Failed to perform action '%s', got bad result from server: %s" , wiki , action , status )
63
54
log .debug (response )
64
55
56
+ @staticmethod
57
+ def _post_action (wiki : Wiki , action : str , form : dict = None , apply_token : bool = True , timeout : int = 15 , extra_args : dict = None ) -> dict :
58
+ """Convienence method, performs the actual POST of the action to the server.
59
+
60
+ Args:
61
+ wiki (Wiki): The Wiki object to use
62
+ action (str): The action to perform.
63
+ form (dict, optional): The parameters to POST to the server, if applicable. Defaults to None.
64
+ apply_token (bool, optional): Set `True` to also send the Wiki's csrf token in the POST. Defaults to True.
65
+ timeout (int, optional): The length of time (in seconds) to wait before marking the action as failed. Defaults to 15.
66
+ extra_args (dict, optional): Any `kwargs` that should be passed to the underlying requests Session object when performing a POST. Defaults to None.
67
+
68
+ Returns:
69
+ dict: The response from the server. Empty dict if there was an error.
70
+ """
71
+ pl = make_params (action , form ) | ({"token" : wiki .csrf_token } if apply_token else {})
72
+
73
+ try :
74
+ return wiki .client .post (wiki .endpoint , data = pl , ** ({"timeout" : timeout } | (extra_args or {}))).json ()
75
+ except Exception :
76
+ log .error ("%s: Could not reach server or read response while performing %s with params %s" , wiki , action , pl , exc_info = True )
77
+
78
+ return {}
79
+
65
80
@staticmethod
66
81
def delete (wiki : Wiki , title : str , reason : str ) -> bool :
67
82
"""Deletes a page. PRECONDITION: `wiki` must be logged in and have the ability to delete pages for this to work.
@@ -74,7 +89,7 @@ def delete(wiki: Wiki, title: str, reason: str) -> bool:
74
89
Returns:
75
90
bool: `True` if this action succeeded.
76
91
"""
77
- return bool (WAction ._action_and_validate (wiki , "delete" , {"title" : title , "reason" : reason }))
92
+ return bool (WAction ._action_and_validate (wiki , "delete" , {"title" : title , "reason" : reason }, success_vals = None ))
78
93
79
94
@staticmethod
80
95
def edit (wiki : Wiki , title : str , text : str = None , summary : str = "" , prepend : str = None , append : str = None , minor : bool = False ) -> bool :
@@ -83,10 +98,10 @@ def edit(wiki: Wiki, title: str, text: str = None, summary: str = "", prepend: s
83
98
Args:
84
99
wiki (Wiki): The Wiki to use.
85
100
title (str): The title to edit.
86
- text (str, optional): Text to replace the current page's contents with. Mutually exclusive with `prepend`/`append`. Defaults to None.
101
+ text (str, optional): Text to replace the current page's contents with. Overrides `prepend`/`append`. Defaults to None.
87
102
summary (str, optional): The edit summary to use. Defaults to "".
88
- prepend (str, optional): Text to prepend to the page. Mutually exclusive with `text`. Defaults to None.
89
- append (str, optional): Text to append to the page. Mutually exclusive with `text`. Defaults to None.
103
+ prepend (str, optional): Text to prepend to the page. Defaults to None.
104
+ append (str, optional): Text to append to the page. Defaults to None.
90
105
minor (bool, optional): Set `True` to mark this edit as minor. Defaults to False.
91
106
92
107
Raises:
0 commit comments