Add execute for create command

This commit is contained in:
Erik
2023-04-19 16:00:07 +02:00
parent e3e72de1f4
commit b65e42304c
2 changed files with 48 additions and 37 deletions

View File

@ -1,12 +1,26 @@
package pcsdk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/netip"
"github.com/google/uuid"
"github.com/thefeli73/polemos/state"
)
type ExecuteCommand interface {
Execute(string) error
}
type response struct {
message string `json:"message"`
}
type ProxyCommandCreate struct {
Command CommandCreate `json:"create"`
// signature Signature
@ -19,6 +33,36 @@ type CommandCreate struct {
Id string `json:"id"`
}
func (c ProxyCommandCreate) Execute(url string) error {
data, err := json.Marshal(c)
if err != nil {
return errors.New(fmt.Sprintf("could not serialize: %s\n", err))
}
requestURL := fmt.Sprintf("%s/command", url)
bodyReader := bytes.NewReader(data)
res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader)
if err != nil {
return errors.New(fmt.Sprintf("error making http request: %s\n", err))
}
fmt.Println(res)
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
if err != nil {
return errors.New(fmt.Sprintf("error reading response: %s\n", err))
}
if res.StatusCode != 202 {
return errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body))
} else {
return nil
}
}
func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandCreate {
c := CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))}
return ProxyCommandCreate{c}