Spaces:
Sleeping

Salahin commited on
Commit
fde7aa9
·
0 Parent(s):

first commit

Browse files
Files changed (3) hide show
  1. README.md +40 -0
  2. main.py +40 -0
  3. requirements.txt +2 -0
README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Random API
2
+ ===========
3
+
4
+ This API provides a set of endpoints for generating random data.
5
+
6
+ Endpoints
7
+ ---------
8
+
9
+ ### `/`
10
+ Get a random float between 0 and 1.
11
+
12
+ * Method: `GET`
13
+ * Response: `{"random": float}`
14
+
15
+ ### `/random_in_range/{left}/{right}`
16
+ Get a random integer within the specified range.
17
+
18
+ * Method: `GET`
19
+ * Parameters:
20
+ + `left`: int (default: 0)
21
+ + `right`: int (default: math.inf)
22
+ * Response: `{"random": int}`
23
+ * Errors:
24
+ + 400: Invalid range (left > right)
25
+
26
+ ### `/toss_a_coin`
27
+ Simulate a coin toss and get the result.
28
+
29
+ * Method: `GET`
30
+ * Response: `{"result": "Heads" | "Tails"}`
31
+
32
+ ### `/select/{options}`
33
+ Select a random option from the given list.
34
+
35
+ * Method: `GET`
36
+ * Parameters:
37
+ + `options`: JSON-encoded list of options
38
+ * Response: `{"selected": string}`
39
+ * Errors:
40
+ + 400: Invalid options (empty list)
main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import random
3
+ import json
4
+ import math
5
+
6
+ app = FastAPI()
7
+
8
+
9
+ def generate_random_int(left: int = 0, right: int = math.inf) -> int:
10
+ """Generate a random integer within the given range."""
11
+ return random.randint(left, right)
12
+
13
+
14
+ @app.get("/")
15
+ def get_root() -> dict:
16
+ """Return a random float between 0 and 1."""
17
+ return {"random": random.random()}
18
+
19
+
20
+ @app.get("/random_in_range/{left}/{right}")
21
+ def get_random_in_range(left: int, right: int) -> dict:
22
+ """Return a random integer within the given range."""
23
+ if left > right:
24
+ raise HTTPException(status_code=400, detail="Invalid range")
25
+ return {"random": generate_random_int(left, right)}
26
+
27
+
28
+ @app.get("/toss_a_coin")
29
+ def toss_coin() -> dict:
30
+ """Simulate a coin toss and return the result."""
31
+ return {"result": random.choice(["Heads", "Tails"])}
32
+
33
+
34
+ @app.get("/select/{options}")
35
+ def select_randomly(options) -> dict:
36
+ """Select a random option from the given list."""
37
+ options_list = json.loads(options)
38
+ if not options_list:
39
+ raise HTTPException(status_code=400, detail="Invalid options")
40
+ return {"selected": random.choice(options_list)}
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn