Skip to content

Commit

Permalink
Merge pull request #28 from CameronRP/add-salt-id
Browse files Browse the repository at this point in the history
Added salt id when device registers
  • Loading branch information
CameronRP authored Jun 16, 2021
2 parents ff62237 + b078279 commit c2e17ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
22 changes: 19 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CacophonyDevice struct {
name string
password string
id int
saltId int
}

type CacophonyAPI struct {
Expand Down Expand Up @@ -138,7 +139,7 @@ func New() (*CacophonyAPI, error) {

// Register will check that there is not already device config files, will then
// register with the given parameters and then save them in new config files.
func Register(devicename string, password string, group string, apiURL string) (*CacophonyAPI, error) {
func Register(devicename, password, group, apiURL string, saltId int) (*CacophonyAPI, error) {
url, err := url.Parse(apiURL)
if err != nil {
return nil, err
Expand All @@ -157,11 +158,15 @@ func Register(devicename string, password string, group string, apiURL string) (
return nil, errors.New("device is already registered")
}

payload, err := json.Marshal(map[string]string{
regData := map[string]interface{}{
"group": group,
"devicename": devicename,
"password": password,
})
}
if saltId != 0 {
regData["saltId"] = saltId
}
payload, err := json.Marshal(regData)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,6 +199,7 @@ func Register(devicename string, password string, group string, apiURL string) (
group: group,
name: devicename,
password: password,
saltId: respData.SaltId,
}
api.token = respData.Token

Expand Down Expand Up @@ -317,6 +323,9 @@ func (api *CacophonyAPI) UploadThermalRaw(r io.Reader, metadata map[string]inter

// Add the file as a new MIME part.
fw, err := w.CreateFormFile("file", "file")
if err != nil {
return 0, err
}
io.Copy(fw, &fileBytes)
w.Close()
req, err := http.NewRequest("POST", joinURL(api.serverURL, apiBasePath, "/recordings"), buf)
Expand Down Expand Up @@ -347,6 +356,7 @@ type tokenResponse struct {
Messages []string
Token string
ID int
SaltId int
}

type fileUploadResponse struct {
Expand Down Expand Up @@ -424,6 +434,9 @@ func (api *CacophonyAPI) GetFileDetails(fileID int) (*FileResponse, error) {
buf := new(bytes.Buffer)

req, err := http.NewRequest("GET", joinURL(api.serverURL, apiBasePath, "/files/"+strconv.Itoa(fileID)), buf)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)

resp, err := api.httpClient.Do(req)
Expand Down Expand Up @@ -524,6 +537,9 @@ func isHTTPClientError(code int) bool {
// GetSchedule will get the audio schedule
func (api *CacophonyAPI) GetSchedule() ([]byte, error) {
req, err := http.NewRequest("GET", joinURL(api.serverURL, apiBasePath, "schedules"), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", api.token)
//client := new(http.Client)

Expand Down
13 changes: 9 additions & 4 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"log"
"math/rand"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -153,7 +154,7 @@ func GetNewAuthenticateServer(t *testing.T) *httptest.Server {

//getMimeParts retrieves data and file:file and Value:data from a multipart request
func getMimeParts(r *http.Request) (map[string]interface{}, string) {
partReader, err := r.MultipartReader()
partReader, _ := r.MultipartReader()

var fileData string
var data map[string]interface{}
Expand Down Expand Up @@ -214,7 +215,7 @@ func TestAPIAuthenticate(t *testing.T) {
}

func randomRegister() (*CacophonyAPI, error) {
return Register(randString(20), randString(20), defaultGroup, apiURL)
return Register(randString(20), randString(20), defaultGroup, apiURL, rand.Int())
}

func TestAPIUploadThermalRaw(t *testing.T) {
Expand All @@ -223,6 +224,7 @@ func TestAPIUploadThermalRaw(t *testing.T) {
require.NoError(t, err)

reader, err := os.Open(testCPTVFile)
assert.NoError(t, err)
defer reader.Close()

id, err := api.UploadThermalRaw(reader, nil)
Expand Down Expand Up @@ -281,12 +283,13 @@ func TestRegisterAndNew(t *testing.T) {

name := randString(10)
password := randString(10)
api1, err := Register(name, password, defaultGroup, apiURL)
api1, err := Register(name, password, defaultGroup, apiURL, 100)
require.NoError(t, err, "failed to register")
assert.Equal(t, api1.device.name, name, "name does not match what was registered with")
assert.Equal(t, api1.device.group, defaultGroup, "group does not match what was registered with")
assert.Equal(t, api1.Password(), password, "password does not match what was registered with")
assert.Equal(t, api1.getHostname(), getHostnameFromFile(t))
assert.Equal(t, 100, api1.device.saltId)
assert.NoError(t, checkHostsFile(api1))

api2, err := New()
Expand All @@ -298,6 +301,7 @@ func TestRegisterAndNew(t *testing.T) {
assert.NoError(t, checkHostsFile(api2))

reader, err := os.Open(testCPTVFile)
assert.NoError(t, err)
defer reader.Close()

id, err := api2.UploadThermalRaw(reader, nil)
Expand All @@ -306,7 +310,7 @@ func TestRegisterAndNew(t *testing.T) {

assert.NoError(t, checkHostsFile(api2))

_, err = Register(name+"a", defaultPassword, defaultGroup, apiURL)
_, err = Register(name+"a", defaultPassword, defaultGroup, apiURL, 0)
assert.Error(t, err, "must not be able to register when the device is already registered")
}

Expand Down Expand Up @@ -419,6 +423,7 @@ func TestDeviceReregister(t *testing.T) {
assert.NoError(t, checkHostsFile(api2))

reader, err := os.Open(testCPTVFile)
assert.NoError(t, err)
defer reader.Close()

id, err := api2.UploadThermalRaw(reader, nil)
Expand Down

0 comments on commit c2e17ae

Please sign in to comment.