Browse Source

Added trust level support and improved error handling

Thisnthat 5 years ago
parent
commit
5332032076
1 changed files with 15 additions and 9 deletions
  1. 15 9
      users.go

+ 15 - 9
users.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"net/http"
+	"strings"
 )
 
 // UserResponse - Structure of a discourse user api response// UserResponse - Structure of a discourse user api response
@@ -15,17 +16,18 @@ type UserResponse struct {
 
 // User - A discoruse User
 type User struct {
-	ID        int     `json:"id" schema:"external_id"`
-	Username  string  `json:"username"`
-	CanSendPM bool    `json:"can_send_private_messages"`
-	Moderator bool    `json:"moderator"`
-	Admin     bool    `json:"admin"`
-	Groups    []Group `json:"groups"`
-	GroupStr  string  `schema:"groups"`
+	ID         int     `json:"id" schema:"external_id"`
+	Username   string  `json:"username"`
+	CanSendPM  bool    `json:"can_send_private_messages"`
+	Moderator  bool    `json:"moderator"`
+	Admin      bool    `json:"admin"`
+	TrustLevel int     `json:"trust_level"`
+	Groups     []Group `json:"groups"`
+	GroupStr   string  `schema:"groups"`
 }
 
 // GetUser - Get a discourse user
-func GetUser(config APIConfig, username string) User {
+func GetUser(config APIConfig, username string) (User, error) {
 	url := fmt.Sprintf("%s/users/%s.json?api_key=%s&api_username=%s", config.Endpoint, username, config.APIKey, config.APIUsername)
 
 	response, _ := http.Get(url)
@@ -33,5 +35,9 @@ func GetUser(config APIConfig, username string) User {
 	var result *UserResponse
 	json.NewDecoder(response.Body).Decode(&result)
 
-	return result.User
+	if result.ErrorType != "" {
+		return User{}, fmt.Errorf("Failed to get discourse user. Error: %s", strings.Join(result.Errors, "; "))
+	}
+
+	return result.User, nil
 }