Go client for the Write.as API https://developers.write.as
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

56 righe
1.3 KiB

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