Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ctx in GetAuth functions #68

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions api/api_init_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -73,7 +74,7 @@ func init() {
}
client := &gosip.SPClient{AuthCnfg: auth}
// Pre-auth for tests not include auth timing involved
if _, _, err := client.AuthCnfg.GetAuth(); err != nil {
if _, _, err := client.AuthCnfg.GetAuth(context.Background()); err != nil {
fmt.Printf("can't auth, %s\n", err)
// Force all test being skipped in case of auth issues
return nil
Expand All @@ -91,7 +92,7 @@ func init() {
}
client := &gosip.SPClient{AuthCnfg: auth}
// Pre-auth for tests not include auth timing involved
if _, _, err := client.AuthCnfg.GetAuth(); err != nil {
if _, _, err := client.AuthCnfg.GetAuth(context.Background()); err != nil {
fmt.Printf("can't auth, %s\n", err)
// Force all test being skipped in case of auth issues
return nil
Expand Down
7 changes: 4 additions & 3 deletions api/associatedGroups_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"testing"
)

Expand All @@ -10,7 +11,7 @@ func TestAssociatedGroups(t *testing.T) {
sp := NewSP(spClient)

t.Run("Visitors", func(t *testing.T) {
group, err := sp.Web().AssociatedGroups().Visitors().Get()
group, err := sp.Web().AssociatedGroups().Visitors().Get(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -20,7 +21,7 @@ func TestAssociatedGroups(t *testing.T) {
})

t.Run("Members", func(t *testing.T) {
group, err := sp.Web().AssociatedGroups().Members().Get()
group, err := sp.Web().AssociatedGroups().Members().Get(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -30,7 +31,7 @@ func TestAssociatedGroups(t *testing.T) {
})

t.Run("Owners", func(t *testing.T) {
group, err := sp.Web().AssociatedGroups().Owners().Get()
group, err := sp.Web().AssociatedGroups().Owners().Get(context.Background())
if err != nil {
t.Error(err)
}
Expand Down
29 changes: 15 additions & 14 deletions api/attachments.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -58,16 +59,16 @@ func NewAttachment(client *gosip.SPClient, endpoint string, config *RequestConfi
}

// Get gets attachments collection response
func (attachments *Attachments) Get() (AttachmentsResp, error) {
func (attachments *Attachments) Get(ctx context.Context) (AttachmentsResp, error) {
client := NewHTTPClient(attachments.client)
return client.Get(attachments.endpoint, attachments.config)
return client.Get(ctx, attachments.endpoint, attachments.config)
}

// Add uploads new attachment to the item
func (attachments *Attachments) Add(name string, content io.Reader) (AttachmentResp, error) {
func (attachments *Attachments) Add(ctx context.Context, name string, content io.Reader) (AttachmentResp, error) {
client := NewHTTPClient(attachments.client)
endpoint := fmt.Sprintf("%s/Add(FileName='%s')", attachments.endpoint, name)
return client.Post(endpoint, content, attachments.config)
return client.Post(ctx, endpoint, content, attachments.config)
}

// GetByName gets an attachment by its name
Expand All @@ -80,31 +81,31 @@ func (attachments *Attachments) GetByName(fileName string) *Attachment {
}

// Get gets attachment data object
func (attachment *Attachment) Get() (AttachmentResp, error) {
func (attachment *Attachment) Get(ctx context.Context) (AttachmentResp, error) {
client := NewHTTPClient(attachment.client)
return client.Get(attachment.endpoint, attachment.config)
return client.Get(ctx, attachment.endpoint, attachment.config)
}

// Delete delete an attachment skipping recycle bin
func (attachment *Attachment) Delete() error {
func (attachment *Attachment) Delete(ctx context.Context) error {
client := NewHTTPClient(attachment.client)
_, err := client.Delete(attachment.endpoint, attachment.config)
_, err := client.Delete(ctx, attachment.endpoint, attachment.config)
return err
}

// Recycle moves an attachment to the recycle bin
func (attachment *Attachment) Recycle() error {
func (attachment *Attachment) Recycle(ctx context.Context) error {
client := NewHTTPClient(attachment.client)
endpoint := fmt.Sprintf("%s/RecycleObject", attachment.endpoint)
_, err := client.Post(endpoint, nil, attachment.config)
_, err := client.Post(ctx, endpoint, nil, attachment.config)
return err
}

// GetReader gets attachment body data reader
func (attachment *Attachment) GetReader() (io.ReadCloser, error) {
func (attachment *Attachment) GetReader(ctx context.Context) (io.ReadCloser, error) {
endpoint := fmt.Sprintf("%s/$value", attachment.endpoint)

req, err := http.NewRequest("GET", endpoint, nil)
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, err
}
Expand All @@ -127,8 +128,8 @@ func (attachment *Attachment) GetReader() (io.ReadCloser, error) {
}

// Download downloads attachment's as byte array
func (attachment *Attachment) Download() ([]byte, error) {
body, err := attachment.GetReader()
func (attachment *Attachment) Download(ctx context.Context) ([]byte, error) {
body, err := attachment.GetReader(ctx)
if err != nil {
return nil, err
}
Expand Down
19 changes: 10 additions & 9 deletions api/attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"context"
"testing"

"github.com/google/uuid"
Expand All @@ -13,11 +14,11 @@ func TestAttachments(t *testing.T) {
web := NewSP(spClient).Web()
listTitle := uuid.New().String()

if _, err := web.Lists().Add(listTitle, nil); err != nil {
if _, err := web.Lists().Add(context.Background(), listTitle, nil); err != nil {
t.Error(err)
}
list := web.Lists().GetByTitle(listTitle)
item, err := list.Items().Add([]byte(`{"Title":"Attachment test"}`))
item, err := list.Items().Add(context.Background(), []byte(`{"Title":"Attachment test"}`))
if err != nil {
t.Error(err)
}
Expand All @@ -30,14 +31,14 @@ func TestAttachments(t *testing.T) {
"att_04.txt": []byte("attachment 04"),
}
for fileName, content := range attachments {
if _, err := list.Items().GetByID(item.Data().ID).Attachments().Add(fileName, bytes.NewBuffer(content)); err != nil {
if _, err := list.Items().GetByID(item.Data().ID).Attachments().Add(context.Background(), fileName, bytes.NewBuffer(content)); err != nil {
t.Error(err)
}
}
})

t.Run("Get", func(t *testing.T) {
data, err := list.Items().GetByID(item.Data().ID).Attachments().Get()
data, err := list.Items().GetByID(item.Data().ID).Attachments().Get(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -50,7 +51,7 @@ func TestAttachments(t *testing.T) {
})

t.Run("GetByName", func(t *testing.T) {
data, err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_01.txt").Get()
data, err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_01.txt").Get(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -63,7 +64,7 @@ func TestAttachments(t *testing.T) {
})

t.Run("Delete", func(t *testing.T) {
if err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_02.txt").Delete(); err != nil {
if err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_02.txt").Delete(context.Background()); err != nil {
t.Error(err)
}
})
Expand All @@ -73,14 +74,14 @@ func TestAttachments(t *testing.T) {
t.Skip("is not supported with SP 2013")
}

if err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_03.txt").Recycle(); err != nil {
if err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_03.txt").Recycle(context.Background()); err != nil {
t.Error(err)
}
})

t.Run("Download", func(t *testing.T) {
expectedContent := []byte("attachment 04")
content, err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_04.txt").Download()
content, err := list.Items().GetByID(item.Data().ID).Attachments().GetByName("att_04.txt").Download(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -89,7 +90,7 @@ func TestAttachments(t *testing.T) {
}
})

if err := list.Delete(); err != nil {
if err := list.Delete(context.Background()); err != nil {
t.Error(err)
}

Expand Down
11 changes: 6 additions & 5 deletions api/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -89,10 +90,10 @@ func NewChanges(client *gosip.SPClient, endpoint string, config *RequestConfig)
}

// GetCurrentToken gets current change token for this parent entity
func (changes *Changes) GetCurrentToken() (string, error) {
func (changes *Changes) GetCurrentToken(ctx context.Context) (string, error) {
endpoint := fmt.Sprintf("%s?$select=CurrentChangeToken", changes.endpoint)
client := NewHTTPClient(changes.client)
data, err := client.Get(endpoint, changes.config)
data, err := client.Get(ctx, endpoint, changes.config)
if err != nil {
return "", err
}
Expand All @@ -107,7 +108,7 @@ func (changes *Changes) GetCurrentToken() (string, error) {
}

// GetChanges gets changes in scope of the parent container using provided change query
func (changes *Changes) GetChanges(changeQuery *ChangeQuery) (*ChangesResp, error) {
func (changes *Changes) GetChanges(ctx context.Context, changeQuery *ChangeQuery) (*ChangesResp, error) {
endpoint := toURL(fmt.Sprintf("%s/GetChanges", changes.endpoint), changes.modifiers)
client := NewHTTPClient(changes.client)
metadata := map[string]interface{}{}
Expand All @@ -132,7 +133,7 @@ func (changes *Changes) GetChanges(changeQuery *ChangeQuery) (*ChangesResp, erro
if err != nil {
return nil, err
}
data, err := client.Post(endpoint, bytes.NewBuffer(body), changes.config)
data, err := client.Post(ctx, endpoint, bytes.NewBuffer(body), changes.config)
if err != nil {
return nil, err
}
Expand All @@ -159,7 +160,7 @@ func (changes *Changes) GetChanges(changeQuery *ChangeQuery) (*ChangesResp, erro
return nil, fmt.Errorf("can't get next page of an empty collection")
}
changeQuery.ChangeTokenStart = result.data[len(result.data)-1].ChangeToken.StringValue
return changes.GetChanges(changeQuery)
return changes.GetChanges(ctx, changeQuery)
}

return result, nil
Expand Down
Loading