46 lines
879 B
Go
46 lines
879 B
Go
|
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
|
||
|
}
|