Skip to content

Commit

Permalink
better exception for forbidden status code (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust authored Jul 15, 2024
1 parent eb97aef commit 98e2423
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## Welcome!!
## Welcome

Welcome to the pymediawiki: a python MediaWiki API wrapper project. I hope that
you have found the project to be useful. If you are here, you must want to help
Expand Down Expand Up @@ -165,4 +165,4 @@ A special thanks to all the code contributors to `pymediawiki`!
* [@rubabredwan](https://github.com/rubabredwan) - Fix for `suggest` [#85](https://github.com/barrust/mediawiki/pull/85)
* [@ldorigo](https://github.com/ldorigo) - Pulling links for header sections [#90](https://github.com/barrust/mediawiki/pull/90)
* [@tbm](https://github.com/tbm) - `categorymember` improvements [PR #100](https://github.com/barrust/mediawiki/pull/100)
* [@dnanto](https://github.com/dnanto) - Determining `available_languages` [PR #116](https://github.com/barrust/mediawiki/pull/116)
* [@dnanto](https://github.com/dnanto) - Determining `available_languages` [PR #116](https://github.com/barrust/mediawiki/pull/116)
3 changes: 3 additions & 0 deletions mediawiki/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""
mediawiki module initialization
"""

from mediawiki.configuraton import URL, VERSION
from mediawiki.exceptions import (
DisambiguationError,
HTTPTimeoutError,
MediaWikiAPIURLError,
MediaWikiCategoryTreeError,
MediaWikiException,
MediaWikiForbidden,
MediaWikiGeoCoordError,
MediaWikiLoginError,
PageError,
Expand Down Expand Up @@ -38,4 +40,5 @@
"MediaWikiGeoCoordError",
"MediaWikiCategoryTreeError",
"MediaWikiLoginError",
"MediaWikiForbidden",
]
9 changes: 9 additions & 0 deletions mediawiki/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
MediaWiki Exceptions
"""

from typing import Dict, List, Optional

from mediawiki.utilities import str_or_unicode
Expand Down Expand Up @@ -249,3 +250,11 @@ def __init__(self, error: str):
def error(self) -> str:
"""str: The error message that the MediaWiki site returned"""
return self._error


class MediaWikiForbidden(MediaWikiBaseException):
"""Exception raised when a forbidden status code is returned"""

def __init__(self, error: str):
self._error = error
super().__init__(self._error)
13 changes: 10 additions & 3 deletions mediawiki/mediawiki.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
MediaWiki class module
"""

# MIT License
# Author: Tyler Barrus ([email protected])

Expand All @@ -19,6 +20,7 @@
MediaWikiAPIURLError,
MediaWikiCategoryTreeError,
MediaWikiException,
MediaWikiForbidden,
MediaWikiGeoCoordError,
MediaWikiLoginError,
PageError,
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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 {}

Expand Down

0 comments on commit 98e2423

Please sign in to comment.