From ec30b064ff3739b75dd028c17f799c2543508543 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 10 Sep 2024 08:38:00 -0700 Subject: [PATCH 1/2] 88 enable branching in graphql --- netbox_branching/middleware.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/netbox_branching/middleware.py b/netbox_branching/middleware.py index 73de3dd..1084b78 100644 --- a/netbox_branching/middleware.py +++ b/netbox_branching/middleware.py @@ -46,7 +46,10 @@ def get_active_branch(request): Return the active Branch (if any). """ # The active Branch is specified by HTTP header for REST API requests. - if request.path_info.startswith(reverse('api-root')) and (schema_id := request.headers.get(BRANCH_HEADER)): + if ( + request.path_info.startswith(reverse('api-root')) or ( + request.path_info.startswith(reverse('graphql')))) and ( + schema_id := request.headers.get(BRANCH_HEADER)): branch = Branch.objects.get(schema_id=schema_id) if not branch.ready: return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") From 291bd73066fbdf62ac698e40a99faa752ac30e77 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 10 Sep 2024 13:55:26 -0400 Subject: [PATCH 2/2] Tweak formatting of conditional logic --- netbox_branching/middleware.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/netbox_branching/middleware.py b/netbox_branching/middleware.py index 1084b78..79f9bbc 100644 --- a/netbox_branching/middleware.py +++ b/netbox_branching/middleware.py @@ -45,15 +45,13 @@ def get_active_branch(request): """ Return the active Branch (if any). """ - # The active Branch is specified by HTTP header for REST API requests. - if ( - request.path_info.startswith(reverse('api-root')) or ( - request.path_info.startswith(reverse('graphql')))) and ( - schema_id := request.headers.get(BRANCH_HEADER)): - branch = Branch.objects.get(schema_id=schema_id) - if not branch.ready: - return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") - return branch + # The active Branch is specified by HTTP header for REST & GraphQL API requests. + if request.path_info.startswith(reverse('api-root')) or request.path_info.startswith(reverse('graphql')): + if schema_id := request.headers.get(BRANCH_HEADER): + branch = Branch.objects.get(schema_id=schema_id) + if not branch.ready: + return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") + return branch # Branch activated/deactivated by URL query parameter elif QUERY_PARAM in request.GET: