Skip to content

Commit 8d904d8

Browse files
Merge pull request #51 from codejunction/main
added pydantic response model as DTO for API
2 parents 0373198 + 07cc447 commit 8d904d8

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

Flask/hello-world-app/app.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# Importing flask module in the project is mandatory
22
# An object of Flask class is our WSGI application.
3-
from flask import Flask
4-
3+
from flask import Flask, jsonify
4+
from models import UserInfoRepository
5+
56
# Flask constructor takes the name of
67
# current module (__name__) as argument.
78
app = Flask(__name__)
8-
9+
910
# The route() function of the Flask class is a decorator,
1011
# which tells the application which URL should call
1112
# the associated function.
12-
@app.route('/')
13+
@app.route("/")
1314
# ‘/’ URL is bound with hello_world() function.
1415
def hello_world():
15-
return 'Hello World'
16+
return "Hello World"
1617

17-
@app.route('/hello/<user_name>')
18-
def say_hello(user_name:str = None):
18+
19+
@app.route("/hello/<user_name>")
20+
def say_hello(user_name: str = None):
1921
if user_name:
20-
return f'Hello, {user_name}'
22+
return f"Hello, {user_name}"
2123
else:
2224
return 'Please provide a name "/hello/\{user_name\}"'
23-
25+
26+
27+
@app.route("/api/getUserInfo/<int:limit>")
28+
def get_all_user_info(limit: int):
29+
user_repo: UserInfoRepository = UserInfoRepository()
30+
return jsonify(user_repo.get_all_users(limit))
31+
32+
2433
# main driver function
25-
if __name__ == '__main__':
34+
if __name__ == "__main__":
2635
# run() method of Flask class runs the application
2736
# on the local development server.
2837
app.run(debug=True)

Flask/hello-world-app/models.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pydantic.main import BaseModel
2+
from uuid import UUID, uuid4
3+
from typing import List, Any
4+
import random as r
5+
6+
7+
class ApiReposnseDTO(BaseModel):
8+
def __init__(__pydantic_self__, **data: Any) -> None:
9+
super().__init__(**data)
10+
11+
id: UUID = uuid4()
12+
userName: str # provide a username (mandatory field)
13+
phoneNumber: int = 0 # provide a phone number (optional field)[default 0]
14+
email: str # provide a emal id (mandatory field)
15+
16+
17+
class UserInfoRepository(object):
18+
def get_random_phone_number(self) -> int:
19+
"""Function to generate random phone number
20+
21+
Returns:
22+
int: phone number
23+
"""
24+
phone_number: List[int] = []
25+
phone_number.append(r.randint(6, 9))
26+
for _ in range(1, 10):
27+
phone_number.append(r.randint(0, 9))
28+
return int("".join(map(str, phone_number)))
29+
30+
def get_all_users(self, limit: int = 10) -> List[ApiReposnseDTO]:
31+
response: List[ApiReposnseDTO] = []
32+
for i in range(limit):
33+
response.append(
34+
ApiReposnseDTO(
35+
userName=f"userName_{str(i+1)}",
36+
phoneNumber=self.get_random_phone_number(),
37+
email=f"email_{str(i+1)}@gmail.com",
38+
).dict()
39+
)
40+
return response
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
flask
2+
pydantic

0 commit comments

Comments
 (0)