Add ReachViewVersion() API

This commit is contained in:
wpetit 2018-09-19 15:53:40 +02:00
parent 055ee0000d
commit 1e16779c52
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package reach
import (
"sync"
"forge.cadoles.com/Pyxis/golang-socketio"
"github.com/pkg/errors"
)
const (
eventGetReachViewVersion = "get reachview version"
eventReachViewVersionResults = "current reachview version"
)
type reachViewVersion struct {
Version string `json:"version"`
}
// ReachViewVersion returns the ReachRS module ReachView version
func (c *Client) ReachViewVersion() (string, error) {
var err error
var version string
var wg sync.WaitGroup
wg.Add(1)
err = c.conn.On(eventReachViewVersionResults, func(h *gosocketio.Channel, rv *reachViewVersion) {
version = rv.Version
c.conn.Off(eventReachViewVersionResults)
wg.Done()
})
if err != nil {
return "", errors.Wrapf(err, "error while binding to '%s' event", eventReachViewVersionResults)
}
c.logf("sending '%s' event", eventGetReachViewVersion)
if err = c.conn.Emit(eventGetReachViewVersion, nil); err != nil {
return "", errors.Wrapf(err, "error while emitting '%s' event", eventGetReachViewVersion)
}
c.logf("'%s' event sent", eventGetReachViewVersion)
wg.Wait()
return version, err
}

View File

@ -0,0 +1,35 @@
package reach
import (
"log"
"testing"
)
func TestClientReachViewVersion(t *testing.T) {
if !*runIntegrationTests {
t.Skip("To run this test, use: go test -integration")
}
client := NewClient(
WithStandardLogger(),
WithEndpoint(*reachHost, 80),
)
if err := client.Connect(); err != nil {
t.Fatal(err)
}
version, err := client.ReachViewVersion()
if err != nil {
t.Error(err)
}
if version == "" {
t.Error("version should not be empty")
}
log.Printf("reachview version result: %v", version)
defer client.Close()
}