155 lines
3.2 KiB
Go
155 lines
3.2 KiB
Go
package v2
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v2/model"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (o *Operations) getURL(path string) string {
|
|
return fmt.Sprintf("http://%s%s", o.addr, path)
|
|
}
|
|
|
|
func (o *Operations) GetJSON(path string, dst any) error {
|
|
var res *http.Response
|
|
|
|
url := o.getURL(path)
|
|
client := o.getHTTPClient()
|
|
|
|
res, err := client.Get(url)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
|
|
return errors.Errorf("unexpected http status code %d (%s)", res.StatusCode, res.Status)
|
|
}
|
|
|
|
defer func() {
|
|
if err := res.Body.Close(); err != nil {
|
|
errors.WithStack(err)
|
|
}
|
|
}()
|
|
|
|
var body []byte
|
|
|
|
body, err = io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if err = json.Unmarshal(body, dst); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *Operations) PostJSON(path string, data any, dst any) error {
|
|
|
|
var res *http.Response
|
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
if err := encoder.Encode(data); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
url := o.getURL(path)
|
|
client := o.getHTTPClient()
|
|
|
|
res, err := client.Post(url, "application/json", &buf)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
|
|
return errors.Errorf("unexpected http status code %d (%s)", res.StatusCode, res.Status)
|
|
}
|
|
|
|
defer func() {
|
|
if err := res.Body.Close(); err != nil {
|
|
errors.WithStack(err)
|
|
}
|
|
}()
|
|
|
|
var body []byte
|
|
|
|
body, err = io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if err = json.Unmarshal(body, dst); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *Operations) getHTTPClient() *http.Client {
|
|
o.getClientOnce.Do(func() {
|
|
o.httpClient = &http.Client{
|
|
Transport: &http.Transport{
|
|
Dial: o.dial,
|
|
},
|
|
}
|
|
})
|
|
|
|
return o.httpClient
|
|
}
|
|
|
|
func (o *Operations) PostBaseCoordinates(ctx context.Context, base *model.Base) (*model.Base, error) {
|
|
var updated model.Base
|
|
|
|
if err := o.PostJSON("/configuration/base_mode/base_coordinates", base, &updated); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return &updated, nil
|
|
}
|
|
|
|
func (o *Operations) PostDevice(ctx context.Context, device *model.ConfigurationDevice) (*model.ConfigurationDevice, error) {
|
|
var updated model.ConfigurationDevice
|
|
|
|
if err := o.PostJSON("/configuration/device", device, &updated); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return &updated, nil
|
|
}
|
|
|
|
func (o *Operations) GetUpdater(ctx context.Context) (*model.Updater, error) {
|
|
updater := &model.Updater{}
|
|
if err := o.GetJSON("/updater", updater); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return updater, nil
|
|
}
|
|
|
|
func (o *Operations) GetInfo(ctx context.Context) (*model.Info, error) {
|
|
info := &model.Info{}
|
|
if err := o.GetJSON("/info", info); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return info, nil
|
|
}
|
|
|
|
func (o *Operations) GetConfiguration(ctx context.Context) (*model.Configuration, error) {
|
|
config := &model.Configuration{}
|
|
if err := o.GetJSON("/configuration", config); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return config, nil
|
|
}
|