add_balance

This commit is contained in:
Radu Ioan Fericean
2014-04-17 16:59:17 +03:00
parent 5f4580947a
commit 2da2294c0f
4 changed files with 99 additions and 6 deletions

View File

@@ -135,9 +135,8 @@ func main() {
}
if f, err := os.Create(history_fn); err != nil {
fmt.Println("Error writing history file: ", err)
log.Print("Error writing history file: ", err)
} else {
log.Print("Writing history: ", history_fn)
line.WriteHistory(f)
f.Close()
}

View File

@@ -24,7 +24,6 @@ import (
"reflect"
"github.com/cgrates/cgrates/apier"
"github.com/cgrates/cgrates/engine"
)
func init() {
@@ -41,7 +40,7 @@ type CmdAddAccount struct {
}
func (self *CmdAddAccount) Usage() string {
jsn, _ := json.Marshal(engine.CallDescriptor{Direction: "*out"})
jsn, _ := json.Marshal(apier.AttrSetAccount{Direction: "*out"})
return "\n\tUsage: add_account " + FromJSON(jsn, self.ClientArgs()) + "\n"
}
@@ -76,7 +75,7 @@ func (self *CmdAddAccount) RpcResult() interface{} {
}
func (self *CmdAddAccount) ClientArgs() (args []string) {
val := reflect.ValueOf(apier.AttrSetAccount{}).Elem()
val := reflect.ValueOf(&apier.AttrSetAccount{}).Elem()
for i := 0; i < val.NumField(); i++ {
typeField := val.Type().Field(i)

87
console/add_balance.go Normal file
View File

@@ -0,0 +1,87 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute 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
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 <http://www.gnu.org/licenses/>
*/
package console
import (
"encoding/json"
"fmt"
"reflect"
"github.com/cgrates/cgrates/apier"
"github.com/cgrates/cgrates/engine"
)
func init() {
commands["add_balance"] = &CmdAddBalance{
rpcMethod: "ApierV1.AddBalance",
rpcParams: &apier.AttrAddBalance{BalanceType: engine.CREDIT},
}
}
// Commander implementation
type CmdAddBalance struct {
rpcMethod string
rpcParams *apier.AttrAddBalance
rpcResult string
}
func (self *CmdAddBalance) Usage() string {
jsn, _ := json.Marshal(apier.AttrAddBalance{Direction: "*out"})
return "\n\tUsage: add_balance " + FromJSON(jsn, self.ClientArgs()) + "\n"
}
// Parses command line args and builds CmdBalance value
func (self *CmdAddBalance) FromArgs(args string, verbose bool) error {
if len(args) == 0 {
return fmt.Errorf(self.Usage())
}
// defaults
self.rpcParams = &apier.AttrAddBalance{Direction: "*out"}
if err := json.Unmarshal(ToJSON(args), &self.rpcParams); err != nil {
return err
}
if verbose {
jsn, _ := json.Marshal(self.rpcParams)
fmt.Println("add_balance ", FromJSON(jsn, self.ClientArgs()))
}
return nil
}
func (self *CmdAddBalance) RpcMethod() string {
return self.rpcMethod
}
func (self *CmdAddBalance) RpcParams() interface{} {
return self.rpcParams
}
func (self *CmdAddBalance) RpcResult() interface{} {
return &self.rpcResult
}
func (self *CmdAddBalance) ClientArgs() (args []string) {
val := reflect.ValueOf(&apier.AttrAddBalance{}).Elem()
for i := 0; i < val.NumField(); i++ {
typeField := val.Type().Field(i)
args = append(args, typeField.Name)
}
return
}

View File

@@ -47,9 +47,17 @@ func TestToJSONEmpty(t *testing.T) {
}
func TestFromJSON(t *testing.T) {
line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`))
line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`), []string{"TimeStart", "Test", "Crazy", "Mama", "Test"})
expected := `TimeStart="Test" Crazy=1 Mama=true Test=1`
if line != expected {
t.Errorf("Expected: %s got: '%s'", expected, line)
}
}
func TestFromJSONInterestingFields(t *testing.T) {
line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`), []string{"TimeStart", "Test"})
expected := `TimeStart="Test" Test=1`
if line != expected {
t.Errorf("Expected: %s got: '%s'", expected, line)
}
}