diff --git a/agents/httpagent.go b/agents/httpagent.go index e25d1dae5..d7bba6aa2 100644 --- a/agents/httpagent.go +++ b/agents/httpagent.go @@ -21,7 +21,6 @@ package agents import ( "fmt" "net/http" - "strconv" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" @@ -58,13 +57,11 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) { utils.HTTPAgent, err.Error())) return } + agReq := newAgentRequest(dcdr) var processed bool - procVars := make(processorVars) - rpl := engine.NewNavigableMap(nil) for _, reqProcessor := range ha.reqProcessors { var lclProcessed bool - if lclProcessed, err = ha.processRequest(reqProcessor, dcdr, - procVars, rpl); lclProcessed { + if lclProcessed, err = ha.processRequest(reqProcessor, agReq); lclProcessed { processed = lclProcessed } if err != nil || @@ -74,13 +71,13 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) { } if err != nil { utils.Logger.Warning( - fmt.Sprintf("<%s> error: %s processing request: %s, process vars: %+v", - utils.HTTPAgent, err.Error(), utils.ToJSON(req), procVars)) + fmt.Sprintf("<%s> error: %s processing request: %s", + utils.HTTPAgent, err.Error(), utils.ToJSON(req))) return // FixMe with returning some error on HTTP level } else if !processed { utils.Logger.Warning( - fmt.Sprintf("<%s> no request processor enabled, ignoring request %s, process vars: %+v", - utils.HTTPAgent, utils.ToJSON(req), procVars)) + fmt.Sprintf("<%s> no request processor enabled, ignoring request %s", + utils.HTTPAgent, utils.ToJSON(req))) return // FixMe with returning some error on HTTP level } encdr, err := newHAReplyEncoder(ha.rplyPayload, w) @@ -90,33 +87,28 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) { utils.HTTPAgent, err.Error())) return } - if err = encdr.encode(rpl); err != nil { + if err = encdr.encode(agReq.Reply); err != nil { utils.Logger.Warning( fmt.Sprintf("<%s> error: %s encoding out %s", - utils.HTTPAgent, err.Error(), utils.ToJSON(rpl))) + utils.HTTPAgent, err.Error(), utils.ToJSON(agReq.Reply))) return } } // processRequest represents one processor processing the request func (ha *HTTPAgent) processRequest(reqProcessor *config.HttpAgntProcCfg, - dP engine.DataProvider, procVars processorVars, - reply *engine.NavigableMap) (processed bool, err error) { - tnt, err := dP.FieldAsString([]string{utils.Tenant}) + agReq *AgentRequest) (processed bool, err error) { + tnt, err := agReq.Request.FieldAsString([]string{utils.Tenant}) if err != nil { return false, err } - if pass, err := ha.filterS.Pass(tnt, reqProcessor.Filters, dP); err != nil { + if pass, err := ha.filterS.Pass(tnt, reqProcessor.Filters, agReq); err != nil { return false, err } else if !pass { return false, nil } - for k, v := range reqProcessor.Flags { // update procVars with flags from processor - procVars[k] = strconv.FormatBool(v) - } if reqProcessor.DryRun { - utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, HTTP request: %s", utils.HTTPAgent, dP)) - utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, process variables: %+v", utils.HTTPAgent, procVars)) + utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, HTTP request: %s", utils.HTTPAgent, agReq)) } /* ev, err := radReqAsCGREvent(req, procVars, reqProcessor.Flags, reqProcessor.RequestFields) diff --git a/agents/libhttpagent.go b/agents/libhttpagent.go index 0d8bc7f63..ca3b98d71 100644 --- a/agents/libhttpagent.go +++ b/agents/libhttpagent.go @@ -25,7 +25,7 @@ import ( "github.com/cgrates/cgrates/engine" ) -// newHAReqDecoder produces decoders +// newHADataProvider constructs a DataProvider func newHADataProvider(dpType string, req *http.Request) (dP engine.DataProvider, err error) { switch dpType { diff --git a/engine/dataprovider.go b/engine/dataprovider.go index b8c58d9b7..58250199d 100644 --- a/engine/dataprovider.go +++ b/engine/dataprovider.go @@ -24,8 +24,8 @@ import ( // DataProvider is a data source from multiple formats type DataProvider interface { + String() string // printable version of data FieldAsInterface(fldPath []string) (interface{}, error) FieldAsString(fldPath []string) (string, error) - String() string // printable versin of data AsNavigableMap([]*config.CfgCdrField) (*NavigableMap, error) } diff --git a/utils/consts.go b/utils/consts.go index c7f026a3b..7dab06a8f 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -543,6 +543,9 @@ const ( MetaUrl = "*url" MetaXml = "*xml" ApiKey = "apikey" + MetaRequest = "*request" + MetaVars = "*vars" + MetaReply = "*reply" ) // Migrator Action diff --git a/utils/slice.go b/utils/slice.go index a0e0d0b57..6d8ebeae7 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -75,3 +75,11 @@ func PrefixSliceItems(slc []string, prfx string) (out []string) { } return } + +// StripSlicePrefix will strip a number of items from the beginning of the slice +func StripSlicePrefix(slc []string, nrItems int) []string { + if len(slc) < nrItems { + return []string{} + } + return slc[nrItems:] +} diff --git a/utils/slice_test.go b/utils/slice_test.go index 5347118e5..bbce3c3e5 100644 --- a/utils/slice_test.go +++ b/utils/slice_test.go @@ -17,7 +17,10 @@ along with this program. If not, see */ package utils -import "testing" +import ( + "reflect" + "testing" +) func TestAvg(t *testing.T) { values := []float64{1, 2, 3} @@ -36,3 +39,18 @@ func TestAvgEmpty(t *testing.T) { t.Errorf("Wrong Avg: expected %v got %v", expected, result) } } + +func TestStripSlicePrefix(t *testing.T) { + eSlc := make([]string, 0) + if retSlc := StripSlicePrefix([]string{}, 2); !reflect.DeepEqual(eSlc, retSlc) { + t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc) + } + eSlc = []string{"1", "2"} + if retSlc := StripSlicePrefix([]string{"0", "1", "2"}, 1); !reflect.DeepEqual(eSlc, retSlc) { + t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc) + } + eSlc = []string{} + if retSlc := StripSlicePrefix([]string{"0", "1", "2"}, 3); !reflect.DeepEqual(eSlc, retSlc) { + t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc) + } +}