This commit is contained in:
2025-11-01 20:53:38 +01:00
parent aa8851aca0
commit 6e4452ddb9
12 changed files with 457 additions and 106 deletions

View File

@@ -28,7 +28,7 @@ func New(url string) *Client {
}
func (c *Client) Apply(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "projects", pr.Name)
url, err := url.JoinPath(c.url, "api", "v1", "projects")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
@@ -60,6 +60,31 @@ func (c *Client) Apply(pr project.Project) error {
return nil
}
func (c *Client) Version() (obj.SystemInformation, error) {
url, err := url.JoinPath(c.url, "api", "v1", "version")
if err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to make url: %s", err)
}
res, err := http.Get(url)
if err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return obj.SystemInformation{}, fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
var payload obj.HTTPObject[obj.SystemInformation]
d := json.NewDecoder(res.Body)
if err := d.Decode(&payload); err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to parse the server response, is your client up-to-date? (reason: %s)", err)
}
return payload.Data, nil
}
func (c *Client) List() ([]project.Project, error) {
url, err := url.JoinPath(c.url, "api", "v1", "projects", "all")
if err != nil {
@@ -92,6 +117,70 @@ func (c *Client) List() ([]project.Project, error) {
return prs, nil
}
func (c *Client) Remove(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "projects")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
data, err := json.Marshal(pr)
if err != nil {
return fmt.Errorf("failed to marshal project data: %s", err)
}
r := bytes.NewReader(data)
req, err := http.NewRequest("DELETE", url, r)
if err != nil {
return fmt.Errorf("failed to make request: %s", err)
}
cli := http.Client{}
res, err := cli.Do(req)
if err != nil {
return fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 204 {
return fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
return nil
}
func (c *Client) RunOne(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "run")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
data, err := json.Marshal(pr)
if err != nil {
return fmt.Errorf("failed to marshal project data: %s", err)
}
r := bytes.NewReader(data)
req, err := http.NewRequest("EXECUTE", url, r)
if err != nil {
return fmt.Errorf("failed to make request: %s", err)
}
cli := http.Client{}
res, err := cli.Do(req)
if err != nil {
return fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
return nil
}
func toError(body io.ReadCloser) error {
var msg SimpleError