52 lines
1 KiB
Go
52 lines
1 KiB
Go
package webclient
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
daemon "spud/daemon"
|
|
service_definition "spud/service_definition"
|
|
)
|
|
|
|
type HttpClient interface {
|
|
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)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, c.getBaseUrl()+"/service/", bytes.NewBuffer(serializedPayload))
|
|
_, e := c.httpClient.Do(req)
|
|
|
|
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
|
|
}
|