Better handling of API root resource

parent 23808123
......@@ -50,12 +50,14 @@ CONFIG = {
API_ROOT_data = os.path.join(CONFIG_HTTP["API_ROOT"], "data")
API_ROOT_STAGING_data = os.path.join(CONFIG_HTTP["API_ROOT_STAGING"], "data")
API_ROOT_ops = os.path.join(CONFIG_HTTP["API_ROOT"], "operations")
API_ROOT_ylv = os.path.join(CONFIG_HTTP["API_ROOT"], "yang-library-version")
def load_config(filename: str) -> bool:
global API_ROOT_data
global API_ROOT_STAGING_data
global API_ROOT_ops
global API_ROOT_ylv
with open(filename) as conf_fd:
conf_yaml = yaml.load(conf_fd)
......@@ -69,6 +71,7 @@ def load_config(filename: str) -> bool:
API_ROOT_data = os.path.join(CONFIG_HTTP["API_ROOT"], "data")
API_ROOT_STAGING_data = os.path.join(CONFIG_HTTP["API_ROOT_STAGING"], "data")
API_ROOT_ops = os.path.join(CONFIG_HTTP["API_ROOT"], "operations")
API_ROOT_ylv = os.path.join(CONFIG_HTTP["API_ROOT"], "yang-library-version")
def print_config():
......
......@@ -152,19 +152,24 @@ def unknown_req_handler(headers: OrderedDict, data: Optional[str], client_cert:
)
def api_root_handler(headers: OrderedDict, data: Optional[str], client_cert: SSLCertT):
# Top level api resource (appendix B.1.1)
def _get_yl_date() -> str:
try:
yang_lib_date_ts = os.path.getmtime(os.path.join(CONFIG_GLOBAL["YANG_LIB_DIR"], "yang-library-data.json"))
yang_lib_date = datetime.fromtimestamp(yang_lib_date_ts).strftime("%Y-%m-%d")
except OSError:
yang_lib_date = None
return yang_lib_date
def api_root_handler(headers: OrderedDict, data: Optional[str], client_cert: SSLCertT):
# Top level api resource (appendix B.1.1)
top_res = {
"ietf-restconf:restconf": {
"data": {},
"operations": {},
"yang-library-version": yang_lib_date
"yang-library-version": _get_yl_date()
}
}
......@@ -172,6 +177,15 @@ def api_root_handler(headers: OrderedDict, data: Optional[str], client_cert: SSL
return HttpResponse(HttpStatus.Ok, response.encode(), CT_YANG_JSON)
def api_ylv_handler(headers: OrderedDict, data: Optional[str], client_cert: SSLCertT):
ylv = {
"ietf-restconf:yang-library-version": _get_yl_date()
}
response = json.dumps(ylv, indent=4)
return HttpResponse(HttpStatus.Ok, response.encode(), CT_YANG_JSON)
def _get(ds: BaseDatastore, req_headers: OrderedDict, pth: str, username: str, staging: bool=False) -> HttpResponse:
url_split = pth.split("?")
url_path = url_split[0]
......
......@@ -13,7 +13,7 @@ from h2.events import DataReceived, RequestReceived, RemoteSettingsChanged, Stre
from . import http_handlers as handlers
from .http_handlers import HttpResponse, HttpStatus, RestconfErrType, ERRTAG_MALFORMED, ERRTAG_OPNOTSUPPORTED, ERRTAG_REQLARGE
from .config import CONFIG_HTTP, API_ROOT_data, API_ROOT_STAGING_data, API_ROOT_ops
from .config import CONFIG_HTTP, API_ROOT_data, API_ROOT_STAGING_data, API_ROOT_ops, API_ROOT_ylv
from .data import BaseDatastore
from .helpers import SSLCertT, LogHelpers
......@@ -152,7 +152,7 @@ class H2Protocol(asyncio.Protocol):
# Find and run handler for specific URI and HTTP method
def run_request_handler(self, headers: OrderedDict, stream_id: int, data: Optional[str]):
url_path = headers[":path"].split("?")[0]
url_path = headers[":path"].split("?")[0].rstrip("/")
method = headers[":method"]
if method == "HEAD":
......@@ -257,6 +257,7 @@ class RestServer:
# Register HTTP handlers
api_get_root = handlers.api_root_handler
api_get_ylv = handlers.api_ylv_handler
api_get = handlers.create_get_api(datastore)
api_get_st = handlers.create_get_staging_api(datastore)
api_post = handlers.create_post_api(datastore)
......@@ -264,9 +265,10 @@ class RestServer:
api_delete = handlers.create_api_delete(datastore)
api_op = handlers.create_api_op(datastore)
self.http_handlers.register(lambda m, p: (m == "GET") and (p == CONFIG_HTTP["API_ROOT"]), api_get_root)
self.http_handlers.register(lambda m, p: (m == "GET") and (p.startswith(API_ROOT_data)), api_get)
self.http_handlers.register(lambda m, p: (m == "GET") and (p.startswith(API_ROOT_STAGING_data)), api_get_st)
self.http_handlers.register(lambda m, p: (m == "GET") and (p == API_ROOT_ylv), api_get_ylv)
self.http_handlers.register(lambda m, p: (m == "GET") and (p == CONFIG_HTTP["API_ROOT"]), api_get_root)
self.http_handlers.register(lambda m, p: (m == "POST") and (p.startswith(API_ROOT_data)), api_post)
self.http_handlers.register(lambda m, p: (m == "PUT") and (p.startswith(API_ROOT_data)), api_put)
self.http_handlers.register(lambda m, p: (m == "DELETE") and (p.startswith(API_ROOT_data)), api_delete)
......@@ -278,7 +280,7 @@ class RestServer:
def register_static_handlers(self):
global h2_handlers
self.http_handlers.register(lambda m, p: m == "GET", handlers.get_file)
self.http_handlers.register(lambda m, p: (m == "GET") and not (p.startswith(CONFIG_HTTP["API_ROOT"])), handlers.get_file)
self.http_handlers.register_default(handlers.unknown_req_handler)
h2_handlers = self.http_handlers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment