Skip to content

Commit 02e5bfd

Browse files
committed
Add HTTP server example
1 parent 8014f48 commit 02e5bfd

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

examples/http/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Compute API Example
2+
3+
## Running the API
4+
5+
```bash
6+
uvicorn main:app --reload
7+
```
8+
9+
## API Endpoints
10+
11+
### GET Endpoint
12+
13+
- URL: `/compute`
14+
- Query Parameters:
15+
- `input` (required): The expression to compute
16+
- `timeout` (optional, default: 10.0): Timeout for computation in seconds
17+
18+
Examples:
19+
20+
```bash
21+
# Basic usage (URL-encoded)
22+
curl "http://localhost:8000/compute?input=21%2B21"
23+
24+
# Alternative URL-encoded formats
25+
curl "http://localhost:8000/compute?input=21%20%2B%2021"
26+
curl "http://localhost:8000/compute?input=21+21" # Some clients handle this
27+
28+
# With custom timeout (URL-encoded)
29+
curl "http://localhost:8000/compute?input=21%2B21&timeout=5.0"
30+
```
31+
32+
```bash
33+
# With function call
34+
curl "http://localhost:8000/compute?input=say_hello(\"Monic\")&timeout=5.0"
35+
```
36+
37+
### POST Endpoint
38+
39+
- URL: `/compute`
40+
- Request Body (JSON):
41+
42+
```json
43+
{
44+
"input": "string", // Required: Expression to compute
45+
"timeout": float // Optional: Timeout for computation (default: 10.0)
46+
}
47+
```
48+
49+
Example:
50+
51+
```bash
52+
curl -X POST http://localhost:8000/compute \
53+
-H "Content-Type: application/json" \
54+
-d '{"input": "21 + 21", "timeout": 5.0}'
55+
```
56+
57+
```bash
58+
curl -X POST http://localhost:8000/compute \
59+
-H "Content-Type: application/json" \
60+
-d '{"input": "say_hello(\"Monic\")", "timeout": 5.0}'
61+
```
62+
63+
### Response
64+
65+
Both endpoints return a JSON response:
66+
67+
```json
68+
{
69+
"result": 42
70+
}
71+
```
72+
73+
## Notes
74+
75+
- The `input` field is required for both GET and POST requests
76+
- You can optionally specify a `timeout` to limit computation time
77+
- The API uses the Monic Framework's expression parser and interpreter
78+
- For GET requests, use URL encoding for special characters like `+`

examples/http/main.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# Monic Framework
3+
#
4+
# Copyright (c) 2024 Cognica, Inc.
5+
#
6+
7+
# pylint: disable=redefined-builtin
8+
9+
from fastapi import FastAPI, Query
10+
from pydantic import BaseModel
11+
12+
from monic.expressions import (
13+
ExpressionsContext,
14+
ExpressionsParser,
15+
ExpressionsInterpreter,
16+
register,
17+
)
18+
19+
20+
app = FastAPI(title="Compute API")
21+
22+
23+
class ComputeInput(BaseModel):
24+
input: str
25+
timeout: float = 10.0
26+
27+
28+
@register
29+
def say_hello(name: str) -> str:
30+
return f"Hello, {name}!"
31+
32+
33+
@app.get("/compute")
34+
async def compute_get(
35+
input: str = Query(..., description="Expression to compute"),
36+
timeout: float = Query(10.0, description="Timeout for computation"),
37+
):
38+
"""
39+
Compute endpoint supporting GET method
40+
Returns the computed result of the input expression
41+
"""
42+
parser = ExpressionsParser()
43+
context = ExpressionsContext(timeout=timeout)
44+
interpreter = ExpressionsInterpreter(context=context)
45+
result = interpreter.execute(parser.parse(input))
46+
47+
return {"result": result}
48+
49+
50+
@app.post("/compute")
51+
async def compute_post(compute_input: ComputeInput):
52+
"""
53+
Compute endpoint supporting POST method
54+
Returns the computed result of the input expression
55+
"""
56+
parser = ExpressionsParser()
57+
context = ExpressionsContext(timeout=compute_input.timeout)
58+
interpreter = ExpressionsInterpreter(context=context)
59+
result = interpreter.execute(parser.parse(compute_input.input))
60+
61+
return {"result": result}
62+
63+
64+
if __name__ == "__main__":
65+
import uvicorn
66+
67+
uvicorn.run(app, host="0.0.0.0", port=8000)

0 commit comments

Comments
 (0)