Browse Source

Added dashboard api

thisnthat 4 years ago
parent
commit
241e4975fd
1 changed files with 57 additions and 0 deletions
  1. 57 0
      dashboard.go

+ 57 - 0
dashboard.go

@@ -0,0 +1,57 @@
+package discourse
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strings"
+)
+
+type DashboardResponse struct {
+	Dashboard Dashboard `json:"user"`
+	Errors    []string  `json:"errors"`
+	ErrorType string    `json:"error_type"`
+}
+
+type Dashboard struct {
+	UpdatedAt    string       `json:"updated_at"`
+	VersionCheck VersionCheck `json:"version_check"`
+}
+
+type VersionCheck struct {
+	InstalledVersion     string `json:"installed_version"`
+	InstalledSha         string `json:"installed_sha"`
+	InstalledDescribe    string `json:"installed_describe"`
+	GitBranch            string `json:"git_branch"`
+	UpdatedAt            string `json:"updated_at"`
+	LatestVersion        string `json:"latest_version"`
+	CritialUpdates       bool   `json:"critical_updates"`
+	MissingVersionsCount int    `json:"missing_versions_count"`
+	StaleData            bool   `json:"stale_data"`
+}
+
+// GetDashboard - Get a discourse admin dashboard
+func GetDashboard(config APIConfig) (Dashboard, error) {
+	url := fmt.Sprintf("%s/admin/dashboard.json", config.Endpoint)
+
+	req, _ := newGetRequest(config, url)
+	client := getClient()
+	response, err := client.Do(req)
+
+	if err != nil {
+		return Dashboard{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
+	}
+
+	if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
+		var result *DashboardResponse
+		json.NewDecoder(response.Body).Decode(&result)
+
+		if result.ErrorType != "" {
+			return Dashboard{}, fmt.Errorf("Failed to get discourse version. Error: %s", strings.Join(result.Errors, "; "))
+		}
+
+		return result.Dashboard, nil
+	}
+
+	return Dashboard{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
+}