|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import functools |
| 19 | +import importlib |
| 20 | + |
| 21 | +import pydantic |
| 22 | +from fastapi import APIRouter |
| 23 | + |
| 24 | +from burr.core import Application, ApplicationBuilder, Result, default, expr |
| 25 | +from burr.tracking import LocalTrackingClient |
| 26 | + |
| 27 | +"""This file represents a simple counter API backed with Burr. |
| 28 | +We manage an application, write to it with post endpoints, and read with |
| 29 | +get/ endpoints. |
| 30 | +
|
| 31 | +This demonstrates how you can build interactive web applications with Burr! |
| 32 | +""" |
| 33 | + |
| 34 | +counter_application = importlib.import_module( |
| 35 | + "burr.examples.hello-world-counter.application" |
| 36 | +) # noqa: F401 |
| 37 | + |
| 38 | +router = APIRouter() |
| 39 | + |
| 40 | +COUNTER_LIMIT = 1000 |
| 41 | + |
| 42 | + |
| 43 | +class CounterState(pydantic.BaseModel): |
| 44 | + """Pydantic model for the counter state.""" |
| 45 | + |
| 46 | + counter: int |
| 47 | + app_id: str |
| 48 | + |
| 49 | + |
| 50 | +@functools.lru_cache(maxsize=128) |
| 51 | +def _get_application(project_id: str, app_id: str) -> Application: |
| 52 | + """Quick tool to get the application -- caches it""" |
| 53 | + tracker = LocalTrackingClient(project=project_id, storage_dir="~/.burr") |
| 54 | + return ( |
| 55 | + ApplicationBuilder() |
| 56 | + .with_actions( |
| 57 | + counter=counter_application.counter, |
| 58 | + result=Result("counter"), |
| 59 | + ) |
| 60 | + .with_transitions( |
| 61 | + ("counter", "counter", expr(f"counter < {COUNTER_LIMIT}")), |
| 62 | + ("counter", "result", default), |
| 63 | + ) |
| 64 | + .initialize_from( |
| 65 | + tracker, |
| 66 | + resume_at_next_action=True, |
| 67 | + default_state={"counter": 0}, |
| 68 | + default_entrypoint="counter", |
| 69 | + ) |
| 70 | + .with_tracker(tracker) |
| 71 | + .with_identifiers(app_id=app_id) |
| 72 | + .build() |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@router.post("/count/{project_id}/{app_id}", response_model=CounterState) |
| 77 | +def count(project_id: str, app_id: str) -> CounterState: |
| 78 | + """Increment the counter by one step and return the new state. |
| 79 | +
|
| 80 | + :param project_id: Project ID to run |
| 81 | + :param app_id: Application ID to run |
| 82 | + :return: The current counter state |
| 83 | + """ |
| 84 | + burr_app = _get_application(project_id, app_id) |
| 85 | + burr_app.step() |
| 86 | + return CounterState( |
| 87 | + counter=burr_app.state["counter"], |
| 88 | + app_id=app_id, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@router.get("/state/{project_id}/{app_id}", response_model=CounterState) |
| 93 | +def get_counter_state(project_id: str, app_id: str) -> CounterState: |
| 94 | + """Get the current counter state without incrementing. |
| 95 | +
|
| 96 | + :param project_id: Project ID |
| 97 | + :param app_id: App ID |
| 98 | + :return: The current counter state |
| 99 | + """ |
| 100 | + burr_app = _get_application(project_id, app_id) |
| 101 | + return CounterState( |
| 102 | + counter=burr_app.state["counter"], |
| 103 | + app_id=app_id, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@router.post("/create/{project_id}/{app_id}", response_model=str) |
| 108 | +async def create_new_application(project_id: str, app_id: str) -> str: |
| 109 | + """Endpoint to create a new application -- used by the FE when |
| 110 | + the user types in a new App ID |
| 111 | +
|
| 112 | + :param project_id: Project ID |
| 113 | + :param app_id: App ID |
| 114 | + :return: The app ID |
| 115 | + """ |
| 116 | + _get_application(app_id=app_id, project_id=project_id) |
| 117 | + return app_id |
0 commit comments