flare.network

Tools for handling Ajax/fetch-network requests

Module Contents

Classes

DeferredCall

Calls the given function with a fixed delay.

HTTPRequest

Wrapper around XMLHttpRequest.

NetworkService

Generic wrapper around ajax requests.

requestGroup

Functions

fetch_json(url, callback, **kwargs)

Wrapper that performs a fetch request (with all parameters related to [pyfetch](https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch).

NiceError(req, code[, params, then])

Displays a descriptive error message using an Alert dialog to the user.

NiceErrorAndThen(function)

Returns a callback which first displays a descriptive error message to the user and then calls another function.

processSkelQueue()

getUrlHashAsString([urlHash])

getUrlHashAsObject([urlHash])

setUrlHash(hash[, param])

Attributes

skeyRequestQueue

flare.network.fetch_json(url, callback, **kwargs)

Wrapper that performs a fetch request (with all parameters related to [pyfetch](https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch).

Parameters:
class flare.network.DeferredCall(func, *args, **kwargs)

Bases: object

Calls the given function with a fixed delay.

This allows assuming that calls to NetworkService are always asynchronous, so its guaranteed that any initialization code can run before the Network-Call yields results.

run()

Internal callback that executes the callback function.

class flare.network.HTTPRequest(method, url, callbackSuccess=None, callbackFailure=None, payload=None, content_type=None, response_type=None, asynchronous=True)

Bases: object

Wrapper around XMLHttpRequest.

onReadyStateChange(*args, **kwargs)

Internal callback.

flare.network.NiceError(req, code, params='', then=None)

Displays a descriptive error message using an Alert dialog to the user.

flare.network.NiceErrorAndThen(function)

Returns a callback which first displays a descriptive error message to the user and then calls another function.

flare.network.skeyRequestQueue = []
flare.network.processSkelQueue()
class flare.network.NetworkService(module, url, params, successHandler, failureHandler, finishedHandler, modifies, secure, kickoff, group=None)

Bases: object

Generic wrapper around ajax requests.

Handles caching and multiplexing multiple concurrent requests to the same resource. It also acts as the central proxy to notify currently active widgets of changes made to data on the server.

changeListeners = []
host = ''
prefix = '/json'
defaultFailureHandler
retryCodes
retryMax = 3
retryDelay = 5000
static notifyChange(module, **kwargs)

Broadcasts a change made to data of module ‘module’ to all currently registered changeListeners.

Parameters:

module (str) – Name of the module where the change occured

static registerChangeListener(listener)

Registers object ‘listener’ for change notifications.

‘listener’ must provide an ‘onDataChanged’ function accepting one parameter: the name of the module. Does nothing if that object has already registered. :param listener: The object to register :type listener: object

static removeChangeListener(listener)

Unregisters the object ‘listener’ from change notifications.

Parameters:

listener (object) – The object to unregister. It must be currently registered.

static genReqStr(params)
static decode(req)

Decodes a response received from the server (ie parsing the json).

Returns:

object

static isOkay(req)
static urlForArgs(module, path)

Constructs the final url for that request.

If module is given, it prepends “/prefix” If module is None, path is returned unchanged. :param module: Name of the target module or None :type module: str or None :param path: Path (either relative to ‘module’ or absolute if ‘module’ is None :type path: str :returns: str

kickoff()
static request(module, url, params=None, successHandler=None, failureHandler=None, finishedHandler=None, modifies=False, secure=False, kickoff=True, group=None)

Performs an AJAX request. Handles caching and security-keys.

Calls made to this function are guaranteed to be async.

Parameters:
  • module (str or None) – Target module on the server. Set to None if you want to call anything else

  • url (str or None) – The path (relative to module) or a full url if module is None

  • successHandler (callable) – function beeing called if the request succeeds. Must take one argument (the request).

  • failureHandler (callable) – function beeing called if the request failes. Must take two arguments (the request and an error-code).

  • finishedHandler (callable) – function beeing called if the request finished (regardless wherever it succeeded or not). Must take one argument (the request).

  • modifies (bool) – If set to True, it will automatically broadcast an onDataChanged event for that module.

  • secure (bool) – If true, include a fresh securitykey in this request. Defaults to False.

doFetch(url, params, skey)

Internal function performing the actual AJAX request.

onCompletion(text)

Internal hook for the AJAX call.

onError(text, code)

Internal hook for the AJAX call.

onTimeout(text)

Internal hook for the AJAX call.

clear()
onFinished(success)
class flare.network.requestGroup(callback=None)
addRequest(request)
call()
onFinished(success)
flare.network.getUrlHashAsString(urlHash=None)
flare.network.getUrlHashAsObject(urlHash=None)
flare.network.setUrlHash(hash, param=None)