From 02654dd7b8bb9512284b39d2bc17b4d39c1c4ae2 Mon Sep 17 00:00:00 2001 From: DanB Date: Sat, 10 Oct 2015 18:27:16 +0200 Subject: [PATCH] Adding sources and initial methods --- README.md | 5 +- kamjsonrpc.go | 115 +++++++++++++++++++++++++++++++++++++++ kamjsonrpc_local_test.go | 48 ++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 kamjsonrpc.go create mode 100644 kamjsonrpc_local_test.go diff --git a/README.md b/README.md index 61b6693..e5b6714 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # kamjsonrpc -JSON-RPC client for Kamailio +Simple JSON-RPC client for Kamailio + + +See local_test file for usage. \ No newline at end of file diff --git a/kamjsonrpc.go b/kamjsonrpc.go new file mode 100644 index 0000000..0c8ff70 --- /dev/null +++ b/kamjsonrpc.go @@ -0,0 +1,115 @@ +/* +Released under MIT License 299 { + return fmt.Errorf("Unexpected status code received: %d", resp.StatusCode) + } + if kamResponse.Id != reqId { + return fmt.Errorf("Unsynchronized request, had: %d, received: %d", reqId, kamResponse.Id) + } + *reply = *kamResponse.Result + return nil +} + +// Add inidividual methods over the generic one +type RegistrationInfo struct { + LocalUuid string `json:"l_uuid"` + LocalUsername string `json:"l_username"` + LocalDomain string `json:"l_domain"` + RemoteUsername string `json:"r_username"` + RemoteDomain string `json:"r_domain"` + Realm string `json:"realm"` + AuthUsername string `json:"auth_username"` + AuthPassword string `json:"auth_password"` + AuthProxy string `json:"auth_proxy"` + Expires int64 `json:"expires"` + Flags int64 `json:"flags"` + DiffExpires int64 `json:"diff_expires"` + TimerExpires int64 `json:"timer_expires"` +} + +func (self *KamailioJsonRpc) UacRegInfo(params []string, reply *RegistrationInfo) error { + var regRaw json.RawMessage + if err := self.Call("uac.reg_info", params, ®Raw); err != nil { + return err + } + return json.Unmarshal(regRaw, reply) +} diff --git a/kamjsonrpc_local_test.go b/kamjsonrpc_local_test.go new file mode 100644 index 0000000..a3d09e3 --- /dev/null +++ b/kamjsonrpc_local_test.go @@ -0,0 +1,48 @@ +package kamjsonrpc + +import ( + "encoding/json" + "flag" + "reflect" + "testing" +) + +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 +var kamAddr = flag.String("kam_addr", "http://127.0.0.1:5060", "Address where to reach kamailio http server") + +var kamRpc *KamailioJsonRpc + +func TestKamJsonRpcConn(t *testing.T) { + if !*testLocal { + return + } + var err error + if kamRpc, err = NewKamailioJsonRpc(*kamAddr, true); err != nil { + t.Fatal("Cannot connect to kamailio:", err) + } +} + +func TestKamJsonRpcCall(t *testing.T) { + if !*testLocal { + return + } + var reply json.RawMessage + if err := kamRpc.Call("uac.reg_info", []string{"l_uuid", "unknown"}, &reply); err != nil { + t.Error(err) + } else if reflect.DeepEqual(reply, json.RawMessage{}) { + t.Error("Empty reply") + } +} + +func TestKamJsonRpcUacRegInfo(t *testing.T) { + if !*testLocal { + return + } + var eReply RegistrationInfo + var reply RegistrationInfo + if err := kamRpc.UacRegInfo([]string{"l_uuid", "unknown"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, eReply) { + t.Errorf("Expecting: %+v, received: %+v", eReply, reply) + } +}