Spaces:
Sleeping
Sleeping
Update
Browse files- .gitattributes +0 -35
- .gitignore +2 -0
- Dockerfile +5 -3
- LICENSE +21 -0
- README.md +155 -10
- app/__init__.py +0 -0
- app/main.py +12 -0
- app/models/__init__.py +0 -0
- app/routers/__init__.py +0 -0
- app/routers/random.py +68 -0
- app/schemas/__init__.py +0 -0
- app/schemas/user.py +6 -0
- app/utils/__init__.py +0 -0
- app/utils/utils.py +50 -0
- main.py +0 -116
- requirements.txt +2 -2
.gitattributes
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/myenv/
|
| 2 |
+
**/__pycache__/
|
Dockerfile
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
FROM python:3.9
|
| 3 |
|
| 4 |
-
# Set the working directory
|
| 5 |
WORKDIR /code
|
| 6 |
|
| 7 |
# Copy the requirements file
|
|
@@ -13,5 +12,8 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
| 13 |
# Copy the application code
|
| 14 |
COPY . .
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# Run the command to start the development server
|
| 17 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
|
|
|
| 2 |
|
| 3 |
+
# Set the working directory
|
| 4 |
WORKDIR /code
|
| 5 |
|
| 6 |
# Copy the requirements file
|
|
|
|
| 12 |
# Copy the application code
|
| 13 |
COPY . .
|
| 14 |
|
| 15 |
+
# Set the working directory to /code/app
|
| 16 |
+
WORKDIR /code/app
|
| 17 |
+
|
| 18 |
# Run the command to start the development server
|
| 19 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024 Mahir Salahin
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,10 +1,155 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Random API
|
| 2 |
+
===========
|
| 3 |
+
A FastAPI application that provides a variety of randomization endpoints for generating random data, simulating random events, and more.
|
| 4 |
+
|
| 5 |
+
Features
|
| 6 |
+
----------
|
| 7 |
+
|
| 8 |
+
* Generate random floats, integers and characters
|
| 9 |
+
* Simulate coin tosses and random choices
|
| 10 |
+
* Generate random users and passwords
|
| 11 |
+
|
| 12 |
+
Endpoints
|
| 13 |
+
-----------
|
| 14 |
+
|
| 15 |
+
### `/`
|
| 16 |
+
|
| 17 |
+
* **Method:** GET
|
| 18 |
+
* **Description:** Server status
|
| 19 |
+
* **Response:** {"message": "Server is running"}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
### `/random`
|
| 23 |
+
|
| 24 |
+
* **Method:** GET
|
| 25 |
+
* **Description:** Returns a random float between 0 and 1.
|
| 26 |
+
* **Response:** `{"float": <random_float>}`
|
| 27 |
+
* **Errors:**
|
| 28 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random float.
|
| 29 |
+
|
| 30 |
+
### `/random/int`
|
| 31 |
+
|
| 32 |
+
* **Method:** GET
|
| 33 |
+
* **Description:** Returns a random integer.
|
| 34 |
+
* **Response:** `{"int": <random_int>}`
|
| 35 |
+
* **Errors:**
|
| 36 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random integers.
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
### `/random/int/{left}/{right}`
|
| 42 |
+
|
| 43 |
+
* **Method:** GET
|
| 44 |
+
* **Description:** Returns a random integer within the specified range.
|
| 45 |
+
* **Parameters:**
|
| 46 |
+
+ `left`: The minimum value of the range (inclusive).
|
| 47 |
+
+ `right`: The maximum value of the range (inclusive).
|
| 48 |
+
* **Response:** `{"int": <random_int>}`
|
| 49 |
+
* **Errors:**
|
| 50 |
+
+ `400 Bad Request`: If `left` is greater than `right`.
|
| 51 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random integer.
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
### `/random/char`
|
| 55 |
+
|
| 56 |
+
* **Method:** GET
|
| 57 |
+
* **Description:** Returns a random ASCII character.
|
| 58 |
+
* **Response:** `{"char": <random_char>}`
|
| 59 |
+
* **Errors:**
|
| 60 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random character.
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
### `/random/coinflip`
|
| 64 |
+
|
| 65 |
+
* **Method:** GET
|
| 66 |
+
* **Description:** Simulates a coin toss and returns the result.
|
| 67 |
+
* **Response:** `{"result": "Heads" or "Tails"}`
|
| 68 |
+
* **Errors:**
|
| 69 |
+
+ `500 Internal Server Error`: If an error occurs while simulating the coin toss.
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
### `/random/select/{options}`
|
| 73 |
+
|
| 74 |
+
* **Method:** GET
|
| 75 |
+
* **Description:** Selects a random option from the given list.
|
| 76 |
+
* **Parameters:**
|
| 77 |
+
+ `options`: A JSON-encoded list of options.
|
| 78 |
+
* **Response:** `{"selected": <random_option>}`
|
| 79 |
+
* **Errors:**
|
| 80 |
+
+ `400 Bad Request`: If `options` is an empty list or not a valid JSON-encoded list.
|
| 81 |
+
+ `500 Internal Server Error`: If an error occurs while selecting the random option.
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
### `/random/color`
|
| 85 |
+
|
| 86 |
+
* **Method:** GET
|
| 87 |
+
* **Description:** Returns a random hex color.
|
| 88 |
+
* **Response:** `{"color": <random_color>}`
|
| 89 |
+
* **Errors:**
|
| 90 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random color.
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
### `/random/user`
|
| 94 |
+
|
| 95 |
+
* **Method:** GET
|
| 96 |
+
* **Description:** Generates a random user with name, email, and password.
|
| 97 |
+
* **Response:** `{"name": <random_name>, "email": <random_email>, "password": <random_password>}`
|
| 98 |
+
* **Errors:**
|
| 99 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random user data.
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
### `/random/password/{length}`
|
| 103 |
+
|
| 104 |
+
* **Method:** GET
|
| 105 |
+
* **Description:** Generates a random password of the specified length.
|
| 106 |
+
* **Parameters:**
|
| 107 |
+
+ `length`: The length of the password.
|
| 108 |
+
* **Response:** `{"password": <random_password>}`
|
| 109 |
+
* **Errors:**
|
| 110 |
+
+ `400 Bad Request`: If `length` is less than 1.
|
| 111 |
+
+ `500 Internal Server Error`: If an error occurs while generating the random password.
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
Live API
|
| 116 |
+
--------
|
| 117 |
+
|
| 118 |
+
The API is live at:
|
| 119 |
+
* *https://salahin-random-api.hf.space/*
|
| 120 |
+
* *https://random-api-dnl4.onrender.com/*
|
| 121 |
+
|
| 122 |
+
You can test the endpoints using your favorite HTTP client or tool, such as `curl` or `Postman`.
|
| 123 |
+
|
| 124 |
+
Installation (for Development)
|
| 125 |
+
------------
|
| 126 |
+
|
| 127 |
+
To install the required dependencies for development, run:
|
| 128 |
+
```
|
| 129 |
+
pip install -r requirements.txt
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
Running the API (for Development)
|
| 133 |
+
---------------
|
| 134 |
+
|
| 135 |
+
To run the API locally for development, execute:
|
| 136 |
+
```
|
| 137 |
+
uvicorn main:app --reload
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
This will start the API server on `http://localhost:8000`.
|
| 141 |
+
|
| 142 |
+
License
|
| 143 |
+
-------
|
| 144 |
+
|
| 145 |
+
This API is licensed under the MIT License.
|
| 146 |
+
|
| 147 |
+
Contributing
|
| 148 |
+
------------
|
| 149 |
+
|
| 150 |
+
Contributions are welcome! If you'd like to add a new endpoint or feature, please submit a pull request.
|
| 151 |
+
|
| 152 |
+
Acknowledgments
|
| 153 |
+
-----------------
|
| 154 |
+
|
| 155 |
+
Thanks to the FastAPI and Python communities for making this project possible.
|
app/__init__.py
ADDED
|
File without changes
|
app/main.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Response
|
| 2 |
+
from routers.random import router as randoom_router
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
app.include_router(randoom_router)
|
| 6 |
+
|
| 7 |
+
@app.get("/")
|
| 8 |
+
def get_home() -> Response:
|
| 9 |
+
"""
|
| 10 |
+
Return server status
|
| 11 |
+
"""
|
| 12 |
+
return Response("Server is running")
|
app/models/__init__.py
ADDED
|
File without changes
|
app/routers/__init__.py
ADDED
|
File without changes
|
app/routers/random.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
import json
|
| 3 |
+
import string
|
| 4 |
+
from utils.utils import *
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
router = APIRouter(prefix="/random")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@router.get("/")
|
| 11 |
+
def get_root() -> dict:
|
| 12 |
+
"""Return a random float between 0 and 1."""
|
| 13 |
+
return {"float": random_float()}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@router.get("/int")
|
| 17 |
+
def get_random_int() -> dict:
|
| 18 |
+
"""Return a random integer."""
|
| 19 |
+
return {"int": random_int()}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@router.get("/int/{left}/{right}")
|
| 23 |
+
def get_random_in_range(left: int, right: int) -> dict:
|
| 24 |
+
"""Return a random integer within the given range."""
|
| 25 |
+
if left > right:
|
| 26 |
+
raise HTTPException(status_code=400, detail="Invalid range")
|
| 27 |
+
return {"int": random_int(left, right)}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@router.get("/char")
|
| 31 |
+
def generate_random_char() -> dict:
|
| 32 |
+
"""Get a random ASCII character."""
|
| 33 |
+
return {"char": random_choice(string.ascii_letters)}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@router.get("/coinflip")
|
| 37 |
+
def toss_coin() -> dict:
|
| 38 |
+
"""Simulate a coin toss and return the result."""
|
| 39 |
+
return {"result": random_choice(["Heads", "Tails"])}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@router.get("/select/{options}")
|
| 43 |
+
def get_random_option(options: str) -> dict:
|
| 44 |
+
"""Select a random option from the given list."""
|
| 45 |
+
options_list = json.loads(options)
|
| 46 |
+
if not options_list:
|
| 47 |
+
raise HTTPException(status_code=400, detail="Invalid options")
|
| 48 |
+
return {"selected": random_choice(options_list)}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@router.get("/color")
|
| 52 |
+
def get_color() -> dict:
|
| 53 |
+
"""Select a random hex color."""
|
| 54 |
+
hex_color = "#" + \
|
| 55 |
+
"".join([random_choice('0123456789ABCDEF') for _ in range(6)])
|
| 56 |
+
return {"color": hex_color}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@router.get("/user")
|
| 60 |
+
def get_user() -> User:
|
| 61 |
+
"""Generate a random user with name, email, and password."""
|
| 62 |
+
return random_user()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@router.get("/password/{length}")
|
| 66 |
+
def get_password(length: int) -> dict:
|
| 67 |
+
"""Generate a random password of a specified length."""
|
| 68 |
+
return {"password": random_password(length=length)}
|
app/schemas/__init__.py
ADDED
|
File without changes
|
app/schemas/user.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class User(BaseModel):
|
| 4 |
+
name: str = None
|
| 5 |
+
email: str = None
|
| 6 |
+
password: str = None
|
app/utils/__init__.py
ADDED
|
File without changes
|
app/utils/utils.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from random import randint, random, choice
|
| 2 |
+
from schemas.user import User
|
| 3 |
+
from faker import Faker
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def random_int(l: int = 0, r: int = 10**3) -> int:
|
| 7 |
+
"""Generate a random integer within the given range."""
|
| 8 |
+
return randint(l, r)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def random_float() -> float:
|
| 12 |
+
"""Generate a random float number."""
|
| 13 |
+
return random()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def random_choice(options: list):
|
| 17 |
+
"""Generate a random choice from a list of options."""
|
| 18 |
+
return choice(options)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def random_password(length=8) -> str:
|
| 22 |
+
"""Generate a random password of given length"""
|
| 23 |
+
return Faker().password(length=length)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def random_email(user_name: str = Faker().name()) -> str:
|
| 27 |
+
"""Generate a random email address"""
|
| 28 |
+
first_name, last_name = user_name.split(
|
| 29 |
+
)[0].lower(), user_name.split()[-1].lower()
|
| 30 |
+
|
| 31 |
+
random_nums = ''.join(str(random_int(r=9))
|
| 32 |
+
for _ in range(random_int(r=4)))
|
| 33 |
+
email_domain = Faker().free_email_domain()
|
| 34 |
+
|
| 35 |
+
return choice(
|
| 36 |
+
[f"{first_name}.{last_name}@{email_domain}",
|
| 37 |
+
f"{last_name}.{first_name}@{email_domain}",
|
| 38 |
+
f"{first_name}.{last_name}{random_nums}@{email_domain}",
|
| 39 |
+
f"{random_nums}{first_name}@{email_domain}",
|
| 40 |
+
f"{first_name}{random_nums}@{email_domain}",
|
| 41 |
+
f"{last_name}{random_nums}@{email_domain}",
|
| 42 |
+
])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def random_user() -> User:
|
| 46 |
+
user = User()
|
| 47 |
+
user.name = Faker().name()
|
| 48 |
+
user.password = random_password()
|
| 49 |
+
user.email = random_email(user_name=user.name)
|
| 50 |
+
return user
|
main.py
DELETED
|
@@ -1,116 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
import random
|
| 3 |
-
import json
|
| 4 |
-
import secrets
|
| 5 |
-
import string
|
| 6 |
-
import faker
|
| 7 |
-
|
| 8 |
-
app = FastAPI()
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def generate_random_int(left: int = 0, right: int = 10**100) -> int:
|
| 12 |
-
"""Generate a random integer within the given range."""
|
| 13 |
-
return random.randint(left, right)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
@app.get("/")
|
| 17 |
-
def get_home() -> dict:
|
| 18 |
-
"""Return the API home page with available routes."""
|
| 19 |
-
routes = {
|
| 20 |
-
"/": "Description of the api",
|
| 21 |
-
"/random": "Get a random float between 0 and 1.",
|
| 22 |
-
"/random/int": "Get a random integer",
|
| 23 |
-
"/random/range/{left}/{right}": "Get a random integer within the specified range.",
|
| 24 |
-
"/random/char": "Get a random ASCII character.",
|
| 25 |
-
"/random/coinflip": "Simulate a coin toss and get the result.",
|
| 26 |
-
"/random/choice/{options}": "Select a random option from the given list.",
|
| 27 |
-
"/random/color": "Select a random hex color.",
|
| 28 |
-
"/random/user": "Generate a random user with name, email, and password.",
|
| 29 |
-
"/random/password/{length}": "Generate a random password of a specified length.",
|
| 30 |
-
}
|
| 31 |
-
meta = {
|
| 32 |
-
"api_version": "1.0",
|
| 33 |
-
"docs": "https://github.com/MahirSalahin/random_api"
|
| 34 |
-
}
|
| 35 |
-
return {"routes": routes, "meta": meta}
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
@app.get("/random")
|
| 39 |
-
def get_root() -> dict:
|
| 40 |
-
"""Return a random float between 0 and 1."""
|
| 41 |
-
return {"float": random.random()}
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@app.get("/random/int")
|
| 45 |
-
def get_random_int() -> dict:
|
| 46 |
-
"""Return a random integer."""
|
| 47 |
-
return {"int": generate_random_int()}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@app.get("/random/range/{left}/{right}")
|
| 51 |
-
def get_random_in_range(left: int, right: int) -> dict:
|
| 52 |
-
"""Return a random integer within the given range."""
|
| 53 |
-
if left > right:
|
| 54 |
-
raise HTTPException(status_code=400, detail="Invalid range")
|
| 55 |
-
return {"int": generate_random_int(left, right)}
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
@app.get("/random/char")
|
| 59 |
-
def generate_random_char() -> dict:
|
| 60 |
-
"""Get a random ASCII character."""
|
| 61 |
-
return {"char": random.choice(string.ascii_letters)}
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
@app.get("/random/coinflip")
|
| 65 |
-
def toss_coin() -> dict:
|
| 66 |
-
"""Simulate a coin toss and return the result."""
|
| 67 |
-
return {"result": random.choice(["Heads", "Tails"])}
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
@app.get("/random/choice/{options}")
|
| 71 |
-
def select_randomly(options: str) -> dict:
|
| 72 |
-
"""Select a random option from the given list."""
|
| 73 |
-
options_list = json.loads(options)
|
| 74 |
-
if not options_list:
|
| 75 |
-
raise HTTPException(status_code=400, detail="Invalid options")
|
| 76 |
-
return {"selected": random.choice(options_list)}
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
@app.get("/random/color")
|
| 80 |
-
def get_color() -> dict:
|
| 81 |
-
"""Select a random hex color."""
|
| 82 |
-
hex_colors = [
|
| 83 |
-
"#" + "".join([random.choice('0123456789ABCDEF') for _ in range(6)])]
|
| 84 |
-
return {"color": hex_colors[0]}
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
@app.get("/random/user")
|
| 88 |
-
def generate_user() -> dict:
|
| 89 |
-
"""Generate a random user with name, email, and password."""
|
| 90 |
-
fake = faker.Faker()
|
| 91 |
-
name = fake.name()
|
| 92 |
-
first_name, last_name = name.split()[0].lower(), name.split()[-1].lower()
|
| 93 |
-
nums = ''
|
| 94 |
-
for _ in range(random.randint(0, 4)):
|
| 95 |
-
nums += str(random.randint(0, 9))
|
| 96 |
-
email = random.choice(
|
| 97 |
-
[f"{first_name}.{last_name}@{fake.free_email_domain()}",
|
| 98 |
-
f"{first_name}.{last_name}{nums}@{fake.free_email_domain()}",
|
| 99 |
-
f"{nums}{first_name}@{fake.free_email_domain()}",
|
| 100 |
-
f"{first_name}{nums}@{fake.free_email_domain()}",
|
| 101 |
-
f"{last_name}{nums}@{fake.free_email_domain()}",
|
| 102 |
-
])
|
| 103 |
-
user = {
|
| 104 |
-
"name": name,
|
| 105 |
-
"email": email,
|
| 106 |
-
"password": fake.password()
|
| 107 |
-
}
|
| 108 |
-
return user
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
@app.get("/random/password/{length}")
|
| 112 |
-
def generate_password(length: int) -> dict:
|
| 113 |
-
"""Generate a random password of a specified length."""
|
| 114 |
-
alphabet = string.ascii_letters + string.digits + string.punctuation
|
| 115 |
-
password = "".join(secrets.choice(alphabet) for _ in range(length))
|
| 116 |
-
return {"password": password}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
faker
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
faker
|