diff --git a/README.md b/README.md index bc0196f..f43f8e7 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,20 @@ Steps to run: If Flask doesn't run, it's possible you'd need to first export FLASK_APP as an environment variable, set to "wsgi" (so in Bash, export FLASK_APP=wsgi). However, the script is named wsgi.py specifically because Flask is supposed to auto-detect it. So if this issue ever did come up, please ping Luke so he can look into it. + +## Testing in VS Code + +Be sure to add the following to `.vscode/settings.json` in your local checkout: + +```json + "python.testing.unittestEnabled": true, + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests/cases", + "-t", + "./", + "-p", + "t_*.py" + ], +``` diff --git a/src/ogd/apis/HelloAPI.py b/src/ogd/apis/HelloAPI.py index 9a32825..0f3a4ba 100644 --- a/src/ogd/apis/HelloAPI.py +++ b/src/ogd/apis/HelloAPI.py @@ -13,6 +13,8 @@ # import OGD libraries # import locals +from ogd.apis.resources.Hello import Hello +from ogd.apis.resources.ParamHello import ParamHello from ogd.apis.configs.ServerConfig import ServerConfig from ogd.apis.models.enums.ResponseStatus import ResponseStatus from ogd.apis.models.enums.RESTType import RESTType @@ -22,62 +24,12 @@ class HelloAPI: @staticmethod def register(app:Flask, server_config:ServerConfig): api = Api(app) - api.add_resource(HelloAPI.Hello, '/hello') - api.add_resource(HelloAPI.ParamHello, '/p_hello/') + api.add_resource(Hello, '/hello') + api.add_resource(ParamHello, '/p_hello/') api.add_resource(HelloAPI.Version, '/version') HelloAPI.server_config = server_config - class Hello(Resource): - def get(self): - ret_val = APIResponse( - req_type = RESTType.GET, - val = None, - msg = "Hello! You GETted successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - - def post(self): - ret_val = APIResponse( - req_type = RESTType.POST, - val = None, - msg = "Hello! You POSTed successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - - def put(self): - ret_val = APIResponse( - req_type = RESTType.PUT, - val = None, - msg = "Hello! You PUTted successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - - class ParamHello(Resource): - def get(self, name): - ret_val = APIResponse( - req_type = RESTType.GET, - val = None, - msg = f"Hello {name}! You GETted successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - - def post(self, name): - ret_val = APIResponse( - req_type = RESTType.POST, - val = None, - msg = f"Hello {name}! You POSTed successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - - def put(self, name): - ret_val = APIResponse( - req_type = RESTType.PUT, - val = None, - msg = f"Hello {name}! You PUTted successfully!", - status = ResponseStatus.OK) - return ret_val.AsDict - class Version(Resource): def get(self): ret_val = APIResponse( diff --git a/src/ogd/apis/models/APIResponse.py b/src/ogd/apis/models/APIResponse.py index 7727ca7..7e8a376 100644 --- a/src/ogd/apis/models/APIResponse.py +++ b/src/ogd/apis/models/APIResponse.py @@ -34,15 +34,15 @@ def __init__(self, req_type:Optional[RESTType | str], val:Optional[Map], msg:str self._type = RESTType[req_type] else: self._type = None - if isinstance(val, dict): + if isinstance(val, dict) or val is None: self._val = val else: try: self._val = json.loads(str(val)) except json.decoder.JSONDecodeError as err: abbreviated_val = f"{str(val)[:20]}..." if len(str(val)) > 20 else str(val) - msg = f"API response 'value' field contained value '{abbreviated_val}' with invalid type {type(val)}, which could not be converted to a dictionary. Attempting to do so resulted in error:\n{err}\nThe value field will be left blank." - Logger.Log(msg, logging.ERROR) + _msg = f"API response 'value' field contained value '{abbreviated_val}' with invalid type {type(val)}, which could not be converted to a dictionary. Attempting to do so resulted in error:\n{err}\nThe value field will be left blank." + Logger.Log(_msg, logging.ERROR) self._val = None self._msg : str = msg self._status : ResponseStatus = status diff --git a/src/ogd/apis/resources/Hello.py b/src/ogd/apis/resources/Hello.py new file mode 100644 index 0000000..aa997a6 --- /dev/null +++ b/src/ogd/apis/resources/Hello.py @@ -0,0 +1,29 @@ +# import 3rd-party libraries +from flask_restful import Resource + +from ogd.apis.models.APIResponse import APIResponse, RESTType, ResponseStatus + +class Hello(Resource): + def get(self): + ret_val = APIResponse( + req_type = RESTType.GET, + val = None, + msg = "Hello! You GETted successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict + + def post(self): + ret_val = APIResponse( + req_type = RESTType.POST, + val = None, + msg = "Hello! You POSTed successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict + + def put(self): + ret_val = APIResponse( + req_type = RESTType.PUT, + val = None, + msg = "Hello! You PUTted successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict diff --git a/src/ogd/apis/resources/ParamHello.py b/src/ogd/apis/resources/ParamHello.py new file mode 100644 index 0000000..7e60127 --- /dev/null +++ b/src/ogd/apis/resources/ParamHello.py @@ -0,0 +1,29 @@ +# import 3rd-party libraries +from flask_restful import Resource + +from ogd.apis.models.APIResponse import APIResponse, RESTType, ResponseStatus + +class ParamHello(Resource): + def get(self, name): + ret_val = APIResponse( + req_type = RESTType.GET, + val = None, + msg = f"Hello {name}! You GETted successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict + + def post(self, name): + ret_val = APIResponse( + req_type = RESTType.POST, + val = None, + msg = f"Hello {name}! You POSTed successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict + + def put(self, name): + ret_val = APIResponse( + req_type = RESTType.PUT, + val = None, + msg = f"Hello {name}! You PUTted successfully!", + status = ResponseStatus.OK) + return ret_val.AsDict