51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import json
|
|
from enum import Enum
|
|
from functools import wraps
|
|
|
|
VALID_HTTP_METHODS = ['GET', 'POST']
|
|
|
|
def register_url(url, http_method):
|
|
def actual_decorator(f):
|
|
@wraps(f)
|
|
def _impl(self, *f_args, **f_kwargs):
|
|
RestAPI._register_url(self, f, url, http_method)
|
|
return f(self, *f_args, **f_kwargs)
|
|
return actual_decorator
|
|
|
|
class RestAPI:
|
|
def __init__(self, database=None):
|
|
self._rest_methods = {}
|
|
self._database = database if database else {}
|
|
|
|
def get(self, url, payload=None):
|
|
methods = self._rest_methods['GET']
|
|
if url in methods.keys():
|
|
return methods[url](payload)
|
|
else:
|
|
raise Exception('Error 404')
|
|
|
|
def post(self, url, payload=None):
|
|
methods = self._rest_methods['POST']
|
|
if url in methods.keys():
|
|
return methods[url](payload)
|
|
else:
|
|
raise Exception('Error 404')
|
|
|
|
def _register_url(self, f, url, http_method):
|
|
|
|
if http_method not in VALID_HTTP_METHODS:
|
|
raise Exception('Invalid Http method')
|
|
|
|
self._rest_methods[http_method][url] = f
|
|
|
|
@register_url('/users', 'GET')
|
|
def users(self, payload=None):
|
|
pass
|
|
|
|
@register_url('/add', 'POST')
|
|
def add(self, payload=None):
|
|
pass
|
|
|
|
@register_url('/iou', 'POST')
|
|
def iou(self, payload=None):
|
|
pass
|