How to interact with a community from an api/app like fastapi or flask
adagrad opened this issue · 2 comments
It is unclear to me how I am supposed to interact (send messages, await response) with the community object from a different application like a fastAPI univicon app.
async def start_communities() -> None:
builder = ConfigBuilder().clear_keys().clear_overlays()
# builder stuff
await IPv8(builder.finalize(), extra_communities={'Lala': Lala}).start()
await run_forever()
@asynccontextmanager
async def lifespan(app: FastAPI):
print("starting communitys ... ")
asyncio.create_task(start_communities())
yield
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root():
return # how can is send a message to my Lala community and return the response
if __name__ == '__main__':
check_upgrade_yfinance()
uvicorn.run(app, **CONFIG["api"])
Not the comment in the async def root()
function.
PS I think this might also be worth to be added to the documentation
Strictly speaking, IPv8 is only a networking library (by design) and you can hook into it however you like. We do not enforce or hook in any higher-level API like Flask or FastAPI. However, if you choose to run the stand-alone ipv8_service.py
, we provide a (Swagger) REST API to interact with Community instances.
If you want to adopt this approach, you'll need to spawn a RESTManager
(extended to provide the desired interface for your Community
) for every IPv8
instance you create:
py-ipv8/scripts/ipv8_plugin.py
Lines 44 to 60 in 24ae8c9
By default, this opens a REST API on port 8085
and you can view the Swagger API at http://localhost:8085/docs
.
You can find further information on the API key here and on the optional SSLContext
here.
Thanks, that got me started.