package discourse import ( "encoding/json" "fmt" "strings" ) // UserResponse - Structure of a discourse user api response// UserResponse - Structure of a discourse user api response type UserResponse struct { User User `json:"user"` Errors []string `json:"errors"` ErrorType string `json:"error_type"` } // 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"` 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, error) { url := fmt.Sprintf("%s/users/%s.json", config.Endpoint, username) req, _ := newGetRequest(config, url) client := getClient() response, _ := client.Do(req) var result *UserResponse json.NewDecoder(response.Body).Decode(&result) if result.ErrorType != "" { return User{}, fmt.Errorf("Failed to get discourse user. Error: %s", strings.Join(result.Errors, "; ")) } return result.User, nil } func GetUserByID(config APIConfig, userID int) (User, error) { url := fmt.Sprintf("%s/admin/users/%d.json", config.Endpoint, userID) req, _ := newGetRequest(config, url) client := getClient() response, _ := client.Do(req) var result *UserResponse json.NewDecoder(response.Body).Decode(&result) if result.ErrorType != "" { return User{}, fmt.Errorf("Failed to get discourse user. Error: %s", strings.Join(result.Errors, "; ")) } return result.User, nil }