@@ -16,7 +16,7 @@ class ClickStatsData(BaseModel):
1616 original_url : str
1717
1818class UrlRequest (BaseModel ):
19- url : str
19+ url : str
2020
2121class ClickStats (BaseModel ):
2222 short_code : str
@@ -32,63 +32,35 @@ class ClickStats(BaseModel):
3232click_stats : Dict [str , ClickStatsData ] = {}
3333
3434def generate_short_code (length : int = 6 ) -> str :
35- return '' .join (random .choices (string .ascii_letters + string .digits , k = length ))
35+ return '' .join (random .choices (string .ascii_letters + string .digits , k = length ))
3636
3737@app .post ("/shorten" )
3838def shorten_url (request : UrlRequest ):
39-
40- if request .url in url_store .values ():
41- raise HTTPException (status_code = 400 , detail = "URL was already shortened previously" )
42-
43- short_code = generate_short_code ()
44- while short_code in url_store :
45- short_code = generate_short_code ()
46-
47- url_store [short_code ] = request .url
48-
49- # Initialize click stats for the new URL
50- click_stats [short_code ] = ClickStatsData (
51- count = 0 ,
52- original_url = request .url
53- )
54-
55- return {short_code }
39+ # TODO: implement this function
40+ # 1. Generate short code for long url
41+ # 2. Store the mapping of short code to long url
42+ # 3. Optional: store click_stats using the class ClickStatsData for the short_code with initial values
43+ short_code = ""
44+ return {short_code }
5645
5746@app .get ("/get-long-url/{short_code}" )
5847def get_long_url (short_code : str ):
59- if short_code in url_store :
60- # Track clicks
61- if short_code not in click_stats :
62- click_stats [short_code ] = ClickStatsData (
63- count = 0 ,
64- original_url = url_store [short_code ]
65- )
66-
67- click_stats [short_code ].count += 1
68- return {url_store [short_code ]}
69-
70- raise HTTPException (status_code = 404 , detail = "Short URL not found" )
71-
48+ # TODO: implement this function
49+ # 1. Fetch the long URL from the url_store using the short_code
50+ # 2. Return HTTP 404 (not found) if the short_code is not found
51+ # 3. (Optional) Update the click statistics in click_stats for the short_code
52+ long_url = ""
53+ return {long_url }
7254
7355@app .get ("/get-qr-code/{url}" )
7456def get_qr_code (url : str ):
75- # URL decode the parameter to handle special characters
76- decoded_url = url
77-
78- qr = qrcode .QRCode (
79- version = 1 ,
80- error_correction = qrcode .constants .ERROR_CORRECT_L ,
81- box_size = 10 ,
82- border = 0 ,
83- )
84- qr .add_data (decoded_url )
85- qr .make (fit = True )
86- img = qr .make_image (fill_color = "black" , back_color = "white" )
87- buf = io .BytesIO ()
88- img .save (buf , format = "PNG" )
89- buf .seek (0 )
90- encoded_string = base64 .b64encode (buf .read ()).decode ("utf-8" )
91- return {"image_base64" : f"data:image/png;base64,{ encoded_string } " }
57+ # TODO: implement this function
58+ # 1. Generate a QR code for the given url parameter
59+ # 2. Return the QR code image as a base64-encoded string
60+ # Hint: You can use the qrcode library to generate QR codes.
61+ # Take a look at the documentation here: https://pypi.org/project/qrcode/
62+ encoded_string = ""
63+ # return {"image_base64": f"data:image/png;base64,{encoded_string}"}
9264
9365# Click Statistics Endpoints
9466@app .get ("/stats" , response_model = List [ClickStats ])
@@ -125,21 +97,20 @@ def get_stats_for_url(short_code: str):
12597 "click_count" : data .count ,
12698 }
12799
128- # Returns simple string message
129100@app .get ("/example" )
130101def example_endpoint (param : str ):
131- if param == "error" :
132- raise HTTPException (status_code = 400 , detail = "Invalid parameter value" )
133- return {"message" : f"This is an example endpoint with parameter: { param } " }
102+ if param == "error" :
103+ raise HTTPException (status_code = 400 , detail = "Invalid parameter value" )
104+ return {"message" : f"This is an example endpoint with parameter: { param } " }
134105
135106# Returns image in base64 format
136107@app .get ("/example-image" )
137108def example_image_endpoint ():
138- file_path = "image.jpeg"
139- if os .path .exists (file_path ):
140- with open (file_path , "rb" ) as image_file :
141- encoded_string = base64 .b64encode (image_file .read ()).decode ('utf-8' )
142- prefix = "data:image/jpeg;base64,"
143- return {"image_base64" : prefix + encoded_string }
144- else :
145- raise HTTPException (status_code = 404 , detail = "Image not found" )
109+ file_path = "image.jpeg"
110+ if os .path .exists (file_path ):
111+ with open (file_path , "rb" ) as image_file :
112+ encoded_string = base64 .b64encode (image_file .read ()).decode ('utf-8' )
113+ prefix = "data:image/jpeg;base64,"
114+ return {"image_base64" : prefix + encoded_string }
115+ else :
116+ raise HTTPException (status_code = 404 , detail = "Image not found" )
0 commit comments