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:
objectThe base object handling connection between a wiki and scripts.
- Parameters
api_url (str) – URL path to the wiki’s
api.phpentry pointindex_url (str) – URL path to the wiki’s
index.phpentry 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.Sessionobject for the connection.It is possible to save the session data by specifying either
cookiejarorcookie_filearguments. Ifcookiejaris present,cookie_fileis ignored.- Parameters
user_agent (str) – string sent as
User-Agentheader 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.FileCookieJarfilecookiejar – an existing
cookielib.CookieJarobject
- Returns
requests.Sessionobject
- static set_argparser(argparser)¶
Add arguments for constructing a
Connectionobject to an instance ofargparse.ArgumentParser.See also the
ws.configmodule.- Parameters
argparser – an instance of
argparse.ArgumentParser
- classmethod from_argparser(args)¶
Construct a
Connectionobject 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 withurlleading 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
requestsexceptions (notablyrequests.exceptions.ConnectionError,requests.exceptions.Timeoutandrequests.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.phpentry point.Checks the
actionparameter (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.paramsandkwargscannot 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.phpentry 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:
ExceptionRaised when a wrong API action is specified.
This is a programming error, it should be fixed in the client code.