|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + dashboardsPath = "%s/api/v3/dashboards" |
| 11 | + dashboardPath = "%s/api/v3/dashboards/%d" |
| 12 | +) |
| 13 | + |
| 14 | +type DashboardInterface interface { |
| 15 | + GetDashboard(ctx context.Context, ID int) (*Dashboard, error) |
| 16 | + CreateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) |
| 17 | + UpdateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) |
| 18 | + DeleteDashboard(ctx context.Context, ID int) error |
| 19 | +} |
| 20 | + |
| 21 | +func (client *Client) GetDashboard(ctx context.Context, ID int) (*Dashboard, error) { |
| 22 | + response, err := client.requester.Request(ctx, http.MethodGet, client.getDashboardURL(ID), nil) |
| 23 | + if err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + defer response.Body.Close() |
| 27 | + |
| 28 | + if response.StatusCode != http.StatusOK { |
| 29 | + return nil, client.ErrorFromResponse(response) |
| 30 | + } |
| 31 | + |
| 32 | + wrapper, err := Unmarshal[*dashboardWrapper](response.Body) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + return wrapper.Dashboard, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (client *Client) CreateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) { |
| 41 | + payload, err := Marshal(dashboardWrapper{Dashboard: dashboard}) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + response, err := client.requester.Request(ctx, http.MethodPost, client.getDashboardsURL(), payload) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + defer response.Body.Close() |
| 51 | + |
| 52 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 53 | + return nil, client.ErrorFromResponse(response) |
| 54 | + } |
| 55 | + |
| 56 | + wrapper, err := Unmarshal[*dashboardWrapper](response.Body) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return wrapper.Dashboard, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (client *Client) UpdateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) { |
| 65 | + payload, err := Marshal(dashboardWrapper{Dashboard: dashboard}) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + response, err := client.requester.Request(ctx, http.MethodPut, client.getDashboardURL(dashboard.ID), payload) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + defer response.Body.Close() |
| 75 | + |
| 76 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 77 | + return nil, client.ErrorFromResponse(response) |
| 78 | + } |
| 79 | + |
| 80 | + wrapper, err := Unmarshal[*dashboardWrapper](response.Body) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + return wrapper.Dashboard, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (client *Client) DeleteDashboard(ctx context.Context, ID int) error { |
| 89 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.getDashboardURL(ID), nil) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + defer response.Body.Close() |
| 94 | + |
| 95 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound { |
| 96 | + return client.ErrorFromResponse(response) |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func (client *Client) getDashboardsURL() string { |
| 103 | + return fmt.Sprintf(dashboardsPath, client.config.url) |
| 104 | +} |
| 105 | + |
| 106 | +func (client *Client) getDashboardURL(id int) string { |
| 107 | + return fmt.Sprintf(dashboardPath, client.config.url, id) |
| 108 | +} |
0 commit comments