Skip to content

routes

HTTP API framework similar to flask.

Classes

Request(path='/', method='GET', data=None, params=None)

Bases: object

Request wrapper object.

Source code in pyrevitlib/pyrevit/routes/server/base.py
def __init__(self, path='/', method='GET', data=None, params=None):
    self.path = path
    self.method = method
    self.data = data
    self._headers = {}
    self._params = params or []

Attributes

path = path instance-attribute
method = method instance-attribute
data = data instance-attribute
headers property

Request headers dict.

params property

Request parameters.

callback_url property

Request callback url, if provided in payload.

Functions

add_header(key, value)

Add new header key:value.

Source code in pyrevitlib/pyrevit/routes/server/base.py
def add_header(self, key, value):
    """Add new header key:value."""
    self._headers[key] = value

Response(status=200, data=None, headers=None)

Bases: object

Response wrapper object.

Source code in pyrevitlib/pyrevit/routes/server/base.py
def __init__(self, status=200, data=None, headers=None):
    self.status = status
    self.data = data
    self._headers = headers or {}

Attributes

status = status instance-attribute
data = data instance-attribute
headers property

Response headers dict.

Functions

add_header(key, value)

Add new header key:value.

Source code in pyrevitlib/pyrevit/routes/server/base.py
def add_header(self, key, value):
    """Add new header key:value."""
    self._headers[key] = value

API(name)

Bases: object

API root object.

Parameters:

Name Type Description Default
name str

URL-safe unique root name of the API

required

Examples:

from pyrevit import routes
api = routes.API("pyrevit-core")
@api.route('/sessions/', methods=['POST'])
def reload_pyrevit(uiapp):
    new_session_id = sessionmgr.reload_pyrevit()
    return {"session_id": new_session_id}
Source code in pyrevitlib/pyrevit/routes/__init__.py
def __init__(self, name):
    self.name = name

Attributes

name = name instance-attribute

Functions

route(pattern, methods=['GET'])

Define a new route on this API.

Source code in pyrevitlib/pyrevit/routes/__init__.py
def route(self, pattern, methods=['GET']):
    """Define a new route on this API."""
    def __func_wrapper__(f):
        for method in methods:
            add_route(
                api_name=self.name,
                pattern=pattern,
                method=method,
                handler_func=f
                )
        return f
    return __func_wrapper__

Functions

init()

Initialize routes. Reset all registered routes and shutdown servers.

Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def init():
    """Initialize routes. Reset all registered routes and shutdown servers."""
    # clear all routes
    router.reset_routes()
    # stop existing server
    deactivate_server()

activate_server()

Activate routes server for this host instance.

Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def activate_server():
    """Activate routes server for this host instance."""
    routes_server = envvars.get_pyrevit_env_var(envvars.ROUTES_SERVER)
    if not routes_server:
        try:
            rsinfo = serverinfo.register()
            routes_server = \
                server.RoutesServer(
                    host=rsinfo.server_host,
                    port=rsinfo.server_port
                    )
            routes_server.start()
            envvars.set_pyrevit_env_var(envvars.ROUTES_SERVER, routes_server)
            return routes_server
        except Exception as rs_ex:
            serverinfo.unregister()
            mlogger.error("Error starting Routes server | %s", str(rs_ex))

deactivate_server()

Deactivate the active routes server for this host instance.

Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def deactivate_server():
    """Deactivate the active routes server for this host instance."""
    routes_server = envvars.get_pyrevit_env_var(envvars.ROUTES_SERVER)
    if routes_server:
        try:
            routes_server.stop()
            envvars.set_pyrevit_env_var(envvars.ROUTES_SERVER, None)
            serverinfo.unregister()
        except Exception as rs_ex:
            mlogger.error("Error stopping Routes server | %s", str(rs_ex))

get_active_server()

Get active routes server for this host instance.

Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def get_active_server():
    """Get active routes server for this host instance."""
    return envvars.get_pyrevit_env_var(envvars.ROUTES_SERVER)

make_response(data, status=OK, headers=None)

Create Reponse object with.

Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def make_response(data, status=OK, headers=None):
    """Create Reponse object with."""
    res = Response(status=status, data=data)
    for key, value in (headers or {}).items():
        res.add_header(key, value)
    return res

get_routes(api_name)

Get all registered routes for given API name.

Parameters:

Name Type Description Default
api_name str

unique name of the api

required
Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def get_routes(api_name):
    """Get all registered routes for given API name.

    Args:
        api_name (str): unique name of the api
    """
    return router.get_routes(api_name)

add_route(api_name, pattern, method, handler_func)

Add new route for given API name.

Parameters:

Name Type Description Default
api_name str

unique name of the api

required
pattern str

route pattern

required
method str

method name

required
handler_func function

route handler function

required
Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def add_route(api_name, pattern, method, handler_func):
    """Add new route for given API name.

    Args:
        api_name (str): unique name of the api
        pattern (str): route pattern
        method (str): method name
        handler_func (function): route handler function
    """
    return router.add_route(api_name, pattern, method, handler_func)

remove_route(api_name, pattern, method)

Remove previously registered route for given API name.

Parameters:

Name Type Description Default
api_name str

unique name of the api

required
pattern str

route pattern

required
method str

method name

required
Source code in pyrevitlib/pyrevit/routes/server/__init__.py
def remove_route(api_name, pattern, method):
    """Remove previously registered route for given API name.

    Args:
        api_name (str): unique name of the api
        pattern (str): route pattern
        method (str): method name
    """
    return router.remove_route(api_name, pattern, method)

active_routes_api()

Activates routes API.

Source code in pyrevitlib/pyrevit/routes/__init__.py
def active_routes_api():
    """Activates routes API."""
    from pyrevit.routes import api