feat: initial commit
This commit is contained in:
127
reach/client/protocol/v2/internal.go
Normal file
127
reach/client/protocol/v2/internal.go
Normal file
@ -0,0 +1,127 @@
|
||||
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)
|
||||
res, err := http.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)
|
||||
res, err := http.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) 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) 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
|
||||
}
|
Reference in New Issue
Block a user