add error statement for context
This commit is contained in:
109
test.go
109
test.go
@@ -1,77 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"beckn-onix/shared/plugin"
|
||||
"beckn-onix/shared/plugin/definition"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/plugin/definition"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/plugin"
|
||||
)
|
||||
|
||||
var (
|
||||
manager *plugin.Manager
|
||||
validators map[string]definition.Validator
|
||||
manager *plugin.Manager
|
||||
)
|
||||
|
||||
// Payload represents the structure of the payload with context information.
|
||||
type Payload struct {
|
||||
Context struct {
|
||||
Action string `json:"action"`
|
||||
BapID string `json:"bap_id"`
|
||||
BapURI string `json:"bap_uri"`
|
||||
BppID string `json:"bpp_id"`
|
||||
BppURI string `json:"bpp_uri"`
|
||||
Domain string `json:"domain"`
|
||||
Location struct {
|
||||
City struct {
|
||||
Code string `json:"code"`
|
||||
} `json:"city"`
|
||||
Country struct {
|
||||
Code string `json:"code"`
|
||||
} `json:"country"`
|
||||
} `json:"location"`
|
||||
MessageID string `json:"message_id"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
TTL string `json:"ttl"`
|
||||
Version string `json:"version"`
|
||||
} `json:"context"`
|
||||
Message struct {
|
||||
CancellationReasonID string `json:"cancellation_reason_id"`
|
||||
Descriptor struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
} `json:"descriptor"`
|
||||
OrderID string `json:"order_id"`
|
||||
} `json:"message"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
// Load the configuration
|
||||
config, err := plugin.LoadConfig("shared/plugin/plugin.yaml")
|
||||
// Load the configuration.
|
||||
config, err := plugin.LoadConfig("pkg/plugin/plugin.yaml")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load plugins configuration: %v", err)
|
||||
}
|
||||
|
||||
// Initialize the plugin manager
|
||||
// Initialize the plugin manager.
|
||||
manager, err = plugin.NewManager(context.Background(), config)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create PluginManager: %v", err)
|
||||
}
|
||||
|
||||
// Get the validators map
|
||||
validators, defErr := manager.Validators(context.Background())
|
||||
if defErr != (definition.Error{}) {
|
||||
// Get the validator.
|
||||
validator, _, defErr := manager.SchemaValidator(context.Background())
|
||||
if defErr != nil {
|
||||
log.Fatalf("Failed to get validators: %v", defErr)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
validateHandler(w, r, validators)
|
||||
validateHandler(w, r, validator)
|
||||
})
|
||||
fmt.Println("Starting server on port 8084...")
|
||||
err = http.ListenAndServe(":8084", nil)
|
||||
@@ -80,13 +47,13 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func validateHandler(w http.ResponseWriter, r *http.Request, validators map[string]definition.Validator) {
|
||||
func validateHandler(w http.ResponseWriter, r *http.Request, validators definition.SchemaValidator) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract endpoint from request URL
|
||||
// Extract endpoint from request URL.
|
||||
requestURL := r.RequestURI
|
||||
u, err := url.ParseRequestURI(requestURL)
|
||||
if err != nil {
|
||||
@@ -94,40 +61,32 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators map[stri
|
||||
return
|
||||
}
|
||||
|
||||
payloadData, err := ioutil.ReadAll(r.Body)
|
||||
payloadData, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read payload data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var payload Payload
|
||||
err = json.Unmarshal(payloadData, &payload)
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse JSON payload: %v", err)
|
||||
http.Error(w, fmt.Sprintf("Failed to parse JSON payload: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate that the domain and version fields are not empty
|
||||
if payload.Context.Domain == "" || payload.Context.Version == "" {
|
||||
http.Error(w, "Invalid payload: domain and version are required fields", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
schemaFileName := "ondc_trv10_v2.0.0_cancel"
|
||||
|
||||
validator, exists := validators[schemaFileName]
|
||||
if !exists {
|
||||
http.Error(w, fmt.Sprintf("Validator not found for %s", schemaFileName), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
valid, validationErr := validator.Validate(ctx, u, payloadData)
|
||||
if validationErr != (definition.Error{}) {
|
||||
http.Error(w, fmt.Sprintf("Document validation failed: %v", validationErr), http.StatusBadRequest)
|
||||
} else if !valid {
|
||||
http.Error(w, "Document validation failed", http.StatusBadRequest)
|
||||
// validationErr := validators.Validate(ctx, u, payloadData)
|
||||
// if validationErr != (definition.SchemaValError{}) {
|
||||
// http.Error(w, fmt.Sprintf("Document validation failed: %v", validationErr), http.StatusBadRequest)
|
||||
// } else if !valid {
|
||||
// http.Error(w, "Document validation failed", http.StatusBadRequest)
|
||||
// } else {
|
||||
// w.WriteHeader(http.StatusOK)
|
||||
// if _, err := w.Write([]byte("Document validation succeeded!")); err != nil {
|
||||
// log.Fatalf("Failed to write response: %v", err)
|
||||
// }
|
||||
// }
|
||||
validationErr := validators.Validate(ctx, u, payloadData)
|
||||
if validationErr != nil {
|
||||
// Handle other types of errors
|
||||
http.Error(w, fmt.Sprintf("Schema validation failed: %v", validationErr), http.StatusBadRequest)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Document validation succeeded!"))
|
||||
if _, err := w.Write([]byte("Schema validation succeeded!")); err != nil {
|
||||
log.Fatalf("Failed to write response: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user