Skip to content

Add optional HTTP origin parameter to Session. #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions mwapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Session:
:Parameters:
host : `str`
Host to which to connect to. Must include http:// or https:// and
no trailing "/".
no trailing "/", unless "origin" is also specified.
user_agent : `str`
The User-Agent header to include with all requests. Use this field
to identify your script/bot/application to system admins of the
Expand All @@ -52,16 +52,25 @@ class Session:
is to hang indefinitely.
session : `requests.Session`
(optional) a `requests` session object to use
origin: `str`
An alternate host to query instead of the host that corresponds
to the literal value of the "host" parameter. Useful for interacting
with load balancers and internal hostnames.
"""

def __init__(self, host, user_agent=None, formatversion=None,
api_path=None,
timeout=None, session=None, **session_params):
timeout=None, session=None, origin=None,
**session_params):
self.host = str(host)
self.formatversion = int(formatversion) \
if formatversion is not None else None
self.api_path = str(api_path or "/w/api.php")
self.api_url = self.host + self.api_path
if origin:
self.api_url = origin + self.api_path
else:
self.api_url = self.host + self.api_path

self.timeout = float(timeout) if timeout is not None else None
self.session = session or requests.Session()
for key, value in session_params.items():
Expand All @@ -77,6 +86,9 @@ def __init__(self, host, user_agent=None, formatversion=None,
else:
self.headers['User-Agent'] = user_agent

if origin:
self.headers['Host'] = self.host

def _request(self, method, params=None, files=None, auth=None):
params = params or {}
if self.formatversion is not None:
Expand Down