change for the context keys

This commit is contained in:
MohitKatare-protean
2025-04-03 12:35:13 +05:30
parent a2c27b4fbf
commit 0ee2010338
3 changed files with 12 additions and 7 deletions

View File

@@ -38,10 +38,13 @@ const (
UnaAuthorizedHeaderGateway string = "Proxy-Authenticate" 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. // 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. // Role defines the type of participant in the network.
type Role string type Role string

View File

@@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"github.com/beckn/beckn-onix/pkg/log" "github.com/beckn/beckn-onix/pkg/log"
"github.com/beckn/beckn-onix/pkg/model"
"github.com/google/uuid" "github.com/google/uuid"
) )
@@ -24,7 +25,6 @@ type becknRequest struct {
} }
const contextKey = "context" const contextKey = "context"
const subscriberIDKey = "subscriber_id"
// NewPreProcessor creates a middleware that processes incoming HTTP requests by extracting // NewPreProcessor creates a middleware that processes incoming HTTP requests by extracting
// and modifying the request context based on the provided configuration. // 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"] subID = req.Context["bpp_id"]
} }
if subID != nil { 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. // 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 { for _, key := range cfg.ContextKeys {
value := uuid.NewString() value := uuid.NewString()
updatedValue := update(req.Context, key, value) 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} reqData := map[string]any{"context": req.Context}
updatedBody, _ := json.Marshal(reqData) updatedBody, _ := json.Marshal(reqData)

View File

@@ -6,6 +6,8 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/beckn/beckn-onix/pkg/model"
) )
func TestNewUUIDSetterSuccessCases(t *testing.T) { func TestNewUUIDSetterSuccessCases(t *testing.T) {
@@ -67,7 +69,7 @@ func TestNewUUIDSetterSuccessCases(t *testing.T) {
ctx := r.Context() ctx := r.Context()
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
subID, ok := ctx.Value(subscriberIDKey).(string) subID, ok := ctx.Value(model.SubscriberIDKey).(string)
if !ok { if !ok {
http.Error(w, "Subscriber ID not found", http.StatusInternalServerError) http.Error(w, "Subscriber ID not found", http.StatusInternalServerError)
return return