spud/webclient/main.go

46 lines
879 B
Go
Raw Normal View History

2024-10-04 04:03:12 +00:00
package webclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
daemon "spud/daemon"
service_definition "spud/service_definition"
)
type HttpClient interface {
Post(string, string, io.Reader) (*http.Response, error)
}
type WebClient struct {
httpClient HttpClient
Host string
Port int
}
func NewWebClient(host string, port int) *WebClient {
return &WebClient{
httpClient: &http.Client{},
Host: host,
Port: port,
}
}
func (c WebClient) getBaseUrl() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
func (c WebClient) CreateService(def service_definition.ServiceDefinition) error {
payload := daemon.ServiceListPayload{
Definition: def,
}
serializedPayload, _ := json.Marshal(payload)
_, e := c.httpClient.Post(c.getBaseUrl()+"/service/", "application/json", bytes.NewBuffer(serializedPayload))
return e
}