Skip to content

Commit

Permalink
Merge pull request #233 from ctreminiom/feature/confluence-v2-endpoints
Browse files Browse the repository at this point in the history
✨ Mapped more Confluence V2 endpoints
  • Loading branch information
ctreminiom authored Aug 14, 2023
2 parents 2b9d1b5 + 98e787b commit a47adfd
Show file tree
Hide file tree
Showing 16 changed files with 1,699 additions and 18 deletions.
29 changes: 28 additions & 1 deletion confluence/internal/attachment_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
)

// NewAttachmentService returns a new Confluence V2 Page service
func NewAttachmentService(client service.Connector) *AttachmentService {
func NewAttachmentService(client service.Connector, version *AttachmentVersionService) *AttachmentService {
return &AttachmentService{
internalClient: &internalAttachmentImpl{c: client},
Version: version,
}
}

type AttachmentService struct {
internalClient confluence.AttachmentConnector
Version *AttachmentVersionService
}

// Get returns a specific attachment.
Expand Down Expand Up @@ -51,10 +53,35 @@ func (a *AttachmentService) Gets(ctx context.Context, entityID int, entityType s
return a.internalClient.Gets(ctx, entityID, entityType, options, cursor, limit)
}

// Delete deletes an attachment by id.
//
// DELETE /wiki/api/v2/attachments/{id}
//
// https://docs.go-atlassian.io/confluence-cloud/v2/attachments#delete-attachment
func (a *AttachmentService) Delete(ctx context.Context, attachmentID string) (*model.ResponseScheme, error) {
return a.internalClient.Delete(ctx, attachmentID)
}

type internalAttachmentImpl struct {
c service.Connector
}

func (i *internalAttachmentImpl) Delete(ctx context.Context, attachmentID string) (*model.ResponseScheme, error) {

if attachmentID == "" {
return nil, model.ErrNoContentAttachmentIDError
}

endpoint := fmt.Sprintf("wiki/api/v2/attachments/%v", attachmentID)

request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil)
if err != nil {
return nil, err
}

return i.c.Call(request, nil)
}

func (i *internalAttachmentImpl) Get(ctx context.Context, attachmentID string, versionID int, serializeIDs bool) (*model.AttachmentScheme, *model.ResponseScheme, error) {

if attachmentID == "" {
Expand Down
113 changes: 111 additions & 2 deletions confluence/internal/attachment_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Test_internalAttachmentImpl_Get(t *testing.T) {
testCase.on(&testCase.fields)
}

attachmentService := NewAttachmentService(testCase.fields.c)
attachmentService := NewAttachmentService(testCase.fields.c, nil)

gotResult, gotResponse, err := attachmentService.Get(testCase.args.ctx, testCase.args.attachmentID, testCase.args.versionID,
testCase.args.serializeIDs)
Expand Down Expand Up @@ -248,7 +248,7 @@ func Test_internalAttachmentImpl_Gets(t *testing.T) {
testCase.on(&testCase.fields)
}

attachmentService := NewAttachmentService(testCase.fields.c)
attachmentService := NewAttachmentService(testCase.fields.c, nil)

gotResult, gotResponse, err := attachmentService.Gets(testCase.args.ctx, testCase.args.entityID, testCase.args.entityType,
testCase.args.options, testCase.args.cursor, testCase.args.limit)
Expand All @@ -271,3 +271,112 @@ func Test_internalAttachmentImpl_Gets(t *testing.T) {
})
}
}

func Test_internalAttachmentImpl_Delete(t *testing.T) {

type fields struct {
c service.Connector
}

type args struct {
ctx context.Context
attachmentID string
}

testCases := []struct {
name string
fields fields
args args
on func(*fields)
wantErr bool
Err error
}{
{
name: "when the parameters are correct",
args: args{
ctx: context.TODO(),
attachmentID: "att10001",
},
on: func(fields *fields) {

client := mocks.NewConnector(t)

client.On("NewRequest",
context.Background(),
http.MethodDelete,
"wiki/api/v2/attachments/att10001",
"", nil).
Return(&http.Request{}, nil)

client.On("Call",
&http.Request{},
nil).
Return(&model.ResponseScheme{}, nil)

fields.c = client

},
},

{
name: "when the http request cannot be created",
args: args{
ctx: context.TODO(),
attachmentID: "att10001",
},
on: func(fields *fields) {

client := mocks.NewConnector(t)

client.On("NewRequest",
context.Background(),
http.MethodDelete,
"wiki/api/v2/attachments/att10001",
"", nil).
Return(&http.Request{}, errors.New("error, unable to create the http request"))

fields.c = client

},
wantErr: true,
Err: errors.New("error, unable to create the http request"),
},

{
name: "when the attachment id is not provided",
args: args{
ctx: context.TODO(),
},
wantErr: true,
Err: model.ErrNoContentAttachmentIDError,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {

if testCase.on != nil {
testCase.on(&testCase.fields)
}

attachmentService := NewAttachmentService(testCase.fields.c, nil)

gotResponse, err := attachmentService.Delete(testCase.args.ctx, testCase.args.attachmentID)

if testCase.wantErr {

if err != nil {
t.Logf("error returned: %v", err.Error())
}

assert.EqualError(t, err, testCase.Err.Error())

} else {

assert.NoError(t, err)
assert.NotEqual(t, gotResponse, nil)
}

})
}
}
100 changes: 100 additions & 0 deletions confluence/internal/attachment_version_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package internal

import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/confluence"
"net/http"
"net/url"
"strconv"
)

// NewAttachmentVersionService returns a new Confluence V2 Attachment Version service
func NewAttachmentVersionService(client service.Connector) *AttachmentVersionService {
return &AttachmentVersionService{
internalClient: &internalAttachmentVersionImpl{c: client},
}
}

type AttachmentVersionService struct {
internalClient confluence.AttachmentVersionConnector
}

// Gets returns the versions of specific attachment.
//
// GET /wiki/api/v2/attachments/{id}/versions
//
// https://docs.go-atlassian.io/confluence-cloud/v2/attachments/versions#get-attachment-versions
func (a *AttachmentVersionService) Gets(ctx context.Context, attachmentID, cursor, sort string, limit int) (*model.AttachmentVersionPageScheme, *model.ResponseScheme, error) {
return a.internalClient.Gets(ctx, attachmentID, cursor, sort, limit)
}

// Get retrieves version details for the specified attachment and version number.
//
// GET /wiki/api/v2/attachments/{attachment-id}/versions/{version-number}
//
// https://docs.go-atlassian.io/confluence-cloud/v2/attachments/versions#get-attachment-version
func (a *AttachmentVersionService) Get(ctx context.Context, attachmentID string, versionID int) (*model.DetailedVersionScheme, *model.ResponseScheme, error) {
return a.internalClient.Get(ctx, attachmentID, versionID)
}

type internalAttachmentVersionImpl struct {
c service.Connector
}

func (i *internalAttachmentVersionImpl) Gets(ctx context.Context, attachmentID, cursor, sort string, limit int) (*model.AttachmentVersionPageScheme, *model.ResponseScheme, error) {

if attachmentID == "" {
return nil, nil, model.ErrNoContentAttachmentIDError
}

query := url.Values{}
query.Add("limit", strconv.Itoa(limit))

if cursor != "" {
query.Add("cursor", cursor)
}

if sort != "" {
query.Add("sort", sort)
}

endpoint := fmt.Sprintf("wiki/api/v2/attachments/%v/versions?%v", attachmentID, query.Encode())

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return nil, nil, err
}

page := new(model.AttachmentVersionPageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}

return page, response, nil
}

func (i *internalAttachmentVersionImpl) Get(ctx context.Context, attachmentID string, versionID int) (*model.DetailedVersionScheme, *model.ResponseScheme, error) {

if attachmentID == "" {
return nil, nil, model.ErrNoContentAttachmentIDError
}

endpoint := fmt.Sprintf("wiki/api/v2/attachments/%v/versions/%v", attachmentID, versionID)

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return nil, nil, err
}

version := new(model.DetailedVersionScheme)
response, err := i.c.Call(request, version)
if err != nil {
return nil, response, err
}

return version, response, nil
}
Loading

0 comments on commit a47adfd

Please sign in to comment.