2018-09-19 07:07:32 +00:00
|
|
|
package arlo
|
2017-12-10 19:17:09 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-17 04:44:41 +00:00
|
|
|
"github.com/jeffreydwalter/arlo-golang/internal/request"
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-12-10 19:17:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Arlo struct {
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
client *request.Client
|
2018-09-17 04:44:41 +00:00
|
|
|
Account Account
|
|
|
|
Basestations Basestations
|
|
|
|
Cameras Cameras
|
2017-12-10 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
func newArlo(user string, pass string) (arlo *Arlo) {
|
|
|
|
|
2017-12-10 19:17:09 +00:00
|
|
|
c, _ := request.NewClient(BaseUrl)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
// Add important headers.
|
|
|
|
c.BaseHttpHeader.Add("DNT", "1")
|
|
|
|
c.BaseHttpHeader.Add("schemaVersion", "1")
|
|
|
|
c.BaseHttpHeader.Add("Host", "arlo.netgear.com")
|
|
|
|
c.BaseHttpHeader.Add("Referer", "https://arlo.netgear.com/")
|
|
|
|
|
|
|
|
return &Arlo{
|
2017-12-10 19:17:09 +00:00
|
|
|
user: user,
|
|
|
|
pass: pass,
|
|
|
|
client: c,
|
|
|
|
}
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
func Login(user string, pass string) (arlo *Arlo, err error) {
|
|
|
|
arlo = newArlo(user, pass)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
body := map[string]string{"email": arlo.user, "password": arlo.pass}
|
|
|
|
resp, err := arlo.post(LoginUri, "", body, nil)
|
|
|
|
if err := checkHttpRequest(resp, err, "login request failed"); err != nil {
|
|
|
|
return nil, err
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
2018-09-20 22:38:01 +00:00
|
|
|
defer resp.Body.Close()
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
var loginResponse LoginResponse
|
|
|
|
if err := resp.Decode(&loginResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Success {
|
|
|
|
// Cache the auth token.
|
2018-09-20 22:38:01 +00:00
|
|
|
arlo.client.BaseHttpHeader.Add("Authorization", loginResponse.Data.Token)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
// Save the account info with the arlo struct.
|
2018-09-20 22:38:01 +00:00
|
|
|
arlo.Account = loginResponse.Data
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
// Get the devices, which also caches them on the arlo object.
|
2018-09-20 22:38:01 +00:00
|
|
|
if _, err := arlo.GetDevices(); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return nil, errors.WithMessage(err, "failed to login")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("failed to login")
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
return arlo, nil
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) Logout() error {
|
|
|
|
resp, err := a.put(LogoutUri, "", nil, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to logout")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevices returns an array of all devices.
|
|
|
|
// When you call Login, this method is called and all devices are cached in the arlo object.
|
2018-09-20 22:38:01 +00:00
|
|
|
func (a *Arlo) GetDevices() (devices Devices, err error) {
|
2018-09-19 19:12:06 +00:00
|
|
|
resp, err := a.get(DevicesUri, "", nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
if err := checkHttpRequest(resp, err, "failed to get devices"); err != nil {
|
|
|
|
return nil, err
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var deviceResponse DeviceResponse
|
|
|
|
if err := resp.Decode(&deviceResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !deviceResponse.Success {
|
|
|
|
return nil, errors.New("failed to get devices")
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
if len(deviceResponse.Data) == 0 {
|
|
|
|
return nil, errors.New("no devices found")
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
for i := range deviceResponse.Data {
|
|
|
|
deviceResponse.Data[i].arlo = a
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:41:00 +00:00
|
|
|
// Unsubscribe all of the basestations from the EventStream.
|
2018-09-19 19:12:06 +00:00
|
|
|
for i := range a.Basestations {
|
|
|
|
if err := a.Basestations[i].Unsubscribe(); err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get devices")
|
|
|
|
}
|
2018-09-19 21:35:05 +00:00
|
|
|
}
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
// Cache the devices as their respective types.
|
|
|
|
a.Cameras = deviceResponse.Data.GetCameras()
|
|
|
|
a.Basestations = deviceResponse.Data.GetBasestations()
|
|
|
|
|
|
|
|
// Subscribe each basestation to the EventStream.
|
|
|
|
for i := range a.Basestations {
|
2018-09-19 19:12:06 +00:00
|
|
|
if err := a.Basestations[i].Subscribe(); err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get devices")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceResponse.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDisplayOrder sets the display order according to the order defined in the DeviceOrder given.
|
|
|
|
func (a *Arlo) UpdateDisplayOrder(d DeviceOrder) error {
|
|
|
|
resp, err := a.post(DeviceDisplayOrderUri, "", d, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to display order")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProfile takes a first and last name, and updates the user profile with that information.
|
|
|
|
func (a *Arlo) UpdateProfile(firstName, lastName string) error {
|
|
|
|
body := map[string]string{"firstName": firstName, "lastName": lastName}
|
|
|
|
resp, err := a.put(UserProfileUri, "", body, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to update profile")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) UpdatePassword(pass string) error {
|
|
|
|
body := map[string]string{"currentPassword": a.pass, "newPassword": pass}
|
|
|
|
resp, err := a.post(UserChangePasswordUri, "", body, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
if err := checkRequest(resp, err, "failed to update password"); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.pass = pass
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-10 19:17:09 +00:00
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
func (a *Arlo) UpdateFriends(f Friend) error {
|
|
|
|
resp, err := a.put(UserFriendsUri, "", f, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to update friends")
|
2017-12-10 19:17:09 +00:00
|
|
|
}
|