|
| 1 | +import uvicorn |
| 2 | +from fastapi import FastAPI, Depends |
| 3 | +from fastapi.openapi.utils import get_openapi |
| 4 | + |
| 5 | +from pydantic import create_model |
| 6 | +from random_profile import RandomProfile |
| 7 | + |
| 8 | +# random_profile==0.2.3 required |
| 9 | +rp = RandomProfile() |
| 10 | +app = FastAPI() |
| 11 | + |
| 12 | +query_model = create_model("num", num=(int, ...)) |
| 13 | +api_version = "0.2.3" |
| 14 | + |
| 15 | + |
| 16 | +@app.get("/") |
| 17 | +def index(): |
| 18 | + return {"status": "200", |
| 19 | + "message": "Welcome to Random Profile Generator API", |
| 20 | + "version": api_version} |
| 21 | + |
| 22 | + |
| 23 | +@app.get('/api/v1/random_profile/full_profile') |
| 24 | +async def multiple_profile(params: query_model = Depends()): |
| 25 | + """ Get multiple profile with all details |
| 26 | +
|
| 27 | + args: |
| 28 | + num (int): number of profiles to generate |
| 29 | + """ |
| 30 | + params_as_dict = params.dict() |
| 31 | + if params_as_dict['num'] > 100: |
| 32 | + return {"status": "400", |
| 33 | + "message": "Number of profiles should be less than 100", |
| 34 | + "version": api_version} |
| 35 | + |
| 36 | + num = params_as_dict['num'] |
| 37 | + profile = rp.full_profile(num) |
| 38 | + return profile |
| 39 | + |
| 40 | + |
| 41 | +@app.get('/api/v1/random_profile/first_name') |
| 42 | +async def multiple_first_name(params: query_model = Depends()): |
| 43 | + """ Get multiple first names |
| 44 | +
|
| 45 | + args: |
| 46 | + num (int): number of first names to generate |
| 47 | +
|
| 48 | + """ |
| 49 | + params_as_dict = params.dict() |
| 50 | + if params_as_dict['num'] > 100: |
| 51 | + return {"status": "400", |
| 52 | + "message": "Number of profiles should be less than 100", |
| 53 | + "version": api_version} |
| 54 | + |
| 55 | + num = params_as_dict['num'] |
| 56 | + first_names = rp.first_name(num) |
| 57 | + return first_names |
| 58 | + |
| 59 | + |
| 60 | +@app.get('/api/v1/random_profile/last_name') |
| 61 | +async def multiple_last_name(params: query_model = Depends()): |
| 62 | + """ Get multiple last names |
| 63 | +
|
| 64 | + args: |
| 65 | + num (int): number of last names to generate |
| 66 | +
|
| 67 | + """ |
| 68 | + params_as_dict = params.dict() |
| 69 | + if params_as_dict['num'] > 100: |
| 70 | + return {"status": "400", |
| 71 | + "message": "Number of profiles should be less than 100", |
| 72 | + "version": api_version} |
| 73 | + |
| 74 | + num = params_as_dict['num'] |
| 75 | + last_names = rp.last_name(num) |
| 76 | + return last_names |
| 77 | + |
| 78 | + |
| 79 | +@app.get('/api/v1/random_profile/full_name') |
| 80 | +async def multiple_full_name(params: query_model = Depends()): |
| 81 | + """ Get multiple full names |
| 82 | +
|
| 83 | + args: |
| 84 | + num (int): number of full names to generate |
| 85 | +
|
| 86 | + """ |
| 87 | + params_as_dict = params.dict() |
| 88 | + if params_as_dict['num'] > 100: |
| 89 | + return {"status": "400", |
| 90 | + "message": "Number of profiles should be less than 100", |
| 91 | + "version": api_version} |
| 92 | + |
| 93 | + num = params_as_dict['num'] |
| 94 | + |
| 95 | + full_names = rp.full_name(num) |
| 96 | + return full_names |
| 97 | + |
| 98 | + |
| 99 | +def custom_openapi(): |
| 100 | + if app.openapi_schema: |
| 101 | + return app.openapi_schema |
| 102 | + openapi_schema = get_openapi( |
| 103 | + title="Random Profile Generator API", |
| 104 | + version=api_version, |
| 105 | + description="Python Module To Generate Random Profile Data", |
| 106 | + routes=app.routes, |
| 107 | + ) |
| 108 | + openapi_schema["info"]["x-logo"] = { |
| 109 | + "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" |
| 110 | + } |
| 111 | + app.openapi_schema = openapi_schema |
| 112 | + return app.openapi_schema |
| 113 | + |
| 114 | + |
| 115 | +app.openapi = custom_openapi |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + uvicorn.run(app, host="0.0.0.0", port=8000) |
0 commit comments