routes
HTTP API framework similar to flask.
Attributes
OK = 200
module-attribute
ACCEPTED = 202
module-attribute
INTERNAL_SERVER_ERROR = 500
module-attribute
NO_CONTENT = 204
module-attribute
Classes
Request(path='/', method='GET', data=None, params=None, query_params=None)
Bases: object
Request wrapper object.
Source code in pyrevitlib/pyrevit/routes/server/base.py
Attributes
path = path
instance-attribute
method = method
instance-attribute
data = data
instance-attribute
headers
property
Request headers, looked up without regard to the case of the name.
params
property
Request parameters.
query_params
property
Request query string parameters as a dictionary.
callback_url
property
Request callback url, if provided in payload.
Methods:
add_header(key, value)
Add new header key:value, replacing any value already held for the name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Header name, matched without regard to case. |
required |
value
|
str
|
Header value. |
required |
Source code in pyrevitlib/pyrevit/routes/server/base.py
Response(status=200, data=None, headers=None)
Bases: object
Response wrapper object.
Source code in pyrevitlib/pyrevit/routes/server/base.py
Attributes
status = status
instance-attribute
data = data
instance-attribute
headers
property
Response headers dict.
Methods:
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
Attributes
name = name
instance-attribute
Methods:
route(pattern, methods=['GET'])
Define a new route on this API.
Source code in pyrevitlib/pyrevit/routes/__init__.py
Functions:
init()
Initialize routes. Reset all registered routes and shutdown servers.
activate_server()
Activate routes server for this host instance.
The server module is imported on first activation, so that sessions with the routes server disabled never import the HTTP stack. Each server owns its request queue and Revit external event, which must be created on the host's main thread. Activation must therefore run on the main thread.
Important
Activation is idempotent within a session: an already active server serves the routes that are registered now, so handing it back is correct. It is not how a server survives a reload - init() still stops the old one on every session load.
Idempotency is only claimed of a server that is still accepting. A server whose accept loop has exited is torn down and replaced, because handing that one back is the same dead registration that #3473 was reported for, reached from the other end.
Returns:
| Type | Description |
|---|---|
RoutesServer
|
the active server, or None if it could not be started. |
Source code in pyrevitlib/pyrevit/routes/server/__init__.py
deactivate_server()
Deactivate the active routes server for this host instance.
Deregisters whether or not the stop succeeded (#3473). A server that is no longer listening but is still in the env var made the next activation hand that dead server back instead of binding the port, so a reload silently lost routes.
Every step is contained, including the one that clears the env var: a failure there must not skip the deregistration, which is the step that keeps the next session from being handed this server.
Source code in pyrevitlib/pyrevit/routes/server/__init__.py
get_active_server()
make_response(data, status=OK, headers=None)
Create Reponse object with.
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 |
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
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 |