LussovAPI
TL;DR: py API container, pip install -r requirements.txt, example, main configuration
Long version:
Install Dependancies
Download file requirements.txt
requirements.txt
Flask>=2.0.2
Flask_RESTful>=0.3.9
pymongo>=3.12.1
requests>=2.26.0
...
Install pip dependancies by running the following command inside of cmd or a shell based application
cmd / shell
pip install -r requirements.txt
Initialization and Setup
Create or use a previously existing file that represents your entry script.
A simple example can be found within the repository at main.py
main.py
from server import Server
def main():
server = Server(
)
if __name__ == '__main__':
main()
There are several configuration options available directly from the initialization of the Server object.
These are passed to the opject as loose type parameters.
name: str = 'Server',
# name of the server
host: str = 'localhost',
# server hosting ip
port: Optional[Union[int, str]] = 5000,
# server hosting port
config: Optional[Union[str, dict]] = 'config.json',
# server configuration & value store
apidir: Optional[str] = 'apis',
# the directory in which endpoints exist
Endpoints and Hierarchy
Each endpoint represents its own API directory, similar to how classes contain functions.
For example, both the default Ping and Test endpoints represent the test API endpoint.
How does this apply within our file structure?
.
├── ...
├── apis # Alternatively, directory defined in apidir
│ ├── ... # ...
│ └── test # API endpoint test, all endpoints inherit prefix
| │ ├── ... # ...
| │ └── ping.py # Contains endpoints Ping and Test
└── ...
Within this example, an API endpoint from ping.py would look as follows:
http://localhost:5000/test/{endpoint}?content={}
Within the file itself, it becomes clear that these endpoints function similar to React routing, requiring functional exporting.
...
"""
Endpoints
"""
class Ping(Endpoint):
def __init__(self, **kwargs) -> Endpoint:
super().__init__(**kwargs)
@Endpoint.RequiresArgs()
def get(self, args: dict) -> tuple:
print(args)
return {'response' : 'Pong'}, Endpoint.Codes.OK
class Test(Endpoint):
def __init__(self, **kwargs) -> Endpoint:
super().__init__(**kwargs)
def get(self) -> tuple:
return {}, Endpoint.Codes.OK
"""
Router
"""
def route() -> Optional[set]:
return [
Ping,
Test
]
Within each endpoint, there are several HTTP methods available for utilizing, such as:
get
def get(self) -> tuple:
return {}, Endpoint.Codes.OK
post
def post(self) -> tuple:
return {}, Endpoint.Codes.OK
put
def put(self) -> tuple:
return {}, Endpoint.Codes.OK
delete
def delete(self) -> tuple:
return {}, Endpoint.Codes.OK
... and more
A list of all modern HTTP methods and their descriptions can be found here
You may also notice that there are several applicable decorators available.
Decorators are not required to maintain the functionality of the application, but are required if you wish to pass arguments to HTTP methods.
default arguments decorator
@Endpoint.RequiresArgs()
# Passes arguments from content to keyword args
Requires keyword content to be present within the query. All values passed to args should be passed through content as a string, per conventions.
That's all, folks