Skip to content

Commit

Permalink
change Open config to be in api.go under NewAPIFromConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaza committed Mar 17, 2019
1 parent bfa5472 commit b4e7bfd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
29 changes: 28 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,34 @@ func (api *CacophonyAPI) JustRegistered() bool {
return api.justRegistered
}

// NewAPI creates a CacophonyAPI instance and obtains a fresh JSON Web
// NewAPIFromConfig prases the supplied configFile and creates a new CacophonyAPI with the configFile information
// and saves the generated password to privConfigFileName(configFile)
func NewAPIFromConfig(configFile string) (*CacophonyAPI, error) {
conf, err := ParseConfigFile(configFile)
if err != nil {
return nil, fmt.Errorf("configuration error: %v", err)
}
privConfigFilename := privConfigFilename(configFile)
password, err := ReadPassword(privConfigFilename)
if err != nil {
return nil, err
}

api, err := NewAPI(conf.ServerURL, conf.Group, conf.DeviceName, password)
if err != nil {
return nil, err
}

if api.JustRegistered() {
err := WritePassword(privConfigFilename, api.Password())
if err != nil {
return nil, err
}
}
return api, nil
}

// createAPI creates a CacophonyAPI instance and obtains a fresh JSON Web
// Token. If no password is given then the device is registered.
func NewAPI(serverURL, group, deviceName, password string) (*CacophonyAPI, error) {

Expand Down
7 changes: 3 additions & 4 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ import (
"github.com/stretchr/testify/require"
)

var apiURL = "http://localhost:1080"
var tokenSuccess = true
var responseHeader = http.StatusOK
var message string
var rawThermalData = "this is the raw thermal file"
var apiURL = "http://localhost:1080"
var rawThermalData = randString(100)
var defaultDevice = "test-device"
var defaultPassword = "test-password"
var defaultGroup = "test-group"
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestUploadThermalRawHttpRequest(t *testing.T) {
func getTokenResponse() *tokenResponse {
return &tokenResponse{
Success: tokenSuccess,
Messages: []string{message},
Messages: []string{},
Token: "tok-" + randString(20),
}
}
Expand Down
27 changes: 0 additions & 27 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,6 @@ func WritePassword(filename, password string) error {
return err
}

// Open the configFile and creates a new CacophonyAPI with the configFile information
// and saves the generated password to privConfigFileName(configFile)
func Open(configFile string) (*CacophonyAPI, error) {
conf, err := ParseConfigFile(configFile)
if err != nil {
return nil, fmt.Errorf("configuration error: %v", err)
}
privConfigFilename := privConfigFilename(configFile)
password, err := ReadPassword(privConfigFilename)
if err != nil {
return nil, err
}

api, err := NewAPI(conf.ServerURL, conf.Group, conf.DeviceName, password)
if err != nil {
return nil, err
}

if api.JustRegistered() {
err := WritePassword(privConfigFilename, api.Password())
if err != nil {
return nil, err
}
}
return api, nil
}

// privCOnfigFileName take a configFile and creates an associated
// file to store the password in with suffix -priv.yaml
func privConfigFilename(configFile string) string {
Expand Down

0 comments on commit b4e7bfd

Please sign in to comment.