1+ # Standard library imports
2+ from typing import Dict , List , Tuple , Union
3+
14# Third-party imports
25from flask import request
36from flask_restful import Resource
@@ -11,7 +14,7 @@ class StorageEntry(db.Model):
1114 entry_key = db .Column (db .String (255 ), nullable = False , unique = True )
1215 entry_value = db .Column (db .Text , nullable = False )
1316
14- def to_dict (self ):
17+ def to_dict (self ) -> Dict [ str , str ] :
1518 return {
1619 'key' : self .entry_key ,
1720 'value' : self .entry_value
@@ -22,15 +25,15 @@ class StorageFile(db.Model):
2225 file_path = db .Column (db .String (500 ), nullable = False , unique = True )
2326 file_content = db .Column (db .Text , nullable = False )
2427
25- def to_dict (self ):
28+ def to_dict (self ) -> Dict [ str , str ] :
2629 return {
2730 'path' : self .file_path ,
2831 'content' : self .file_content
2932 }
3033
3134# Storage Resources for key-value and file operations
3235class StorageEntryResource (Resource ):
33- def get (self , entry_key ) :
36+ def get (self , entry_key : str ) -> Dict [ str , str ] :
3437 """Fetch entry value by key"""
3538 entry = StorageEntry .query .filter_by (entry_key = entry_key ).first ()
3639 if entry :
@@ -40,7 +43,7 @@ def get(self, entry_key):
4043 default_value = request .args .get ('default' , '' )
4144 return {'value' : default_value }
4245
43- def post (self , entry_key ) :
46+ def post (self , entry_key : str ) -> Union [ Dict [ str , str ], Tuple [ Dict [ str , str ], int ]] :
4447 """Save entry value"""
4548 data = request .get_json ()
4649 if not data or 'value' not in data :
@@ -61,7 +64,7 @@ def post(self, entry_key):
6164 return {'error' : 'Failed to save entry' }, 500
6265
6366class StorageResource (Resource ):
64- def get (self , path ) :
67+ def get (self , path : str ) -> Union [ Dict [ str , Union [ List [ str ], str ]], Tuple [ Dict [ str , str ], int ]] :
6568 """Get file content or list directory based on path"""
6669 # Handle empty path as root directory
6770 if not path :
@@ -107,7 +110,7 @@ def get(self, path):
107110 else :
108111 return {'error' : 'File not found' }, 404
109112
110- def post (self , path ) :
113+ def post (self , path : str ) -> Union [ Dict [ str , str ], Tuple [ Dict [ str , str ], int ]] :
111114 """Save file content (only for files, not directories)"""
112115 if path .endswith ('/' ):
113116 return {'error' : 'Cannot save content to a directory path' }, 400
@@ -130,7 +133,7 @@ def post(self, path):
130133 db .session .rollback ()
131134 return {'error' : 'Failed to save file' }, 500
132135
133- def delete (self , path ) :
136+ def delete (self , path : str ) -> Union [ Dict [ str , str ], Tuple [ Dict [ str , str ], int ]] :
134137 """Delete file or directory"""
135138 if path .endswith ('/' ):
136139 # Delete directory (all files starting with this path)
@@ -152,7 +155,7 @@ def delete(self, path):
152155 return {'error' : 'Failed to delete' }, 500
153156
154157class StorageFileRenameResource (Resource ):
155- def post (self ):
158+ def post (self ) -> Union [ Dict [ str , str ], Tuple [ Dict [ str , str ], int ]] :
156159 """Rename file or directory"""
157160 data = request .get_json ()
158161 if not data or 'old_path' not in data or 'new_path' not in data :
@@ -182,7 +185,7 @@ def post(self):
182185 return {'error' : 'Failed to rename' }, 500
183186
184187class StorageRootResource (Resource ):
185- def get (self ):
188+ def get (self ) -> Dict [ str , List [ str ]] :
186189 """List all top-level files and directories"""
187190 files = StorageFile .query .all ()
188191 # Extract top-level files and directories
0 commit comments