fix: reqstruct

This commit is contained in:
mayur.popli
2025-03-24 09:24:14 +05:30
parent 97db5cf6e7
commit 81a82df6f9

View File

@@ -17,6 +17,10 @@ type Config struct {
Role string Role string
} }
type BecknRequest struct {
Context map[string]any `json:"context"`
}
type contextKeyType string type contextKeyType string
const contextKey = "context" const contextKey = "context"
@@ -28,42 +32,35 @@ func NewUUIDSetter(cfg *Config) (func(http.Handler) http.Handler, error) {
} }
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data map[string]any
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError) http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return return
} }
if err := json.Unmarshal(body, &data); err != nil { var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
http.Error(w, "Failed to decode request body", http.StatusBadRequest) http.Error(w, "Failed to decode request body", http.StatusBadRequest)
return return
} }
contextRaw := data[contextKey] if req.Context == nil {
if contextRaw == nil {
http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest) http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest)
return return
} }
contextData, ok := contextRaw.(map[string]any)
if !ok {
http.Error(w, fmt.Sprintf("%s field is not a map.", contextKey), http.StatusBadRequest)
return
}
var subID any var subID any
switch cfg.Role { switch cfg.Role {
case "bap": case "bap":
subID = contextData["bap_id"] subID = req.Context["bap_id"]
case "bpp": case "bpp":
subID = contextData["bpp_id"] subID = req.Context["bpp_id"]
} }
ctx := context.WithValue(r.Context(), subscriberIDKey, subID) ctx := context.WithValue(r.Context(), subscriberIDKey, subID)
for _, key := range cfg.CheckKeys { for _, key := range cfg.CheckKeys {
value := uuid.NewString() value := uuid.NewString()
updatedValue := update(contextData, key, value) updatedValue := update(req.Context, key, value)
ctx = context.WithValue(ctx, contextKeyType(key), updatedValue) ctx = context.WithValue(ctx, contextKeyType(key), updatedValue)
} }
data[contextKey] = contextData reqData := map[string]any{"context": req.Context}
updatedBody, err := json.Marshal(data) updatedBody, err := json.Marshal(reqData)
if err != nil { if err != nil {
http.Error(w, "Failed to marshal updated JSON", http.StatusInternalServerError) http.Error(w, "Failed to marshal updated JSON", http.StatusInternalServerError)
return return