Browse Source

Added the ability to get discourse about version

thisnthat 4 years ago
parent
commit
b4b59eb2b6
1 changed files with 37 additions and 0 deletions
  1. 37 0
      about.go

+ 37 - 0
about.go

@@ -0,0 +1,37 @@
+package discourse
+
+import (
+	"encoding/json"
+	"fmt"
+	"strings"
+)
+
+// AboutResponse - Structure of a discourse user api response
+type AboutResponse struct {
+	About     About    `json:"about"`
+	Errors    []string `json:"errors"`
+	ErrorType string   `json:"error_type"`
+}
+
+// About data for the site
+type About struct {
+	Version string `json:"version"`
+}
+
+// GetAbout - Get a discourse site about
+func GetAbout(config APIConfig) (About, error) {
+	url := fmt.Sprintf("%s/about.json", config.Endpoint)
+
+	req, _ := newGetRequest(config, url)
+	client := getClient()
+	response, _ := client.Do(req)
+
+	var result *AboutResponse
+	json.NewDecoder(response.Body).Decode(&result)
+
+	if result.ErrorType != "" {
+		return About{}, fmt.Errorf("Failed to get discourse user. Error: %s", strings.Join(result.Errors, "; "))
+	}
+
+	return result.About, nil
+}