2018-09-22 19:22:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jeffrey Walter <jeffreydwalter@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
|
|
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
package arlo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-22 19:22:42 +00:00
|
|
|
"io"
|
2018-09-19 19:12:06 +00:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
"github.com/jeffreydwalter/arlo-go/internal/request"
|
|
|
|
"github.com/jeffreydwalter/arlo-go/internal/util"
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-09-22 19:22:42 +00:00
|
|
|
func checkRequest(resp *request.Response, err error, msg string) error {
|
2018-09-19 19:12:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
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) {
|
2018-09-22 19:22:42 +00:00
|
|
|
a.client.AddHeader("xcloudId", xCloudId)
|
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) {
|
2018-09-22 19:22:42 +00:00
|
|
|
a.client.AddHeader("xcloudId", xCloudId)
|
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) {
|
2018-09-22 19:22:42 +00:00
|
|
|
a.client.AddHeader("xcloudId", xCloudId)
|
|
|
|
return a.client.Post(uri, body, header)
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
/*
|
2018-09-22 19:22:42 +00:00
|
|
|
func (a *Arlo) DownloadFile(url, to string) error {
|
|
|
|
msg := fmt.Sprintf("failed to download file (%s) => (%s)", url, to)
|
|
|
|
resp, err := a.get(url, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
2018-09-22 19:22:42 +00:00
|
|
|
defer resp.Body.Close()
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2018-09-22 19:22:42 +00:00
|
|
|
f, err := os.Create(to)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
2018-12-11 23:48:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
func (a *Arlo) DownloadFile(url string, w io.Writer) error {
|
|
|
|
msg := fmt.Sprintf("failed to download file (%s)", url)
|
2018-12-25 22:31:46 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
2018-12-11 23:48:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
2018-12-25 22:31:46 +00:00
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnixMicro(t time.Time) int64 {
|
|
|
|
ns := t.UnixNano()
|
|
|
|
if ns < 0 {
|
|
|
|
return (ns - 999) / 1000
|
|
|
|
}
|
|
|
|
return ns / 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnixMilli(t time.Time) int64 {
|
|
|
|
ns := t.UnixNano()
|
|
|
|
if ns < 0 {
|
|
|
|
return (ns - 999999) / 1000000
|
|
|
|
}
|
|
|
|
return ns / 1000000
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromUnixMicro(µs int64) time.Time { return time.Unix(0, 1000*µs) }
|
|
|
|
|
|
|
|
func FromUnixMilli(ms int64) time.Time { return time.Unix(0, 1000000*ms) }
|