Skip to content

Commit

Permalink
Update wbi_helpers.py (#255)
Browse files Browse the repository at this point in the history
Add a default session instead of recreating a new one everytime.
  • Loading branch information
LeMyst authored Nov 28, 2021
1 parent 65e7147 commit a081e1f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class BColors:
UNDERLINE = '\033[4m'


# Session used for anonymous requests
default_session = requests.Session()


def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Session = None, max_retries: int = 100, retry_after: int = 60, **kwargs: Any) -> Dict:
"""
:param method: 'GET' or 'POST'
Expand All @@ -53,7 +57,7 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
raise ValueError("'format' can only be 'json' when using mediawiki_api_call()")

response = None
session = session if session else requests.Session()
session = session if session else default_session
for n in range(max_retries):
try:
response = session.request(method=method, url=mediawiki_api_url, **kwargs)
Expand Down Expand Up @@ -138,7 +142,7 @@ def mediawiki_api_call_helper(data: Dict[str, Any] = None, login: Login = None,
}

if data is not None:
if login is not None and 'token' not in data:
if not allow_anonymous and login is not None and 'token' not in data:
data.update({'token': login.get_edit_token()})
elif 'token' not in data:
data.update({'token': '+\\'})
Expand Down

0 comments on commit a081e1f

Please sign in to comment.