|
| 1 | +import sys |
| 2 | +import uvicorn |
| 3 | +from fastapi import FastAPI, Depends |
| 4 | +from fastapi.openapi.utils import get_openapi |
| 5 | +from pydantic import create_model |
| 6 | + |
| 7 | +sys.path.append('.') |
| 8 | +from random_profile.main import RandomProfile, VERSION |
| 9 | + |
| 10 | +# random_profile==0.2.3 required |
| 11 | +rp = RandomProfile() |
| 12 | +app = FastAPI() |
| 13 | + |
| 14 | +query_limit = 1000 |
| 15 | +query_model = create_model("num", num=(int, ...)) |
| 16 | + |
| 17 | +metadata = { |
| 18 | + "status": "200", |
| 19 | + "message": "Success", |
| 20 | + "version": VERSION, |
| 21 | + "author": "Deepak Raj", |
| 22 | + "author_email": "[email protected]", |
| 23 | + "github": "https://github.com/codeperfectplus"} |
| 24 | + |
| 25 | +overloaded_error = {"status": "429", |
| 26 | + "Error": "Too Many Requests", |
| 27 | + "message": "Number of profiles should be less than {}".format(query_limit)} |
| 28 | + |
| 29 | + |
| 30 | +@app.get("/") |
| 31 | +def index(): |
| 32 | + return metadata |
| 33 | + |
| 34 | + |
| 35 | +@app.get('/api/v1/random_profile/full_profile') |
| 36 | +async def get_full_profile(params: query_model = Depends()): |
| 37 | + """ Get multiple profile with all details |
| 38 | +
|
| 39 | + args: |
| 40 | + num (int): number of profiles to generate |
| 41 | + """ |
| 42 | + params_as_dict = params.dict() |
| 43 | + if params_as_dict['num'] > query_limit: |
| 44 | + return overloaded_error |
| 45 | + |
| 46 | + num = params_as_dict['num'] |
| 47 | + profile = rp.full_profile(num) |
| 48 | + metadata['data'] = profile |
| 49 | + return metadata |
| 50 | + |
| 51 | + |
| 52 | +@app.get('/api/v1/random_profile/first_name') |
| 53 | +async def get_first_name(params: query_model = Depends()): |
| 54 | + """ Get multiple first names |
| 55 | +
|
| 56 | + args: |
| 57 | + num (int): number of first names to generate |
| 58 | +
|
| 59 | + """ |
| 60 | + params_as_dict = params.dict() |
| 61 | + if params_as_dict['num'] > query_limit: |
| 62 | + return overloaded_error |
| 63 | + |
| 64 | + num = params_as_dict['num'] |
| 65 | + first_names = rp.first_name(num) |
| 66 | + metadata['data'] = first_names |
| 67 | + return metadata |
| 68 | + |
| 69 | + |
| 70 | +@app.get('/api/v1/random_profile/last_name') |
| 71 | +async def get_last_name(params: query_model = Depends()): |
| 72 | + """ Get multiple last names |
| 73 | +
|
| 74 | + args: |
| 75 | + num (int): number of last names to generate |
| 76 | +
|
| 77 | + """ |
| 78 | + params_as_dict = params.dict() |
| 79 | + if params_as_dict['num'] > query_limit: |
| 80 | + return overloaded_error |
| 81 | + |
| 82 | + num = params_as_dict['num'] |
| 83 | + last_names = rp.last_name(num) |
| 84 | + metadata['data'] = last_names |
| 85 | + return metadata |
| 86 | + |
| 87 | + |
| 88 | +@app.get('/api/v1/random_profile/full_name') |
| 89 | +async def get_full_name(params: query_model = Depends()): |
| 90 | + """ Get multiple full names |
| 91 | +
|
| 92 | + args: |
| 93 | + num (int): number of full names to generate |
| 94 | +
|
| 95 | + """ |
| 96 | + params_as_dict = params.dict() |
| 97 | + if params_as_dict['num'] > query_limit: |
| 98 | + return overloaded_error |
| 99 | + |
| 100 | + num = params_as_dict['num'] |
| 101 | + full_names = rp.full_name(num) |
| 102 | + metadata['data'] = full_names |
| 103 | + return metadata |
| 104 | + |
| 105 | + |
| 106 | +@app.get('/api/v1/random_profile/ip_address') |
| 107 | +async def get_ip_address(params: query_model = Depends()): |
| 108 | + """ Get multiple ip addresses |
| 109 | +
|
| 110 | + args: |
| 111 | + num (int): number of ip addresses to generate |
| 112 | +
|
| 113 | + """ |
| 114 | + params_as_dict = params.dict() |
| 115 | + if params_as_dict['num'] > query_limit: |
| 116 | + return overloaded_error |
| 117 | + |
| 118 | + num = params_as_dict['num'] |
| 119 | + ip_addresses = rp.ipaddress(num) |
| 120 | + metadata['data'] = ip_addresses |
| 121 | + return metadata |
| 122 | + |
| 123 | + |
| 124 | +@app.get("/api/v1/random_profile/job_title") |
| 125 | +async def get_job_title(params: query_model = Depends()): |
| 126 | + """ Get multiple job titles |
| 127 | +
|
| 128 | + args: |
| 129 | + num (int): number of job titles to generate |
| 130 | +
|
| 131 | + """ |
| 132 | + params_as_dict = params.dict() |
| 133 | + if params_as_dict['num'] > query_limit: |
| 134 | + return overloaded_error |
| 135 | + |
| 136 | + num = params_as_dict['num'] |
| 137 | + job_titles = rp.job_title(num) |
| 138 | + metadata['data'] = job_titles |
| 139 | + return metadata |
| 140 | + |
| 141 | + |
| 142 | +@app.get("/api/v1/random_profile/address") |
| 143 | +async def get_address(params: query_model = Depends()): |
| 144 | + """ Get multiple address """ |
| 145 | + params_as_dict = params.dict() |
| 146 | + if params_as_dict['num'] > query_limit: |
| 147 | + return overloaded_error |
| 148 | + num = params_as_dict['num'] |
| 149 | + address = rp.generate_address(num) |
| 150 | + metadata['data'] = address |
| 151 | + return metadata |
| 152 | + |
| 153 | + |
| 154 | +def custom_openapi(): |
| 155 | + if app.openapi_schema: |
| 156 | + return app.openapi_schema |
| 157 | + openapi_schema = get_openapi( |
| 158 | + title="Random Profile Generator API", |
| 159 | + version=VERSION, |
| 160 | + description="Python Module To Generate Random Profile Data", |
| 161 | + routes=app.routes, |
| 162 | + ) |
| 163 | + openapi_schema["info"]["x-logo"] = { |
| 164 | + "url": "https://raw.githubusercontent.com/DrakeEntity/project-Image/master/9b2ca712-347a-4987-bac7-a4c3d106ed24_200x200.png" |
| 165 | + } |
| 166 | + app.openapi_schema = openapi_schema |
| 167 | + return app.openapi_schema |
| 168 | + |
| 169 | + |
| 170 | +app.openapi = custom_openapi |
| 171 | + |
| 172 | + |
| 173 | +def start_server(port=8000): |
| 174 | + uvicorn.run(app, host="0.0.0.0", port=port) |
0 commit comments