Skip to content

Commit

Permalink
Merge pull request #51 from kenzo0107/support_get_wiki_attachment_con…
Browse files Browse the repository at this point in the history
…tent_api

Support get-wiki-page-attachment API
  • Loading branch information
kenzo0107 committed May 15, 2023
2 parents 0d31de8 + bb59f91 commit 62d2c8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backlog
import (
"context"
"fmt"
"io"
)

// Wiki : wiki
Expand Down Expand Up @@ -272,6 +273,26 @@ func (c *Client) GetWikiAttachmentsContext(ctx context.Context, wikiID int) ([]*
return attachments, nil
}

// GetWikiAttachmentContent writes the content to writer
func (c *Client) GetWikiAttachmentContent(wikiID, attachmentID int, w io.Writer) error {
return c.GetWikiAttachmentContentContext(context.Background(), wikiID, attachmentID, w)
}

// GetWikiAttachmentContentContext writes the content to writer
func (c *Client) GetWikiAttachmentContentContext(ctx context.Context, wikiID, attachmentID int, w io.Writer) error {
u := fmt.Sprintf("/api/v2/wikis/%v/attachments/%v", wikiID, attachmentID)

req, err := c.NewRequest("GET", u, nil)
if err != nil {
return err
}

if err := c.Do(ctx, req, w); err != nil {
return err
}
return nil
}

// AddAttachmentToWiki adds attachments to a wiki
func (c *Client) AddAttachmentToWiki(wikiID int, input *AddAttachmentToWikiInput) ([]*Attachment, error) {
return c.AddAttachmentToWikiContext(context.Background(), wikiID, input)
Expand Down
27 changes: 27 additions & 0 deletions wiki_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backlog

import (
"bytes"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -557,6 +558,32 @@ func TestGetWikiAttachmentsFailed(t *testing.T) {
}
}

func TestGetWikiAttachmentContent(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

client.httpclient = &mockHTTPClient{}

err := client.GetWikiAttachmentContent(1, 1, &bytes.Buffer{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
}

func TestGetWikiAttachmentContentFailed(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/wikis/1/attachments/2", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})

if err := client.GetWikiAttachmentContent(1, 2, &bytes.Buffer{}); err == nil {
t.Fatal("expected an error but got none")
}
}

func TestAddAttachmentToWiki(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 62d2c8d

Please sign in to comment.