Skip to content

Commit

Permalink
Merge pull request #2 from atselvan/feature/sync
Browse files Browse the repository at this point in the history
Feature/sync
  • Loading branch information
atselvan authored Aug 20, 2022
2 parents abe3a8e + 8ba0eb1 commit 3310e39
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ if restErr != nil {
log.Fatal(restErr)
}
```

### Sync local data to Anki Cloud
```go
client := ankiconnect.NewClient()

restErr := client.Sync.Trigger()
if restErr != nil {
log.Fatal(restErr)
}
```
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type (
// supported interfaces
Decks DecksManager
Notes NotesManager
Sync SyncManager
}

// RequestPayload represents the request payload for anki connect api.
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewClient() *Client {

c.Decks = &decksManager{Client: c}
c.Notes = &notesManager{Client: c}
c.Sync = &syncManager{Client: c}

return c
}
Expand Down Expand Up @@ -94,6 +96,13 @@ func (c *Client) SetNotesManager(nm NotesManager) *Client {
return c
}

// SetSyncManager can be used to set a custom SyncManager interface.
// This function is added for testing the SyncManager interface.
func (c *Client) SetSyncManager(sm SyncManager) *Client {
c.Sync = sm
return c
}

// request formats and returns a base http request that can be extended later.
// as part of this the baseUrl and the default headers are set in the http client.
func (c *Client) request() *resty.Request {
Expand Down
6 changes: 6 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func TestClient_SetNotesManager(t *testing.T) {
assert.Exactly(t, nm, c.Notes)
}

func TestClient_SetSyncManager(t *testing.T) {
sm := &syncManager{}
c := NewClient().SetSyncManager(sm)
assert.Exactly(t, sm, c.Sync)
}

func TestClient_Ping(t *testing.T) {
t.Run("success", func(t *testing.T) {
c := NewClient()
Expand Down
31 changes: 31 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ankiconnect

import "github.com/privatesquare/bkst-go-utils/utils/errors"

const (
ActionSync = "sync"
)

type (
// SyncManager describes the interface that can be used to perform sync operations on Anki.
SyncManager interface {
Trigger() *errors.RestErr
}

// syncManager implements SyncManager
syncManager struct{
Client *Client
}
)

// Trigger syncs local Anki data to Anki web.
// The method returns an error if:
// - the api request to ankiconnect fails.
// - the api returns a http error.
func (sm *syncManager) Trigger() *errors.RestErr {
_, restErr := post[string, ParamsDefault](sm.Client, ActionSync, nil)
if restErr != nil {
return restErr
}
return nil
}
42 changes: 42 additions & 0 deletions sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ankiconnect

import (
"net/http"
"testing"

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

func TestSyncManager_Trigger(t *testing.T) {
t.Run("success", func(t *testing.T) {
httpmock.ActivateNonDefault(client.httpClient.GetClient())
defer httpmock.DeactivateAndReset()

result := new(Result[string])
responder, err := httpmock.NewJsonResponder(http.StatusOK, result)
assert.NoError(t, err)

httpmock.RegisterResponder(http.MethodPost, ankiConnectUrl, responder)

restErr := client.Sync.Trigger()
assert.Nil(t, restErr)
})

t.Run("error", func(t *testing.T) {
httpmock.ActivateNonDefault(client.httpClient.GetClient())
defer httpmock.DeactivateAndReset()

result := new(Result[string])
loadTestData(t, testDataPath+errorTestDataFileName, result)
responder, err := httpmock.NewJsonResponder(http.StatusOK, result)
assert.NoError(t, err)

httpmock.RegisterResponder(http.MethodPost, ankiConnectUrl, responder)

restErr := client.Sync.Trigger()
assert.NotNil(t, restErr)
assert.Equal(t, http.StatusBadRequest, restErr.StatusCode)
assert.Equal(t, "some error message", restErr.Message)
})
}

0 comments on commit 3310e39

Please sign in to comment.