|
| 1 | +package statuspal |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// MetricType defines the possible types for metrics (Uptime or Response Time). |
| 11 | +type MetricType string |
| 12 | + |
| 13 | +const ( |
| 14 | + MetricTypeUptime MetricType = "up" |
| 15 | + MetricTypeResponseTime MetricType = "rt" |
| 16 | +) |
| 17 | + |
| 18 | +// FeaturedNumber defines the possible types for the featured number. |
| 19 | +type FeaturedNumber string |
| 20 | + |
| 21 | +const ( |
| 22 | + FeaturedNumberAvg FeaturedNumber = "avg" |
| 23 | + FeaturedNumberMax FeaturedNumber = "max" |
| 24 | + FeaturedNumberLast FeaturedNumber = "last" |
| 25 | +) |
| 26 | + |
| 27 | +// Metric represents a metric on the status page. |
| 28 | +type Metric struct { |
| 29 | + ID int64 `json:"id,omitempty"` |
| 30 | + Status string `json:"status,omitempty"` |
| 31 | + LatestEntryTime int64 `json:"latest_entry_time,omitempty"` |
| 32 | + Order int64 `json:"order,omitempty"` |
| 33 | + Title string `json:"title"` |
| 34 | + Unit string `json:"unit"` |
| 35 | + Type MetricType `json:"type"` |
| 36 | + Enabled bool `json:"enabled,omitempty"` |
| 37 | + Visible bool `json:"visible,omitempty"` |
| 38 | + RemoteID string `json:"remote_id,omitempty"` |
| 39 | + RemoteName string `json:"remote_name,omitempty"` |
| 40 | + Threshold int64 `json:"threshold,omitempty"` |
| 41 | + FeaturedNumber FeaturedNumber `json:"featured_number,omitempty"` |
| 42 | + IntegrationID string `json:"integration_id,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +type MetricBody struct { |
| 46 | + Metric Metric `json:"metric"` |
| 47 | +} |
| 48 | + |
| 49 | +// GetMetric retrieves a single metric by ID. |
| 50 | +func (c *Client) GetMetric(id int64, subdomain string) (*Metric, error) { |
| 51 | + req, err := http.NewRequest("GET", fmt.Sprintf("%s/status_pages/%s/metrics/%d", c.HostURL, subdomain, id), nil) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + body, err := c.doRequest(req) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + var response MetricBody |
| 62 | + err = json.Unmarshal(*body, &response) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + return &response.Metric, nil |
| 68 | +} |
| 69 | + |
| 70 | +// CreateMetric creates a new metric for the status page. |
| 71 | +func (c *Client) CreateMetric(subdomain string, metric *Metric) (*Metric, error) { |
| 72 | + rb, err := json.Marshal(MetricBody{ |
| 73 | + Metric: *metric, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + req, err := http.NewRequest("POST", fmt.Sprintf("%s/status_pages/%s/metrics", c.HostURL, subdomain), strings.NewReader(string(rb))) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + body, err := c.doRequest(req) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + var response MetricBody |
| 90 | + err = json.Unmarshal(*body, &response) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + return &response.Metric, nil |
| 96 | +} |
| 97 | + |
| 98 | +// UpdateMetric updates an existing metric on the status page. |
| 99 | +func (c *Client) UpdateMetric(id int64, subdomain string, metric *Metric) (*Metric, error) { |
| 100 | + rb, err := json.Marshal(MetricBody{ |
| 101 | + Metric: *metric, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + req, err := http.NewRequest("PUT", fmt.Sprintf("%s/status_pages/%s/metrics/%d", c.HostURL, subdomain, id), strings.NewReader(string(rb))) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + body, err := c.doRequest(req) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + var response MetricBody |
| 118 | + err = json.Unmarshal(*body, &response) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return &response.Metric, nil |
| 124 | +} |
| 125 | + |
| 126 | +// DeleteMetric deletes a metric from the status page. |
| 127 | +func (c *Client) DeleteMetric(id int64, subdomain string) error { |
| 128 | + req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/status_pages/%s/metrics/%d", c.HostURL, subdomain, id), nil) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := c.doRequest(req); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments