-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cae8496
commit 5122a1b
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
endpoints: | ||
download_data: | ||
route: '/api/data/cache' | ||
methods: ['POST'] | ||
task_name: "DOWNLOAD DATA" | ||
celery_task: preview_download_data | ||
schema: DownloadSchema | ||
description: 'Endpoint for downloading data through repo2data.' | ||
tags: ['Data'] | ||
parameters: | ||
- name: user | ||
- name: id | ||
- name: repository_url | ||
- name: email | ||
default: None | ||
- name: is_overwrite | ||
default: None | ||
|
||
build_book: | ||
route: '/api/book/build' | ||
methods: ['POST'] | ||
task_name: "BUILD BOOK" | ||
celery_task: preview_build_book | ||
schema: BuildSchema | ||
description: 'Endpoint for building reproducibility assets on the preview BinderHub instance: Repo2Data, (Binder) Repo2Docker, Jupyter Book.' | ||
tags: ['Book'] | ||
parameters: | ||
- name: user | ||
- name: id | ||
- name: repo_url | ||
- name: commit_hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from flask import Flask | ||
from flask_apispec import use_kwargs, doc | ||
from flask_htpasswd import HtPasswdAuth | ||
from common import load_yaml | ||
from endpoint_handler import handle_endpoint | ||
import importlib | ||
|
||
def generate_routes(app: Flask, htpasswd: HtPasswdAuth): | ||
|
||
endpoints = load_yaml('api/endpoints.yaml') | ||
|
||
for endpoint_name, config in endpoints.items(): | ||
route = config['route'] | ||
methods = config['methods'] | ||
schema_name = config['schema'] | ||
description = config['description'] | ||
tags = config['tags'] | ||
parameters = config['parameters'] | ||
|
||
# Dynamically import the schema | ||
schema_module = importlib.import_module('schemas') # Adjust this import as needed | ||
schema = getattr(schema_module, schema_name) | ||
|
||
# Create the function signature dynamically | ||
param_str = ", ".join(p['name'] + "=" + str(p.get('default', 'None')) for p in parameters) | ||
func_str = f"def {endpoint_name}({param_str}):\n" | ||
func_str += f" return handle_endpoint('{endpoint_name}', {', '.join(p['name'] for p in parameters)})" | ||
|
||
# Create the function object | ||
func_locals = {} | ||
exec(func_str, globals(), func_locals) | ||
view_func = func_locals[endpoint_name] | ||
|
||
# Apply decorators | ||
view_func = app.route(route, methods=methods)(view_func) | ||
view_func = htpasswd.required(view_func) | ||
view_func = marshal_with(None, code=422, description="Cannot validate the payload, missing or invalid entries.")(view_func) | ||
view_func = use_kwargs(schema())(view_func) | ||
view_func = doc(description=description, tags=tags)(view_func) | ||
|
||
# Add the view function to the app | ||
app.view_functions[endpoint_name] = view_func | ||
|
||
|
||
""" | ||
This will be then followed by | ||
""" | ||
|
||
# from flask import Flask | ||
# from flask_htpasswd import HtPasswdAuth | ||
# from route_generator import generate_routes | ||
|
||
# app = Flask(__name__) | ||
# htpasswd = HtPasswdAuth(app) | ||
|
||
# generate_routes(app, htpasswd) | ||
|
||
# if __name__ == '__main__': | ||
# app.run() |