users.go 1011 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package discourse
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. // UserResponse - Structure of a discourse user api response// UserResponse - Structure of a discourse user api response
  8. type UserResponse struct {
  9. User User `json:"user"`
  10. Errors []string `json:"errors"`
  11. ErrorType string `json:"error_type"`
  12. }
  13. // User - A discoruse User
  14. type User struct {
  15. ID int `json:"id" schema:"external_id"`
  16. Username string `json:"username"`
  17. CanSendPM bool `json:"can_send_private_messages"`
  18. Moderator bool `json:"moderator"`
  19. Admin bool `json:"admin"`
  20. Groups []Group `json:"groups"`
  21. GroupStr string `schema:"groups"`
  22. }
  23. // GetUser - Get a discourse user
  24. func GetUser(config APIConfig, username string) User {
  25. url := fmt.Sprintf("%s/users/%s.json?api_key=%s&api_username=%s", config.Endpoint, username, config.APIKey, config.APIUsername)
  26. response, _ := http.Get(url)
  27. var result *UserResponse
  28. json.NewDecoder(response.Body).Decode(&result)
  29. return result.User
  30. }