Go client for the Write.as API https://developers.write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type (
  7. // Author represents a Write.as author.
  8. Author struct {
  9. User *User
  10. Name string `json:"name"`
  11. Slug string `json:"slug"`
  12. }
  13. // AuthorParams are used to create or update a Write.as author.
  14. AuthorParams struct {
  15. // Name is the public display name of the Author.
  16. Name string `json:"name"`
  17. // Slug is the optional slug for the Author.
  18. Slug string `json:"slug"`
  19. // OrgAlias is the alias of the organization the Author belongs to.
  20. OrgAlias string `json:"-"`
  21. }
  22. )
  23. // CreateContributor creates a new contributor on the given organization.
  24. func (c *Client) CreateContributor(sp *AuthorParams) (*Author, error) {
  25. if sp.OrgAlias == "" {
  26. return nil, fmt.Errorf("AuthorParams.OrgAlias is required.")
  27. }
  28. a := &Author{}
  29. env, err := c.post("/organizations/"+sp.OrgAlias+"/contributors", sp, a)
  30. if err != nil {
  31. return nil, err
  32. }
  33. var ok bool
  34. if a, ok = env.Data.(*Author); !ok {
  35. return nil, fmt.Errorf("Wrong data returned from API.")
  36. }
  37. status := env.Code
  38. if status != http.StatusCreated {
  39. if status == http.StatusBadRequest {
  40. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  41. }
  42. return nil, fmt.Errorf("Problem creating author: %d. %s\n", status, env.ErrorMessage)
  43. }
  44. return a, nil
  45. }