From 3cfcce192326c928695f8af0d12808b32eb0f584 Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Mon, 22 Oct 2018 15:29:47 -0500 Subject: [PATCH] Utility for checking if API request Utility function for determining whether a request is directed at the API. Can specify that the check is for a certain API "type", like shibboleth, X509, or basic. --- gracedb/api/utils.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gracedb/api/utils.py b/gracedb/api/utils.py index 129bec525..940a04b6e 100644 --- a/gracedb/api/utils.py +++ b/gracedb/api/utils.py @@ -83,4 +83,23 @@ def api_reverse(viewname, args=None, kwargs=None, request=None, format=None, if request is None: url = build_absolute_uri(url) - return url + return url + + +def is_api_request(request_path, namespace='x509'): + """ + Returns True/False based on whether the request is directed to the API + The namespace variable determines whether we should be testing for the + 'normal' API (x509 auth), basic auth API, or web API. + + These namespaces are specified in the root urlconf (config/urls.py). + """ + + # This is hard-coded because things break if we try to import it from .urls + api_app_name = 'api' + + resolver_match = resolve(request_path) + if (resolver_match.app_name == api_app_name and + resolver_match.namespace == namespace): + return True + return False -- GitLab