Add Execute interface to pcsdk commands

This commit is contained in:
Erik 2023-04-25 12:55:00 +02:00
parent d00d03b0cc
commit a7ae121750
4 changed files with 73 additions and 79 deletions

View File

@ -5,7 +5,7 @@ import (
"net/netip" "net/netip"
"github.com/google/uuid" "github.com/google/uuid"
pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/pcsdk"
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
@ -13,7 +13,7 @@ func main() {
ip := netip.MustParseAddr("127.0.0.1") ip := netip.MustParseAddr("127.0.0.1")
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) m := pcsdk.NewCommandCreate(5555, 8080, ip, state.CustomUUID(uuid))
err := m.Execute("http://localhost:3000") err := m.Execute("http://localhost:3000")
if err != nil { if err != nil {
fmt.Printf("error executing create command: %s\n", err) fmt.Printf("error executing create command: %s\n", err)

View File

@ -1,15 +1,10 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"os"
"github.com/google/uuid" "github.com/google/uuid"
pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/pcsdk"
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
@ -17,38 +12,10 @@ func main() {
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandDelete(state.CustomUUID(uuid)) m := pcsdk.NewCommandDelete(state.CustomUUID(uuid))
data, err := json.Marshal(m) err := m.Execute("http://localhost:3000")
if err != nil { if err != nil {
fmt.Printf("client: could not serialize into JSON") fmt.Printf("error executing modify command: %s\n", err)
os.Exit(1) } else {
fmt.Println("executing modify command completed")
} }
fmt.Printf(string(data))
requestURL := "http://localhost:3000/command"
bodyReader := bytes.NewReader(data)
req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
if err != nil {
fmt.Printf("client: could not create request: %s\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
fmt.Println(req)
res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader)
if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
os.Exit(1)
}
fmt.Printf("client: got response!\n")
fmt.Printf("client: status code: %d\n", res.StatusCode)
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("client: could not read response body: %s\n", err)
os.Exit(1)
}
fmt.Printf("client: response body: %s\n", resBody)
} }

View File

@ -1,16 +1,11 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"net/netip" "net/netip"
"os"
"github.com/google/uuid" "github.com/google/uuid"
pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/pcsdk"
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
@ -18,39 +13,11 @@ func main() {
ip := netip.MustParseAddr("127.0.0.1") ip := netip.MustParseAddr("127.0.0.1")
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandModify(8888, ip, state.CustomUUID(uuid)) m := pcsdk.NewCommandModify(9111, ip, state.CustomUUID(uuid))
data, err := json.Marshal(m) err := m.Execute("http://localhost:3000")
if err != nil { if err != nil {
fmt.Printf("client: could not serialize into JSON") fmt.Printf("error executing modify command: %s\n", err)
os.Exit(1) } else {
fmt.Println("executing modify command completed")
} }
fmt.Printf(string(data))
requestURL := "http://localhost:3000/command"
bodyReader := bytes.NewReader(data)
req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
if err != nil {
fmt.Printf("client: could not create request: %s\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
fmt.Println(req)
res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader)
if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
os.Exit(1)
}
fmt.Printf("client: got response!\n")
fmt.Printf("client: status code: %d\n", res.StatusCode)
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("client: could not read response body: %s\n", err)
os.Exit(1)
}
fmt.Printf("client: response body: %s\n", resBody)
} }

View File

@ -78,6 +78,36 @@ type CommandModify struct {
Id string `json:"id"` Id string `json:"id"`
} }
func (c ProxyCommandModify) 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 NewCommandModify(oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandModify { func NewCommandModify(oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandModify {
c := CommandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))} c := CommandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))}
return ProxyCommandModify{c} return ProxyCommandModify{c}
@ -91,6 +121,36 @@ type CommandDelete struct {
Id string `json:"id"` Id string `json:"id"`
} }
func (c ProxyCommandDelete) 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 NewCommandDelete(id state.CustomUUID) ProxyCommandDelete { func NewCommandDelete(id state.CustomUUID) ProxyCommandDelete {
c := CommandDelete{uuid.UUID.String(uuid.UUID(id))} c := CommandDelete{uuid.UUID.String(uuid.UUID(id))}
return ProxyCommandDelete{c} return ProxyCommandDelete{c}