ws.client.connection module¶
The ws.client.connection
module provides a low-level interface for
connections to the wiki. The requests.Session
class from the
requests
library is used to manage the cookies, authentication
and making requests.
- class ws.client.connection.Connection(api_url, index_url, session, timeout=60)¶
Bases:
object
The base object handling connection between a wiki and scripts.
- Parameters
api_url (str) – URL path to the wiki’s
api.php
entry pointindex_url (str) – URL path to the wiki’s
index.php
entry pointsession (requests.Session) – session created by
make_session()
timeout (int) – connection timeout in seconds
- static make_session(user_agent='wiki-scripts/1.2 (https://github.com/lahwaacz/wiki-scripts)', max_retries=0, cookie_file=None, cookiejar=None, http_user=None, http_password=None)¶
Creates a
requests.Session
object for the connection.It is possible to save the session data by specifying either
cookiejar
orcookie_file
arguments. Ifcookiejar
is present,cookie_file
is ignored.- Parameters
user_agent (str) – string sent as
User-Agent
header to the web servermax_retries (int) – Maximum number of retries for each connection. Applies only to failed DNS lookups, socket connections and connection timeouts, never to requests where data has made it to the server.
cookie_file (str) – path to a
cookielib.FileCookieJar
filecookiejar – an existing
cookielib.CookieJar
object
- Returns
requests.Session
object
- static set_argparser(argparser)¶
Add arguments for constructing a
Connection
object to an instance ofargparse.ArgumentParser
.See also the
ws.config
module.- Parameters
argparser – an instance of
argparse.ArgumentParser
- classmethod from_argparser(args)¶
Construct a
Connection
object from arguments parsed byargparse.ArgumentParser
.- Parameters
args – an instance of
argparse.Namespace
.- Returns
an instance of
Connection
- request(method, url, **kwargs)¶
Simple HTTP request handler. It is basically a wrapper around
requests.request()
using the established session including cookies, so it should be used only for connections withurl
leading to the same site.The parameters are the same as for
requests.request()
, see Requests documentation for details.There is no translation of exceptions, the
requests
exceptions (notablyrequests.exceptions.ConnectionError
,requests.exceptions.Timeout
andrequests.exceptions.HTTPError
) should be catched by the caller.
- call_api(params=None, *, expand_result=True, check_warnings=True, **kwargs)¶
Convenient method to call the
api.php
entry point.Checks the
action
parameter (default is"help"
as in the API), selects correct HTTP request method, handles API errors and warnings.Parameters of the call can be passed either as a dict to
params
, or as keyword arguments.params
andkwargs
cannot be specified at the same time.- Parameters
params – dictionary of API parameters
expand_result – if
True
, return only part of the response relevant to the given action, otherwise full response is returnedcheck_warnings – if
True
, the response is investigated and all API warnings are output into the loggerkwargs – API parameters passed as keyword arguments
- Returns
a dictionary containing (part of) the API response
- call_index(method='GET', **kwargs)¶
Convenient method to call the
index.php
entry point.Currently it only calls
self.request()
with specific URL, default method"GET"
and otherkwargs
(at least"params"
or"data"
should be specified).See MediaWiki for possible parameters to
index.php
.
- get_hostname()¶
- Returns
the hostname part of self.api_url
- exception ws.client.connection.APIWrongAction(action, available)¶
Bases:
Exception
Raised when a wrong API action is specified.
This is a programming error, it should be fixed in the client code.