Skip to content

Instantly share code, notes, and snippets.

@danielballan
Created May 12, 2023 19:41
Show Gist options
  • Save danielballan/476359c7743251582a8302f4794bb8ab to your computer and use it in GitHub Desktop.
Save danielballan/476359c7743251582a8302f4794bb8ab to your computer and use it in GitHub Desktop.

Example of how to extend Tiled with a custom route

  1. Clone this gist and enter the directory.

  2. Install Tiled and and, optionally, a HTTP client to test with.

    pip install tiled[all] httpie
    
  3. Start the server:

    python plugin_route_example.py
    
  4. Test the custom route:

    http :8000/api/v1/example/
    

It should look like this:

$ http :8000/api/v1/example/
HTTP/1.1 200 OK
content-length: 17
content-type: application/json
date: Fri, 12 May 2023 19:39:02 GMT
server: uvicorn
server-timing: app;dur=3.7
set-cookie: tiled_csrf=lg1oMoZ52b5o3yqXL9JunT4RwNxhIy5dvHYEuQSQGV8; HttpOnly; Path=/; SameSite=lax

{
    "hello": "world"
}

from fastapi import APIRouter
from tiled.adapters.array import ArrayAdapter
from tiled.server.app import build_app
from tiled.server.dependencies import SecureEntry
router = APIRouter()
@router.get("/example/{path:path}")
def example(
entry=SecureEntry(scopes=["read:data", "read:metadata"]),
):
return entry.example()
class MyAdapter(ArrayAdapter):
include_routers = [router]
def example(self):
return {"hello": "world"}
def main():
import uvicorn
app = build_app(MyAdapter.from_array([1, 2, 3]), {"allow_anonymous_access": True})
uvicorn.run(app)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment