From 0ee2010338d7c4505928612151b5320052ba9821 Mon Sep 17 00:00:00 2001 From: MohitKatare-protean Date: Thu, 3 Apr 2025 12:35:13 +0530 Subject: [PATCH] change for the context keys --- pkg/model/model.go | 7 +++++-- .../implementation/reqpreprocessor/reqpreprocessor.go | 8 ++++---- .../reqpreprocessor/reqpreprocessor_test.go | 4 +++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/model/model.go b/pkg/model/model.go index 621bdeb..53ff27e 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -38,10 +38,13 @@ const ( UnaAuthorizedHeaderGateway string = "Proxy-Authenticate" ) -type contextKey string +type ContextKey string // MsgIDKey is the context key used to store and retrieve the message ID in a request context. -const MsgIDKey = contextKey("message_id") +const MsgIDKey = ContextKey("message_id") + +// SubscriberIDKey is the context key used to store and retrieve the subscriber ID in a request context. +const SubscriberIDKey = ContextKey("subscriber_id") // Role defines the type of participant in the network. type Role string diff --git a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor.go b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor.go index 24ffa67..ca9f2eb 100644 --- a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor.go +++ b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor.go @@ -10,6 +10,7 @@ import ( "net/http" "github.com/beckn/beckn-onix/pkg/log" + "github.com/beckn/beckn-onix/pkg/model" "github.com/google/uuid" ) @@ -24,7 +25,6 @@ type becknRequest struct { } const contextKey = "context" -const subscriberIDKey = "subscriber_id" // NewPreProcessor creates a middleware that processes incoming HTTP requests by extracting // and modifying the request context based on the provided configuration. @@ -53,14 +53,14 @@ func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) { subID = req.Context["bpp_id"] } if subID != nil { - log.Debugf(ctx, "adding subscriberId to request:%s, %v", subscriberIDKey, subID) + log.Debugf(ctx, "adding subscriberId to request:%s, %v", model.SubscriberIDKey, subID) // TODO: Add a ContextKey type in model and use it here instead of raw context key. - ctx = context.WithValue(ctx, subscriberIDKey, subID) + ctx = context.WithValue(ctx, model.SubscriberIDKey, subID) } for _, key := range cfg.ContextKeys { value := uuid.NewString() updatedValue := update(req.Context, key, value) - ctx = context.WithValue(ctx, key, updatedValue) + ctx = context.WithValue(ctx, model.ContextKey(key), updatedValue) } reqData := map[string]any{"context": req.Context} updatedBody, _ := json.Marshal(reqData) diff --git a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go index d70af8e..f35ee68 100644 --- a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go +++ b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go @@ -6,6 +6,8 @@ import ( "net/http" "net/http/httptest" "testing" + + "github.com/beckn/beckn-onix/pkg/model" ) func TestNewUUIDSetterSuccessCases(t *testing.T) { @@ -67,7 +69,7 @@ func TestNewUUIDSetterSuccessCases(t *testing.T) { ctx := r.Context() w.WriteHeader(http.StatusOK) - subID, ok := ctx.Value(subscriberIDKey).(string) + subID, ok := ctx.Value(model.SubscriberIDKey).(string) if !ok { http.Error(w, "Subscriber ID not found", http.StatusInternalServerError) return