Skip to content

Commit

Permalink
Proxy support (#106)
Browse files Browse the repository at this point in the history
* add proxy support

* add page.wikitext to CHANGELOG.md
  • Loading branch information
barrust committed Jan 1, 2021
1 parent b2013c5 commit 4cade88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# MediaWiki Changelog

## Future Release

* Add `page.wikitext` support for pulling the page contents as [wikitext](https://en.wikipedia.org/wiki/Help:Wikitext)
* Add [proxy support](https://requests.readthedocs.io/en/master/user/advanced/#proxies) by passing info directly to the requests library.

## Version 0.7.0

* Remove support for ***python 2.7***!
Expand Down
35 changes: 30 additions & 5 deletions mediawiki/mediawiki.py
Expand Up @@ -43,7 +43,9 @@ class MediaWiki(object):
setting a unique one and not using the \
library's default user-agent string
username (str): The username to use to log into the MediaWiki
password (str): The password to use to log into the MediaWiki """
password (str): The password to use to log into the MediaWiki
proxies (str): A dictionary of specific proxies to use in the \
Requests libary."""

__slots__ = [
"_version",
Expand All @@ -65,6 +67,7 @@ class MediaWiki(object):
"_refresh_interval",
"_use_cache",
"_is_logged_in",
"_proxies",
]

def __init__(
Expand All @@ -78,6 +81,7 @@ def __init__(
user_agent=None,
username=None,
password=None,
proxies=None
):
""" Init Function """
self._version = VERSION
Expand All @@ -87,12 +91,17 @@ def __init__(
self.category_prefix = cat_prefix
self._timeout = None
self.timeout = timeout
# requests library parameters
self._session = None
self._user_agent = ("python-mediawiki/VERSION-{0}" "/({1})/BOT").format(
VERSION, URL
)
self._proxies = None
# set libary parameters
if user_agent is not None:
self.user_agent = user_agent
self._session = None
self.proxies = proxies # this will call self._reset_session()

self._rate_limit = None
self.rate_limit = bool(rate_limit)
self._rate_limit_last_call = None
Expand All @@ -108,9 +117,6 @@ def __init__(
self._refresh_interval = None
self._use_cache = True

# call helper functions to get everything set up
self._reset_session()

# for login information
self._is_logged_in = False
if password is not None and username is not None:
Expand Down Expand Up @@ -167,6 +173,20 @@ def rate_limit(self, rate_limit):
self._rate_limit_last_call = None
self.clear_memoized()

@property
def proxies(self):
""" dict: Turn on, off, or set proxy use with the Requests library """
return self._proxies

@proxies.setter
def proxies(self, proxies):
""" Turn on, off, or set proxy use through the Requests library """
if proxies and isinstance(proxies, dict):
self._proxies = proxies
else:
self._proxies = None
self._reset_session()

@property
def use_cache(self):
""" bool: Whether caching should be used; on (**True**) or off \
Expand Down Expand Up @@ -382,9 +402,14 @@ def set_api_url(

def _reset_session(self):
""" Set session information """
if self._session:
self._session.close()

headers = {"User-Agent": self._user_agent}
self._session = requests.Session()
self._session.headers.update(headers)
if self._proxies is not None:
self._session.proxies.update(self._proxies)
self._is_logged_in = False

def clear_memoized(self):
Expand Down

0 comments on commit 4cade88

Please sign in to comment.