RcGcDw/src/api/client.py

97 lines
3.9 KiB
Python
Raw Normal View History

2021-04-20 14:52:21 +00:00
# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
#
# RcGcDw is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RcGcDw is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
2021-04-27 13:10:29 +00:00
from __future__ import annotations
2021-04-24 09:19:38 +00:00
import src.misc
from typing import Union
from collections import OrderedDict
from typing import TYPE_CHECKING, Optional
2021-04-27 13:10:29 +00:00
if TYPE_CHECKING:
from src.wiki import Wiki
2021-04-20 14:52:21 +00:00
class Client:
2021-04-24 09:19:38 +00:00
"""
A client for interacting with RcGcDw when creating formatters or hooks.
"""
def __init__(self, hooks, wiki):
self._formatters = hooks
2021-04-27 13:10:29 +00:00
self.__recent_changes: Wiki = wiki
self.WIKI_API_PATH: str = src.misc.WIKI_API_PATH
self.WIKI_ARTICLE_PATH: str = src.misc.WIKI_ARTICLE_PATH
self.WIKI_SCRIPT_PATH: str = src.misc.WIKI_SCRIPT_PATH
self.WIKI_JUST_DOMAIN: str = src.misc.WIKI_JUST_DOMAIN
2021-04-24 09:19:38 +00:00
self.content_parser = src.misc.ContentParser
2021-04-28 11:37:32 +00:00
self.tags = self.__recent_changes.tags
self.LinkParser: type(src.misc.LinkParser) = src.misc.LinkParser
#self.make_api_request: src.rc.wiki.__recent_changes.api_request = self.__recent_changes.api_request
2021-04-24 09:19:38 +00:00
def refresh_internal_data(self):
"""Refreshes internal storage data for wiki tags and MediaWiki messages."""
self.__recent_changes.init_info()
2021-06-05 12:44:05 +00:00
@property
def namespaces(self) -> dict:
"""Return a dict of namespaces, if None return empty dict"""
if self.__recent_changes.namespaces is not None:
return self.__recent_changes.namespaces
else:
return dict()
2021-06-05 12:44:05 +00:00
def parse_links(self, summary: str):
link_parser = self.LinkParser()
link_parser.feed(summary)
return link_parser.new_string
def pull_curseprofile_comment(self, comment_id) -> Optional[str]:
"""Pulls a CurseProfile comment for current wiki set in the settings and with comment_id passed as an argument.
Returns:
String if comment was possible to be fetched
None if not
"""
return self.__recent_changes.pull_comment(comment_id)
2021-04-27 13:10:29 +00:00
def make_api_request(self, params: Union[str, OrderedDict], *json_path: str, timeout: int = 10, allow_redirects: bool = False):
"""Method to GET request data from the wiki's API with error handling including recognition of MediaWiki errors.
Parameters:
params (str, OrderedDict): a string or collections.OrderedDict object containing query parameters
json_path (str): *args taking strings as values. After request is parsed as json it will extract data from given json path
timeout (int, float) (default=10): int or float limiting time required for receiving a full response from a server before returning TimeoutError
allow_redirects (bool) (default=False): switches whether the request should follow redirects or not
2021-04-24 09:19:38 +00:00
Returns:
2021-04-20 14:52:21 +00:00
request_content (dict): a dict resulting from json extraction of HTTP GET request with given json_path
OR
One of the following exceptions:
ServerError: When connection with the wiki failed due to server error
ClientError: When connection with the wiki failed due to client error
KeyError: When json_path contained keys that weren't found in response JSON response
BadRequest: When params argument is of wrong type
MediaWikiError: When MediaWiki returns an error
"""
2021-04-27 13:10:29 +00:00
return self.__recent_changes.api_request(params, *json_path, timeout=timeout, allow_redirects=allow_redirects)
2021-04-20 14:52:21 +00:00
def get_formatters(self):
2021-04-27 13:10:29 +00:00
return self._formatters
def get_ipmapper(self) -> dict:
"""Returns a dict mapping IPs with amount of their edits"""
2021-05-22 13:59:36 +00:00
return self.__recent_changes.map_ips