From 3cff04bab1b9de3ad56a036690982959d524955c Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 25 Sep 2023 17:00:05 -0400 Subject: [PATCH] Add some API endpoints Add: /api/config/ to display all engines, and all books /api/config// to display the config for a specific engine /api/networks// list all of the networks, and the default, for an engine /api/networks/// download a network, given the sha or the name --- OpenBench/urls.py | 6 ++++ OpenBench/views.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 486b7d5..4a6e31c 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -79,6 +79,12 @@ urlpatterns = [ django.urls.path(r'clientSubmitError/', OpenBench.views.client_submit_error), django.urls.path(r'clientSubmitResults/', OpenBench.views.client_submit_results), + # Nice endpoints, which can be hit from the website or with credentials cleanly + django.urls.path(r'api/config/', OpenBench.views.api_configs), + django.urls.path(r'api/config//', OpenBench.views.api_configs), + django.urls.path(r'api/networks//', OpenBench.views.api_networks), + django.urls.path(r'api/networks///', OpenBench.views.api_network_download), + # Redirect anything else to the Index django.urls.path(r'', OpenBench.views.index), diff --git a/OpenBench/views.py b/OpenBench/views.py index 57c5059..56d36fa 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -745,6 +745,92 @@ def client_submit_results(request): # Returns {}, or { 'stop' : True } return JsonResponse(OpenBench.utils.update_test(request, machine)) + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def api_response(data): + return HttpResponse(json.dumps(data, indent=4), content_type='application/json') + +def api_authenticate(request, require_enabled=False): + + try: + + # Don't require a login for Public frameworks + if not require_enabled and not OpenBench.config.REQUIRE_LOGIN_TO_VIEW: + return True + + # Request is made from a browser, and is already logged in + if request.user.is_authenticated: + return not require_enabled or bool(Profile.objects.get(user=request.user).enabled) + + # Request might be made from the command line. Check the headers + user = django.contrib.auth.authenticate( + request.POST['username'], request.POST['password']) + return not require_enabled or Profile.objects.get(user=user).enabled + + except Exception: + import traceback + traceback.print_exc() + return False + +def api_configs(request, engine=None): + + if not api_authenticate(request): + return api_response({ 'error' : 'API requires authentication for this server' }) + + if engine == None: + engines = list(OPENBENCH_CONFIG['engines'].keys()) + books = OPENBENCH_CONFIG['books'] + return api_response({ 'engines' : engines, 'books' : books }) + + if engine in OPENBENCH_CONFIG['engines'].keys(): + return api_response({ 'config' : OPENBENCH_CONFIG['engines'][engine] }) + + return api_response({ 'error' : 'Engine not found. Check /api/config/ for a full list' }) + +def api_networks(request, engine): + + if not api_authenticate(request): + return api_response({ 'error' : 'API requires authentication for this server' }) + + if engine in OPENBENCH_CONFIG['engines'].keys(): + + if not (network := Network.objects.filter(engine=engine, default=True).first()): + return api_response({ 'error' : 'Engine does not have a default Network' }) + + default = { + 'sha' : network.sha256, 'name' : network.name, + 'author' : network.author, 'created' : str(network.created) } + + networks = [ + { 'sha' : network.sha256, 'name' : network.name, + 'author' : network.author, 'created' : str(network.created) } + for network in Network.objects.filter(engine=engine) ] + + return api_response({ 'default' : default, 'networks' : networks }) + + else: + return api_response({ 'error' : 'Engine not found. Check /api/config/ for a full list' }) + +def api_network_download(request, engine, identifier): + + if not api_authenticate(request): + return api_response({ 'error' : 'API requires authentication for this server' }) + + if not api_authenticate(request, require_enabled=True): + return api_response({ 'error' : 'API requires authentication for this endpoint' }) + + if (network := Network.objects.filter(engine=engine, sha256=identifier).first()): + return OpenBench.utils.network_download(request, engine, network) + + if (network := Network.objects.filter(engine=engine, name=identifier).first()): + return OpenBench.utils.network_download(request, engine, network) + + return api_response({ 'error' : 'Engine not found. Check /api/config/ for a full list' }) + + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # BUSINESS VIEWS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #