Skip to content

Commit

Permalink
Merge pull request #61 from coggsflod/pr-to-upstream
Browse files Browse the repository at this point in the history
Add support for v2 API GetAttachmentById
  • Loading branch information
c-seeger authored Nov 20, 2024
2 parents d5ab27b + 5f44f36 commit 2facb0a
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 11 deletions.
64 changes: 64 additions & 0 deletions attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package goconfluence

import (
"encoding/json"
"net/http"
"net/url"
"time"
)

type Attachment struct {
MediaTypeDescription string `json:"mediaTypeDescription"`
WebuiLink string `json:"webuiLink"`
DownloadLink string `json:"downloadLink"`
CreatedAt interface{} `json:"createdAt"`
ID string `json:"id"`
Comment string `json:"comment"`
Version struct {
Number int `json:"number"`
Message string `json:"message"`
MinorEdit bool `json:"minorEdit"`
AuthorID string `json:"authorId"`
CreatedAt time.Time `json:"createdAt"`
} `json:"version"`
Title string `json:"title"`
FileSize int `json:"fileSize"`
Status string `json:"status"`
PageID string `json:"pageId"`
FileID string `json:"fileId"`
MediaType string `json:"mediaType"`
Links struct {
Download string `json:"download"`
Webui string `json:"webui"`
} `json:"_links"`
}

// getAttachmentEndpoint creates the correct api endpoint by given id
func (a *API) getAttachmentEndpoint(id string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/attachments/" + id)
}

// Get info on specific attachment by id
func (a *API) GetAttachmentById(id string) (*Attachment, error) {
ep, err := a.getAttachmentEndpoint(id)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", ep.String(), nil)
if err != nil {
return nil, err
}

res, err := a.Request(req)
if err != nil {
return nil, err
}

var attachment Attachment

err = json.Unmarshal(res, &attachment)
if err != nil {
return nil, err
}
return &attachment, nil
}
28 changes: 28 additions & 0 deletions attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package goconfluence

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetAttachmentEndpoint(t *testing.T) {
a, err := NewAPI("https://test.test", "username", "token")
assert.Nil(t, err)

url, err := a.getAttachmentEndpoint("test")
assert.Nil(t, err)
assert.Equal(t, "/attachments/test", url.Path)
}

func TestAttachmentGetter(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()

api, err := NewAPI(server.URL+"/wiki/api/v2", "userame", "token")
assert.Nil(t, err)

c, err := api.GetAttachmentById("2495990589")
assert.Nil(t, err)
assert.Equal(t, &Attachment{}, c)
}
11 changes: 5 additions & 6 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ Simple example:
fmt.Printf("%+v\n", currentUser)
supported features:
- get user information
- create, update, delete content
- get comments, attachments, history, watchers and children of content objects
- get, add, delete labels
- search using CQL
- get user information
- create, update, delete content
- get comments, attachments, history, watchers and children of content objects
- get, add, delete labels
- search using CQL
see https://github.com/virtomize/confluence-go-api/tree/master/examples for more information and usage examples
*/
package goconfluence
2 changes: 1 addition & 1 deletion examples/attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/virtomize/confluence-go-api"
goconfluence "github.com/virtomize/confluence-go-api"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/virtomize/confluence-go-api"
goconfluence "github.com/virtomize/confluence-go-api"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/virtomize/confluence-go-api"
goconfluence "github.com/virtomize/confluence-go-api"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/virtomize/confluence-go-api"
goconfluence "github.com/virtomize/confluence-go-api"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/virtomize/confluence-go-api"
goconfluence "github.com/virtomize/confluence-go-api"
)

func main() {
Expand Down
2 changes: 2 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ func confluenceRestAPIStub() *httptest.Server {
resp = TemplateSearch{}
case "/wiki/rest/api/template/page":
resp = TemplateSearch{}
case "/wiki/api/v2/attachments/2495990589":
resp = Attachment{}
default:
http.Error(w, "not found", http.StatusNotFound)
return
Expand Down

0 comments on commit 2facb0a

Please sign in to comment.