diff --git a/api.go b/api.go index 216c235..5b9fc4c 100644 --- a/api.go +++ b/api.go @@ -50,6 +50,7 @@ type CacophonyDevice struct { name string password string id int + saltId int } type CacophonyAPI struct { @@ -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 @@ -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 } @@ -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 @@ -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) @@ -347,6 +356,7 @@ type tokenResponse struct { Messages []string Token string ID int + SaltId int } type fileUploadResponse struct { @@ -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) @@ -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) diff --git a/api_test.go b/api_test.go index 31f972f..0a0cfe2 100644 --- a/api_test.go +++ b/api_test.go @@ -23,6 +23,7 @@ import ( "io" "io/ioutil" "log" + "math/rand" "mime/multipart" "net/http" "net/http/httptest" @@ -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{} @@ -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) { @@ -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) @@ -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() @@ -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) @@ -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") } @@ -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)