Skip to content

Commit

Permalink
API: wip: collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain committed Dec 15, 2020
1 parent 6373ce9 commit c5e5c3a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 8 deletions.
47 changes: 41 additions & 6 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package etebase

import (
"log"

"github.com/etesync/etebase-go/internal/codec"
"github.com/etesync/etebase-go/internal/crypto"
)
Expand Down Expand Up @@ -187,23 +185,60 @@ func (acc *Account) Logout() error {
}

// Collection is not implemented yet.
func (acc *Account) Collection() error {
resp, err := acc.client.WithToken(acc.session.Token).Post("/collection/", nil)
func (acc *Account) CreateCollection(col *EncryptedCollection) error {
resp, err := acc.client.WithToken(acc.session.Token).Post("/collection", col)

if err != nil {
return err
}
defer resp.Body.Close()
log.Printf("resp.Status = %+v\n", resp.Status)

var body interface{}
if err := codec.NewDecoder(resp.Body).Decode(&body); err != nil {
return err
}
log.Printf("body = %+v\n", body)

return err
}

func (acc *Account) GetCollection(id string) (*EncryptedCollection, error) {
resp, err := acc.client.WithToken(acc.session.Token).Get("/collection/" + id)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var body EncryptedCollection
if err := codec.NewDecoder(resp.Body).Decode(&body); err != nil {
return nil, err
}

return &body, nil
}

func (acc *Account) ListCollections(types [][]byte) ([]EncryptedCollection, error) {
req := struct {
CollectionTypes [][]byte `msgpack:"collectionTypes"`
}{
types,
}

resp, err := acc.client.WithToken(acc.session.Token).Post("/collection/list_multi", req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var body struct {
Data []EncryptedCollection `msgpack:"data"`
}
if err := codec.NewDecoder(resp.Body).Decode(&body); err != nil {
return nil, err
}

return body.Data, nil
}

// Signup a new user account and returns an Account instance.
func Signup(c *Client, user User, password string) (*Account, error) {
acc := newAccount(c)
Expand Down
76 changes: 76 additions & 0 deletions encrypted_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package etebase

import (
"time"

"github.com/etesync/etebase-go/internal/codec"
"github.com/google/uuid"
)

const (
CurrentVersion uint8 = 1
)

type CollectionAccessLevel uint32

const (
ReadOnly CollectionAccessLevel = iota
Admin
ReadWrite
)

type EncryptedCollection struct {
Item EncryptedItem `msgpack:"item"`
AccessLevel CollectionAccessLevel `msgpack:"accessLevel"`
Type []byte `msgpack:"collectionType"`
Key []byte `msgpack:"collectionKey"`
Stoken string `msgpack:"stoken"`
}

func NewEncryptedCollection(cm *CryptoManager, cType string, meta interface{}, content []byte) (*EncryptedCollection, error) {
metaBytes, err := codec.Marshal(meta)
if err != nil {
return nil, err
}

uid1 := uuid.New().String()
uid2 := uuid.New().String()
return &EncryptedCollection{
Item: EncryptedItem{
UID: uid1,
Version: CurrentVersion,
Content: EncryptedRevision{
UID: uid2,
Meta: metaBytes,
Chunks: []ChunkArrayItem{},
},
},
Type: []byte(time.Now().String()),
Key: []byte("key"),
}, nil
}

type EncryptedItem struct {
UID string `msgpack:"uid"`
Version uint8 `msgpack:"version"`
Etag []byte `msgpack:"etag"`
Content EncryptedRevision `msgpack:"content"`
}

type EncryptedRevision struct {
UID string `msgpack:"uid"`
Meta []byte `msgpack:"meta"`
Deleted bool `msgpack:"deleted"`
Chunks []ChunkArrayItem `msgpack:"chunks"`
}

type ChunkArrayItem struct {
Data string
Option []byte
}

func (c ChunkArrayItem) MarshalMsgpack() ([]byte, error) {
return codec.Marshal([]interface{}{
c.Data, c.Option,
})
}
31 changes: 30 additions & 1 deletion etebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,38 @@ func (s *EtebaseSuite) TestLogin() {
}

func (s *EtebaseSuite) TestCollections() {
col, err := etebase.NewEncryptedCollection(nil, "testType", nil, nil)
col.AccessLevel = etebase.Admin
s.Require().NoError(err)

s.Require().NoError(
s.account.Collection(),
s.account.CreateCollection(col),
)

s.Run("ListMulti", func() {
found, err := s.account.ListCollections([][]byte{
col.Type,
})
s.Require().NoError(err)
s.Require().NotEmpty(found)
})

s.Run("GetCollection", func() {
found, err := s.account.GetCollection(col.Item.UID)
s.Require().NoError(err)
s.Require().NotEmpty(found.Stoken)

expected := col
expected.Stoken = found.Stoken

s.Require().Equal(expected, found)
})

s.Run("NotFound", func() {
_, err := s.account.GetCollection("not-an-existing-uid")
s.Require().Error(err)
s.Require().Contains(err.Error(), "not found")
})
}

// TestLogout logs-out an account twice. The second time it shouldn't be
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/etesync/etebase-go
go 1.14

require (
github.com/google/uuid v1.1.2
github.com/stretchr/testify v1.6.1
github.com/vmihailenco/msgpack/v5 v5.0.0
golang.org/x/crypto v0.0.0-20201116153603-4be66e5b6582
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
3 changes: 2 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ type User struct {
type ErrorResponse struct {
Code string `msgpack:"code"`
Detail string `msgpack:"detail"`
Field string `msgpack:"field,omitempty"`
Errors []ErrorResponse `msgpack:"errors,omitempty"`
}

func (err *ErrorResponse) Error() string {
return fmt.Sprintf("code: %s, detail: %s", err.Code, err.Detail)
return fmt.Sprintf("code: %s, detail: %s (%+v)", err.Code, err.Detail, err.Errors)
}

type SignupRequest struct {
Expand Down

0 comments on commit c5e5c3a

Please sign in to comment.