mirror of
https://github.com/Xevion/vastly.git
synced 2025-12-06 01:16:50 -06:00
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// InstanceGetResponse represents the response from GET /instances
|
|
type InstanceGetResponse struct {
|
|
Instances []Instance `json:"instances"`
|
|
}
|
|
|
|
// GetInstances retrieves all instances for the authenticated user
|
|
func (c *Client) GetInstances() (*InstanceGetResponse, error) {
|
|
resp, err := c.makeRequest(http.MethodGet, "/instances", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result InstanceGetResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// DeleteInstance removes an instance by ID
|
|
func (c *Client) DeleteInstance(id int) error {
|
|
path := fmt.Sprintf("/instances/%d", id)
|
|
resp, err := c.makeRequest(http.MethodDelete, path, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp.Body.Close()
|
|
return nil
|
|
}
|
|
|
|
// PutInstance updates an instance's status
|
|
func (c *Client) PutInstance(id int, status string) error {
|
|
path := fmt.Sprintf("/instances/%d", id)
|
|
data := map[string]string{"status": status}
|
|
|
|
resp, err := c.makeRequest(http.MethodPut, path, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp.Body.Close()
|
|
return nil
|
|
}
|