-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from coggsflod/pr-to-upstream
Add support for v2 API GetAttachmentById
- Loading branch information
Showing
9 changed files
with
104 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters