Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions miro/pictures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import (
"strings"
)

type pictureType string

const (
boards pictureType = "boards"
teams = "teams"
users = "users"
)

const (
picturesPath = "pictures"
picturesPath = "picture"
)

// PicturesService handles communication to Miro Pictures API.
Expand All @@ -24,6 +32,7 @@ type PicturesService service
//go:generate gomodifytags -file $GOFILE -struct Picture -clear-tags -w
//go:generate gomodifytags --file $GOFILE --struct Picture -add-tags json -w -transform camelcase
type Picture struct {
Type string `json:"type"`
ID string `json:"id"`
ImageURL string `json:"imageURL"`
}
Expand All @@ -32,15 +41,28 @@ type Picture struct {
//go:generate gomodifytags -file $GOFILE -struct MiniPicture -clear-tags -w
//go:generate gomodifytags --file $GOFILE --struct MiniPicture -add-tags json -w -transform camelcase
type MiniPicture struct {
Type string `json:"type"`
ID string `json:"id"`
ImageURL string `json:"imageURL"`
}

func validatePictureType(pictureType pictureType) error {
switch pictureType {
case boards, teams, users:
return nil
}
return fmt.Errorf("\"%s\" is not a recognised pictureType", pictureType)
}

// Get gets picture by Picture ID.
//
// API doc: https://developers.miro.com/reference#get-user
func (s *PicturesService) Get(ctx context.Context, id string) (*Picture, error) {
req, err := s.client.NewGetRequest(fmt.Sprintf("type/%s/%s", id, picturesPath))
func (s *PicturesService) Get(ctx context.Context, pictureType pictureType, id string) (*Picture, error) {
err := validatePictureType(pictureType)
if err != nil {
return nil, err
}
req, err := s.client.NewGetRequest(fmt.Sprintf("%s/%s/%s", pictureType, id, picturesPath))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,8 +95,12 @@ type UpsertPictureRequest struct {
// Upsert upserts a picture by Picture ID.
//
// API doc: https://developers.miro.com/reference#create-or-update-picture
func (s *PicturesService) Upsert(ctx context.Context, id string, request *UpsertPictureRequest) (*Picture, error) {
req, err := s.client.NewPostRequest(fmt.Sprintf("type/%s/%s", id, picturesPath), request)
func (s *PicturesService) Upsert(ctx context.Context, pictureType pictureType, id string, request *UpsertPictureRequest) (*Picture, error) {
err := validatePictureType(pictureType)
if err != nil {
return nil, err
}
req, err := s.client.NewPostRequest(fmt.Sprintf("%s/%s/%s", pictureType, id, picturesPath), request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -102,8 +128,12 @@ func (s *PicturesService) Upsert(ctx context.Context, id string, request *Upsert
// Delete deletes picture by Picture ID.
//
// API doc: https://developers.miro.com/reference#delete-picture
func (s *PicturesService) Delete(ctx context.Context, id string) error {
req, err := s.client.NewDeleteRequest(fmt.Sprintf("type/%s/%s", id, picturesPath))
func (s *PicturesService) Delete(ctx context.Context, pictureType pictureType, id string) error {
err := validatePictureType(pictureType)
if err != nil {
return err
}
req, err := s.client.NewDeleteRequest(fmt.Sprintf("%s/%s/%s", pictureType, id, picturesPath))
if err != nil {
return err
}
Expand Down
37 changes: 24 additions & 13 deletions miro/pictures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const (
testPictureImageURL = "test-image-url"
)

func getPictureJSON(id string) string {
func getPictureJSON(pictureType string, id string) string {
return fmt.Sprintf(`{
"type": "%s",
"id": "%s",
"imageUrl": "%s"
}`, id, testPictureImageURL)
}`, pictureType, id, testPictureImageURL)
}

func getPicture(id string) *Picture {
Expand All @@ -33,19 +34,22 @@ func TestPicturesService_Get(t *testing.T) {
defer teardown()

tcs := map[string]struct {
pictureType string
id string
want *Picture
}{
"ok": {"1", getPicture("1")},
"boards": {"boards", "1", getPicture("1")},
"teams": {"teams", "1", getPicture("1")},
"users": {"users", "1", getPicture("1")},
}

for n, tc := range tcs {
t.Run(n, func(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/type/%s/%s", tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf(getPictureJSON(tc.id)))
mux.HandleFunc(fmt.Sprintf("/%s/%s/%s", tc.pictureType, tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf(getPictureJSON(tc.pictureType, tc.id)))
})

got, err := client.Picture.Get(context.Background(), tc.id)
got, err := client.Picture.Get(context.Background(), pictureType(tc.pictureType), tc.id)
if err != nil {
t.Fatalf("Failed: %v", err)
}
Expand All @@ -62,20 +66,23 @@ func TestPicturesService_Upsert(t *testing.T) {
defer teardown()

tcs := map[string]struct {
pictureType string
id string
Image *bytes.Buffer
want *Picture
}{
"ok": {"1", bytes.NewBuffer(make([]byte, 0, 10)), getPicture("1")},
"boards": {"boards", "1", bytes.NewBuffer(make([]byte, 0, 10)), getPicture("1")},
"teams": {"teams", "1", bytes.NewBuffer(make([]byte, 0, 10)), getPicture("1")},
"users": {"users", "1", bytes.NewBuffer(make([]byte, 0, 10)), getPicture("1")},
}

for n, tc := range tcs {
t.Run(n, func(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/type/%s/%s", tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf(getPictureJSON(tc.id)))
mux.HandleFunc(fmt.Sprintf("/%s/%s/%s", tc.pictureType, tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf(getPictureJSON(tc.pictureType, tc.id)))
})

got, err := client.Picture.Upsert(context.Background(), tc.id, &UpsertPictureRequest{
got, err := client.Picture.Upsert(context.Background(), pictureType(tc.pictureType), tc.id, &UpsertPictureRequest{
tc.Image,
})
if err != nil {
Expand All @@ -94,20 +101,24 @@ func TestPicturesService_Delete(t *testing.T) {
defer teardown()

tcs := map[string]struct {
pictureType string
id string
want *Picture
}{
"ok": {"1", getPicture("1")},
"boards": {"boards", "1", getPicture("1")},
"teams": {"teams", "1", getPicture("1")},
"users": {"users", "1", getPicture("1")},
}


for n, tc := range tcs {
t.Run(n, func(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/type/%s/%s", tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/%s/%s/%s", tc.pictureType, tc.id, picturesPath), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("{}"))
})

err := client.Picture.Delete(context.Background(), tc.id)
err := client.Picture.Delete(context.Background(), pictureType(tc.pictureType), tc.id)
if err != nil {
t.Fatalf("Failed: %v", err)
}
Expand Down