1
0
mirror of https://github.com/writeas/go-writeas.git synced 2025-07-27 15:18:28 +00:00

Support creating an organization contributor

Ref T734
This commit is contained in:
Matt Baer 2021-10-14 13:47:34 -04:00
parent 91e1f9124d
commit 22e135ac8a
2 changed files with 91 additions and 0 deletions

54
author.go Normal file
View File

@ -0,0 +1,54 @@
package writeas
import (
"fmt"
"net/http"
)
type (
// Author represents a Write.as author.
Author struct {
User *User
Name string `json:"name"`
Slug string `json:"slug"`
}
// AuthorParams are used to create or update a Write.as author.
AuthorParams struct {
// Name is the public display name of the Author.
Name string `json:"name"`
// Slug is the optional slug for the Author.
Slug string `json:"slug"`
// OrgAlias is the alias of the organization the Author belongs to.
OrgAlias string `json:"-"`
}
)
// CreateContributor creates a new contributor on the given organization.
func (c *Client) CreateContributor(sp *AuthorParams) (*Author, error) {
if sp.OrgAlias == "" {
return nil, fmt.Errorf("AuthorParams.OrgAlias is required.")
}
a := &Author{}
env, err := c.post("/organizations/"+sp.OrgAlias+"/contributors", sp, a)
if err != nil {
return nil, err
}
var ok bool
if a, ok = env.Data.(*Author); !ok {
return nil, fmt.Errorf("Wrong data returned from API.")
}
status := env.Code
if status != http.StatusCreated {
if status == http.StatusBadRequest {
return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
}
return nil, fmt.Errorf("Problem creating author: %d. %s\n", status, env.ErrorMessage)
}
return a, nil
}

37
author_test.go Normal file
View File

@ -0,0 +1,37 @@
package writeas
import "testing"
func TestClient_CreateContributor(t *testing.T) {
c := NewClientWith(Config{URL: "http://localhost:7777/api"})
_, err := c.LogIn("test", "test")
if err != nil {
t.Fatalf("login: %s", err)
}
tests := []struct {
name string
AName string
ASlug string
AOrg string
}{
{
name: "good",
AName: "Bob Contrib",
ASlug: "bob",
AOrg: "write-as",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err = c.CreateContributor(&AuthorParams{
Name: test.AName,
Slug: test.ASlug,
OrgAlias: test.AOrg,
})
if err != nil {
t.Fatalf("create %s: %s", test.name, err)
}
})
}
}