-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better exception for forbidden status code (#140)
- Loading branch information
Showing
4 changed files
with
24 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
MediaWiki class module | ||
""" | ||
|
||
# MIT License | ||
# Author: Tyler Barrus ([email protected]) | ||
|
||
|
@@ -19,6 +20,7 @@ | |
MediaWikiAPIURLError, | ||
MediaWikiCategoryTreeError, | ||
MediaWikiException, | ||
MediaWikiForbidden, | ||
MediaWikiGeoCoordError, | ||
MediaWikiLoginError, | ||
PageError, | ||
|
@@ -335,7 +337,6 @@ def login(self, username: str, password: str, strict: bool = True) -> bool: | |
res = self._post_response(params) | ||
if res["login"]["result"] == "Success": | ||
self._is_logged_in = True | ||
self._config._login = False | ||
return True | ||
self._is_logged_in = False | ||
reason = res["login"]["reason"] | ||
|
@@ -978,14 +979,20 @@ def __cat_tree_rec( | |
def _get_response(self, params: Dict[str, Any]) -> Dict[str, Any]: | ||
"""wrap the call to the requests package""" | ||
try: | ||
return self._session.get(self._config.api_url, params=params, timeout=self._config.timeout).json() | ||
r = self._session.get(self._config.api_url, params=params, timeout=self._config.timeout) | ||
if r.status_code == 403: | ||
raise MediaWikiForbidden(f"{self.api_url} return a 403 Forbidden; likely need to login!") | ||
return r.json() | ||
except JSONDecodeError: | ||
return {} | ||
|
||
def _post_response(self, params: Dict[str, Any]) -> Dict[str, Any]: | ||
"""wrap a post call to the requests package""" | ||
try: | ||
return self._session.post(self._config.api_url, data=params, timeout=self._config.timeout).json() | ||
r = self._session.post(self._config.api_url, data=params, timeout=self._config.timeout) | ||
if r.status_code == 403: | ||
raise MediaWikiForbidden(f"{self.api_url} return a 403 Forbidden; likely need to login!") | ||
return r.json() | ||
except JSONDecodeError: | ||
return {} | ||
|
||
|