From c063bc2a21f123113ddc80e53277ab281979df3b Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 28 May 2014 19:33:47 +0200 Subject: [PATCH] Adding utils.HttpJsonPost client --- apier/cdrs.go | 8 ++++---- cdre/cdrexporter.go | 8 ++++---- engine/action.go | 22 ++++++++------------ local_test.sh | 5 ++++- utils/apitpdata.go | 8 ++++---- utils/coreutils.go | 29 +++++++++++++++++++++++---- utils/utils_local_test.go | 42 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 utils/utils_local_test.go diff --git a/apier/cdrs.go b/apier/cdrs.go index 12672d5f1..4676869b2 100644 --- a/apier/cdrs.go +++ b/apier/cdrs.go @@ -1,14 +1,14 @@ /* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH -This program is free software: you can redistribute it and/or modify +This program is free software: you can Storagetribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of +but WITH*out ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. diff --git a/cdre/cdrexporter.go b/cdre/cdrexporter.go index 1c4c0aee9..0f6c9c416 100644 --- a/cdre/cdrexporter.go +++ b/cdre/cdrexporter.go @@ -1,14 +1,14 @@ /* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH -This program is free software: you can redistribute it and/or modify +This program is free software: you can Storagetribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of +but WITH*out ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. diff --git a/engine/action.go b/engine/action.go index 32ca17de4..ce2ed100c 100644 --- a/engine/action.go +++ b/engine/action.go @@ -1,14 +1,14 @@ /* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH -This program is free software: you can redistribute it and/or modify +This program is free software: you can Storagetribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of +but WITH*out ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -19,11 +19,9 @@ along with this program. If not, see package engine import ( - "bytes" "encoding/json" "errors" "fmt" - "net/http" "net/smtp" "sort" "strings" @@ -215,26 +213,22 @@ func genericReset(ub *Account) error { } func callUrl(ub *Account, a *Action) error { - body, err := json.Marshal(ub) - if err != nil { - return err - } - _, err = http.Post(a.ExtraParameters, "application/json", bytes.NewBuffer(body)) + _, err := utils.HttpJsonPost(a.ExtraParameters, ub) return err } // Does not block for posts, no error reports func callUrlAsync(ub *Account, a *Action) error { - body, err := json.Marshal(ub) + ubJson, err := json.Marshal(ub) if err != nil { return err } go func() { for i := 0; i < 5; i++ { // Loop so we can increase the success rate on best effort - if _, err = http.Post(a.ExtraParameters, "application/json", bytes.NewBuffer(body)); err == nil { + if _, err = utils.HttpJsonPost(a.ExtraParameters, ub); err == nil { break // Success, no need to reinterate } else if i == 4 { // Last iteration, syslog the warning - Logger.Warning(fmt.Sprintf(" WARNING: Failed calling url: [%s], error: [%s], balance: %s", a.ExtraParameters, err.Error(), body)) + Logger.Warning(fmt.Sprintf(" WARNING: Failed calling url: [%s], error: [%s], balance: %s", a.ExtraParameters, err.Error(), ubJson)) break } time.Sleep(time.Duration(i) * time.Minute) diff --git a/local_test.sh b/local_test.sh index 5fb181031..fbfea7c13 100755 --- a/local_test.sh +++ b/local_test.sh @@ -12,10 +12,13 @@ go test github.com/cgrates/cgrates/mediator -local med=$? go test github.com/cgrates/cgrates/config -local cfg=$? +go test github.com/cgrates/cgrates/utils -local +utl=$? -exit $gen && $ap && $en && $cdrc && $med && $cfg + +exit $gen && $ap && $en && $cdrc && $med && $cfg && $utl diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 9e9108bbb..be639e5df 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1,14 +1,14 @@ /* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH -This program is free software: you can redistribute it and/or modify +This program is free software: you can Storagetribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of +but WITH*out ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. diff --git a/utils/coreutils.go b/utils/coreutils.go index 41668ef17..a3981c289 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -1,14 +1,14 @@ /* -Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH -This program is free software: you can redistribute it and/or modify +This program is free software: you can Storagetribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of +but WITH*out ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -19,12 +19,16 @@ along with this program. If not, see package utils import ( + "bytes" "crypto/rand" "crypto/sha1" "encoding/hex" + "encoding/json" "errors" "fmt" + "io/ioutil" "math" + "net/http" "regexp" "strconv" "strings" @@ -255,3 +259,20 @@ func RatingSubjectAliasKey(tenant, subject string) string { func AccountAliasKey(tenant, account string) string { return ConcatenatedKey(tenant, account) } + +func HttpJsonPost(url string, content interface{}) ([]byte, error) { + body, err := json.Marshal(content) + if err != nil { + return nil, err + } + resp, err := http.Post(url, "application/json", bytes.NewBuffer(body)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return respBody, nil +} diff --git a/utils/utils_local_test.go b/utils/utils_local_test.go new file mode 100644 index 000000000..cd2801a34 --- /dev/null +++ b/utils/utils_local_test.go @@ -0,0 +1,42 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH + +This program is free software: you can Storagetribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITH*out ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package utils + +import ( + "flag" + "testing" + "time" +) + +var testLocal = flag.Bool("local", false, "Perform the tests only on local test environment, not by default.") // This flag will be passed here via "go test -local" args + +// Sample HttpJsonPost, more for usage purposes +func TestHttpJsonPost(t *testing.T) { + if !*testLocal { + return + } + cdrOut := &CgrCdrOut{CgrId: Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC).String()), OrderId: 123, TOR: VOICE, AccId: "dsafdsaf", CdrHost: "192.168.1.1", + CdrSource: UNIT_TEST, ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), MediationRunId: DEFAULT_RUNID, + Usage: 0.00000001, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, + } + if _, err := HttpJsonPost("http://localhost:8000", cdrOut); err == nil || err.Error() != "Post http://localhost:8000: dial tcp 127.0.0.1:8000: connection refused" { + t.Error(err) + } +}