dashboard.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package discourse
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type DashboardResponse struct {
  8. Dashboard Dashboard `json:"dashboard"`
  9. Errors []string `json:"errors"`
  10. ErrorType string `json:"error_type"`
  11. }
  12. type Dashboard struct {
  13. UpdatedAt string `json:"updated_at"`
  14. VersionCheck VersionCheck `json:"version_check"`
  15. }
  16. type VersionCheck struct {
  17. InstalledVersion string `json:"installed_version"`
  18. InstalledSha string `json:"installed_sha"`
  19. InstalledDescribe string `json:"installed_describe"`
  20. GitBranch string `json:"git_branch"`
  21. UpdatedAt string `json:"updated_at"`
  22. LatestVersion string `json:"latest_version"`
  23. CritialUpdates bool `json:"critical_updates"`
  24. MissingVersionsCount int `json:"missing_versions_count"`
  25. StaleData bool `json:"stale_data"`
  26. }
  27. // GetDashboard - Get a discourse admin dashboard
  28. func GetDashboard(config APIConfig) (Dashboard, error) {
  29. url := fmt.Sprintf("%s/admin/dashboard.json", config.Endpoint)
  30. req, _ := newGetRequest(config, url)
  31. client := getClient(rl)
  32. response, err := client.Do(req)
  33. if err != nil {
  34. return Dashboard{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
  35. }
  36. if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
  37. var result *Dashboard
  38. json.NewDecoder(response.Body).Decode(&result)
  39. return *result, nil
  40. }
  41. return Dashboard{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
  42. }
  43. // GetVersionCheck will retrieve the admin version check dat from discourse
  44. func GetVersionCheck(config APIConfig) (VersionCheck, error) {
  45. url := fmt.Sprintf("%s/admin/version_check.json", config.Endpoint)
  46. req, _ := newGetRequest(config, url)
  47. client := getClient(rl)
  48. response, err := client.Do(req)
  49. if err != nil {
  50. return VersionCheck{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
  51. }
  52. if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
  53. var result *VersionCheck
  54. json.NewDecoder(response.Body).Decode(&result)
  55. return *result, nil
  56. }
  57. return VersionCheck{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
  58. }