package gateeditor import ( "bytes" "encoding/json" "fmt" "net/http" ) type Client struct { url string client *http.Client } func NewClient(url string) *Client { return &Client{ url: url, client: &http.Client{}, //optional, use for custom timeouts etc } } type Route struct { Host []string `json:"host"` Backend string `json:"backend"` } type apiResponse struct { Routes []Route `json:"routes"` Message string `json:"message"` Error string `json:"error"` } func (c *Client) doRequest(action string, payload interface{}) (*apiResponse, error) { data, err := json.Marshal(map[string]interface{}{ "action": action, "routes": payload, }) if err != nil { return nil, fmt.Errorf("failed marshalling payload: %w", err) } resp, err := c.client.Post(c.url, "application/json", bytes.NewBuffer(data)) if err != nil { return nil, fmt.Errorf("failed to send request: %w", err) } defer resp.Body.Close() var apiResp apiResponse if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil { return nil, fmt.Errorf("failed to decode response: %w", err) } if apiResp.Error != "" { return nil, fmt.Errorf("API error: %s", apiResp.Error) } return &apiResp, nil } func (c *Client) ListRoutes() ([]Route, error) { resp, err := c.doRequest("ListRoutes", nil) if err != nil { return nil, err } return resp.Routes, nil } func (c *Client) UpdateRoutes(routes []Route) error { _, err := c.doRequest("UpdateRoutes", routes) return err }