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) Do(*http.Request) (*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 } func (c WebClient) StopService(name string) error { req, _ := http.NewRequest(http.MethodDelete, c.getBaseUrl()+"/service/"+name+"/", nil) _, e := c.httpClient.Do(req) return e }