From 789953e5a070ef40d524a7f59e4e52810f77ea65 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Mon, 12 Apr 2021 10:27:04 +0300 Subject: [PATCH] Removed error from HTTPPoster constructor --- actions/export.go | 7 ++----- ees/httpjsonmap.go | 2 +- ees/httppost.go | 2 +- engine/libcdre.go | 7 ++----- engine/pstr_http.go | 7 ++----- engine/z_poster_it_test.go | 11 +++-------- 6 files changed, 11 insertions(+), 25 deletions(-) diff --git a/actions/export.go b/actions/export.go index c8c17b881..a4d4d28b2 100644 --- a/actions/export.go +++ b/actions/export.go @@ -52,11 +52,8 @@ func (aL *actHTTPPost) execute(_ context.Context, data utils.MapStorage, _ strin } var partExec bool for _, actD := range aL.cfg().Diktats { - var pstr *engine.HTTPPoster - if pstr, err = engine.NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, actD.Path, - utils.ContentJSON, aL.config.GeneralCfg().PosterAttempts); err != nil { - return - } + pstr := engine.NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, actD.Path, + utils.ContentJSON, aL.config.GeneralCfg().PosterAttempts) if async, has := aL.cfg().Opts[utils.MetaAsync]; has && utils.IfaceAsString(async) == utils.TrueStr { go aL.post(pstr, body, actD.Path) } else if err = aL.post(pstr, body, actD.Path); err != nil { diff --git a/ees/httpjsonmap.go b/ees/httpjsonmap.go index 79916b198..e667e7d3c 100644 --- a/ees/httpjsonmap.go +++ b/ees/httpjsonmap.go @@ -39,7 +39,7 @@ func NewHTTPjsonMapEE(cgrCfg *config.CGRConfig, cfgIdx int, filterS *engine.Filt dc: dc, } - pstrJSON.pstr, err = engine.NewHTTPPoster(cgrCfg.GeneralCfg().ReplyTimeout, + pstrJSON.pstr = engine.NewHTTPPoster(cgrCfg.GeneralCfg().ReplyTimeout, cgrCfg.EEsCfg().Exporters[cfgIdx].ExportPath, utils.PosterTransportContentTypes[cgrCfg.EEsCfg().Exporters[cfgIdx].Type], cgrCfg.EEsCfg().Exporters[cfgIdx].Attempts) diff --git a/ees/httppost.go b/ees/httppost.go index 87af99174..aeff46de9 100644 --- a/ees/httppost.go +++ b/ees/httppost.go @@ -33,7 +33,7 @@ func NewHTTPPostEe(cgrCfg *config.CGRConfig, cfgIdx int, filterS *engine.FilterS dc utils.MapStorage) (httpPost *HTTPPost, err error) { httpPost = &HTTPPost{id: cgrCfg.EEsCfg().Exporters[cfgIdx].ID, cgrCfg: cgrCfg, cfgIdx: cfgIdx, filterS: filterS, dc: dc} - httpPost.httpPoster, err = engine.NewHTTPPoster(cgrCfg.GeneralCfg().ReplyTimeout, + httpPost.httpPoster = engine.NewHTTPPoster(cgrCfg.GeneralCfg().ReplyTimeout, cgrCfg.EEsCfg().Exporters[cfgIdx].ExportPath, utils.PosterTransportContentTypes[cgrCfg.EEsCfg().Exporters[cfgIdx].Type], cgrCfg.EEsCfg().Exporters[cfgIdx].Attempts) diff --git a/engine/libcdre.go b/engine/libcdre.go index fea7034b3..017582333 100644 --- a/engine/libcdre.go +++ b/engine/libcdre.go @@ -154,13 +154,10 @@ func (expEv *ExportEvents) ReplayFailedPosts(attempts int) (failedEvents *Export keyFunc := func() string { return utils.EmptyString } switch expEv.Format { case utils.MetaHTTPjsonCDR, utils.MetaHTTPjsonMap, utils.MetaHTTPjson, utils.MetaHTTPPost: - var pstr *HTTPPoster - pstr, err = NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, expEv.Path, + pstr := NewHTTPPoster(config.CgrConfig().GeneralCfg().ReplyTimeout, expEv.Path, utils.PosterTransportContentTypes[expEv.Format], config.CgrConfig().GeneralCfg().PosterAttempts) - if err != nil { - return expEv, err - } + for _, ev := range expEv.Events { req := ev.(*HTTPPosterRequest) err = pstr.PostValues(req.Body, req.Header) diff --git a/engine/pstr_http.go b/engine/pstr_http.go index 831d7a889..9fc7d71e7 100644 --- a/engine/pstr_http.go +++ b/engine/pstr_http.go @@ -55,16 +55,13 @@ func HTTPPostJSON(url string, content []byte) (respBody []byte, err error) { // NewHTTPPoster return a new HTTP poster func NewHTTPPoster(replyTimeout time.Duration, addr, contentType string, - attempts int) (httposter *HTTPPoster, err error) { - if !utils.SliceHasMember([]string{utils.ContentForm, utils.ContentJSON, utils.ContentText}, contentType) { - return nil, fmt.Errorf("unsupported ContentType: %s", contentType) - } + attempts int) (httposter *HTTPPoster) { return &HTTPPoster{ httpClient: &http.Client{Transport: httpPstrTransport, Timeout: replyTimeout}, addr: addr, contentType: contentType, attempts: attempts, - }, nil + } } // HTTPPoster used to post cdrs diff --git a/engine/z_poster_it_test.go b/engine/z_poster_it_test.go index 2c4382cc8..57248edea 100644 --- a/engine/z_poster_it_test.go +++ b/engine/z_poster_it_test.go @@ -68,10 +68,8 @@ func TestHttpJsonPoster(t *testing.T) { config.CgrConfig().GeneralCfg().FailedPostsDir = "/tmp" content := &TestContent{Var1: "Val1", Var2: "Val2"} jsn, _ := json.Marshal(content) - pstr, err := NewHTTPPoster(2*time.Second, "http://localhost:8080/invalid", utils.ContentJSON, 3) - if err != nil { - t.Error(err) - } + pstr := NewHTTPPoster(2*time.Second, "http://localhost:8080/invalid", utils.ContentJSON, 3) + if err = pstr.PostValues(jsn, make(http.Header)); err == nil { t.Error("Expected error") } @@ -101,10 +99,7 @@ func TestHttpBytesPoster(t *testing.T) { content := []byte(`Test Test2 `) - pstr, err := NewHTTPPoster(2*time.Second, "http://localhost:8080/invalid", utils.ContentText, 3) - if err != nil { - t.Error(err) - } + pstr := NewHTTPPoster(2*time.Second, "http://localhost:8080/invalid", utils.ContentText, 3) if err = pstr.PostValues(content, make(http.Header)); err == nil { t.Error("Expected error") }