File tree Expand file tree Collapse file tree 6 files changed +22
-10
lines changed
sample_fastapi/app/resources Expand file tree Collapse file tree 6 files changed +22
-10
lines changed Original file line number Diff line number Diff line change 11from pydantic import BaseModel
22
33
4- class ConversioHexResponse (BaseModel ):
4+ class ConversionHexResponse (BaseModel ):
5+ """Hexadecimal value response"""
56 value : str
67
78
8- class ConversioDecResponse (BaseModel ):
9+ class ConversionDecResponse (BaseModel ):
10+ """Integer value response"""
911 value : int
Original file line number Diff line number Diff line change 11from fastapi import APIRouter
22
3- from .views import calc_dec2hex , calc_hex2dec
3+ from .views import conv_dec2hex , conv_hex2dec
44
55
66def init_routes ():
77 router = APIRouter (prefix = "/calculator" , tags = ["calculator" ])
88
9- router .add_api_route ("/convert/dec2hex" , calc_dec2hex , methods = {"POST" })
10- router .add_api_route ("/convert/hex2dec" , calc_hex2dec , methods = {"POST" })
9+ router .add_api_route ("/convert/dec2hex" , conv_dec2hex , methods = {"POST" })
10+ router .add_api_route ("/convert/hex2dec" , conv_hex2dec , methods = {"POST" })
1111
1212 return router
Original file line number Diff line number Diff line change 11class ConverterService :
2+ """Data conversion service"""
23 def __init__ (self ):
34 pass
45
56 def dec2hex (self , value : int ) -> str :
7+ """Convert integer to hexadecimal string with '0x' prefix"""
68 return hex (value )
79
810 def hex2dec (self , value : str ) -> int :
11+ """Convert hexadecimal string (with or without prefix) into integer.
12+
13+ Raises `ValueError` for incorrect input"""
914 return int (value , 16 )
Original file line number Diff line number Diff line change 11from fastapi import Depends , HTTPException , status
22
3- from .models import ConversioDecResponse , ConversioHexResponse
3+ from .models import ConversionDecResponse , ConversionHexResponse
44from .services import ConverterService
55
66
7- async def calc_dec2hex (value : int , converter : ConverterService = Depends (ConverterService )) -> ConversioHexResponse :
8- return ConversioHexResponse (
7+ async def conv_dec2hex (value : int , converter : ConverterService = Depends (ConverterService )) -> ConversionHexResponse :
8+ """Convert integer input into hexadecimal string"""
9+ return ConversionHexResponse (
910 value = converter .dec2hex (value ),
1011 )
1112
1213
13- async def calc_hex2dec (value : str , converter : ConverterService = Depends (ConverterService )) -> ConversioDecResponse :
14+ async def conv_hex2dec (value : str , converter : ConverterService = Depends (ConverterService )) -> ConversionDecResponse :
15+ """Convert hexadecimal string input into integer"""
1416 try :
15- return ConversioDecResponse (
17+ return ConversionDecResponse (
1618 value = converter .hex2dec (value ),
1719 )
1820 except ValueError :
Original file line number Diff line number Diff line change 22
33
44class HelloMessage (BaseModel ):
5+ """Message response to hello request"""
56 message : str
Original file line number Diff line number Diff line change 44
55
66async def respond_hello () -> HelloMessage :
7+ """Respond with "Hello, World!" message in a JSON"""
78 return HelloMessage (message = "Hello, World!" )
89
910
1011async def respond_name_greet (name : str = Query ()) -> HelloMessage :
12+ """Respond with "Hello, <name>!" message in a JSON"""
1113 return HelloMessage (message = f"Hello, { name } !" )
You can’t perform that action at this time.
0 commit comments