2018-09-19 19:12:06 +00:00
|
|
|
package arlo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jeffreydwalter/arlo-golang/internal/request"
|
|
|
|
"github.com/jeffreydwalter/arlo-golang/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
func checkHttpRequest(resp *request.Response, err error, msg string) error {
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errors.WithMessage(errors.New(fmt.Sprintf("http request failed: %s (%d)", resp.Status, resp.StatusCode)), msg)
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
2018-09-20 22:38:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRequest(resp *request.Response, err error, msg string) error {
|
2018-09-19 19:12:06 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
if err := checkHttpRequest(resp, err, msg); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
var status Status
|
|
|
|
if err := resp.Decode(&status); err != nil {
|
|
|
|
return err
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if status.Success == false {
|
|
|
|
return errors.WithMessage(errors.New(status.Reason), msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genTransId() string {
|
|
|
|
|
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
|
|
|
random := rand.New(source)
|
|
|
|
|
|
|
|
e := random.Float64() * math.Pow(2, 32)
|
|
|
|
|
|
|
|
ms := time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s!%s!%s", TransIdPrefix, strings.ToLower(util.FloatToHex(e)), strconv.Itoa(int(ms)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) get(uri, xCloudId string, header http.Header) (*request.Response, error) {
|
|
|
|
if len(xCloudId) > 0 {
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Lock()
|
2018-09-19 19:12:06 +00:00
|
|
|
a.client.BaseHttpHeader.Set("xcloudId", xCloudId)
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Unlock()
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.client.Get(uri, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) put(uri, xCloudId string, body interface{}, header http.Header) (*request.Response, error) {
|
|
|
|
if len(xCloudId) > 0 {
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Lock()
|
2018-09-19 19:12:06 +00:00
|
|
|
a.client.BaseHttpHeader.Set("xcloudId", xCloudId)
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Unlock()
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.client.Put(uri, body, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) post(uri, xCloudId string, body interface{}, header http.Header) (*request.Response, error) {
|
|
|
|
if len(xCloudId) > 0 {
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Lock()
|
2018-09-19 19:12:06 +00:00
|
|
|
a.client.BaseHttpHeader.Set("xcloudId", xCloudId)
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Unlock()
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.client.Post(uri, body, header)
|
|
|
|
}
|