about.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package discourse
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. )
  8. // AboutResponse - Structure of a discourse user api response
  9. type AboutResponse struct {
  10. About About `json:"about"`
  11. Errors []string `json:"errors"`
  12. ErrorType string `json:"error_type"`
  13. }
  14. // About data for the site
  15. type About struct {
  16. Version string `json:"version"`
  17. }
  18. // GetAbout - Get a discourse site about
  19. func GetAbout(config APIConfig) (About, error) {
  20. url := fmt.Sprintf("%s/about.json", config.Endpoint)
  21. req, _ := newGetRequest(config, url)
  22. client := getClient(rl)
  23. response, err := client.Do(req)
  24. if err != nil {
  25. return About{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
  26. }
  27. if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
  28. var result *AboutResponse
  29. json.NewDecoder(response.Body).Decode(&result)
  30. if result.ErrorType != "" {
  31. return About{}, fmt.Errorf("Failed to get discourse version. Error: %s", strings.Join(result.Errors, "; "))
  32. }
  33. return result.About, nil
  34. }
  35. return About{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
  36. }