2021-12-02 02:26:19 +00:00
|
|
|
"""
|
|
|
|
|
Example of proxy-provided caps
|
|
|
|
|
|
|
|
|
|
Useful for mocking out a cap that isn't actually implemented by the server
|
|
|
|
|
while developing the viewer-side pieces of it.
|
|
|
|
|
|
|
|
|
|
Implements a cap that accepts an `obj_id` UUID query parameter and returns
|
|
|
|
|
the name of the object.
|
|
|
|
|
"""
|
|
|
|
|
import asyncio
|
2021-12-02 05:54:53 +00:00
|
|
|
import asgiref.wsgi
|
2021-12-02 02:26:19 +00:00
|
|
|
|
2021-12-02 05:54:53 +00:00
|
|
|
from flask import Flask, Response, request
|
2021-12-02 02:26:19 +00:00
|
|
|
|
|
|
|
|
from hippolyzer.lib.base.datatypes import UUID
|
2021-12-02 05:54:53 +00:00
|
|
|
from hippolyzer.lib.proxy import addon_ctx
|
|
|
|
|
from hippolyzer.lib.proxy.webapp_cap_addon import WebAppCapAddon
|
|
|
|
|
|
|
|
|
|
app = Flask("GetObjectNameCapApp")
|
2021-12-02 02:26:19 +00:00
|
|
|
|
|
|
|
|
|
2021-12-02 05:54:53 +00:00
|
|
|
@app.route('/')
|
|
|
|
|
async def get_object_name():
|
|
|
|
|
# Should always have the current region, the cap handler is bound to one.
|
|
|
|
|
# Just need to pull it from the `addon_ctx` module's global.
|
|
|
|
|
obj_mgr = addon_ctx.region.get().objects
|
|
|
|
|
obj_id = UUID(request.args['obj_id'])
|
|
|
|
|
obj = obj_mgr.lookup_fullid(obj_id)
|
|
|
|
|
if not obj:
|
|
|
|
|
return Response(f"Couldn't find {obj_id!r}", status=404, mimetype="text/plain")
|
2021-12-02 02:26:19 +00:00
|
|
|
|
2021-12-02 05:54:53 +00:00
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(obj_mgr.request_object_properties(obj)[0], 1.0)
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
return Response(f"Timed out requesting {obj_id!r}'s properties", status=500, mimetype="text/plain")
|
2021-12-02 02:26:19 +00:00
|
|
|
|
2021-12-02 05:54:53 +00:00
|
|
|
return Response(obj.Name, mimetype="text/plain")
|
2021-12-02 02:26:19 +00:00
|
|
|
|
|
|
|
|
|
2021-12-02 05:54:53 +00:00
|
|
|
class MockProxyCapExampleAddon(WebAppCapAddon):
|
|
|
|
|
# A cap URL with this name will be tied to each region when
|
|
|
|
|
# the sim is first connected to. The URL will be returned to the
|
|
|
|
|
# viewer in the Seed if the viewer requests it by name.
|
|
|
|
|
CAP_NAME = "GetObjectNameExample"
|
|
|
|
|
# Any asgi app should be fine.
|
|
|
|
|
APP = asgiref.wsgi.WsgiToAsgi(app)
|
2021-12-02 02:26:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
addons = [MockProxyCapExampleAddon()]
|