Skip to content

Commit

Permalink
Update client interface to match Ruby SDK.
Browse files Browse the repository at this point in the history
Records can only be created via the Write method;
removed the NewRecord method.
  • Loading branch information
Justin Bailey committed Jun 5, 2017
1 parent f57d7cc commit 244d88a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
28 changes: 16 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Meta struct {
Plain map[string]string `json:"plain"`
Created time.Time `json:"created"`
LastModified time.Time `json:"last_modified"`
Version string `json:"version"`
}

// Record contains a plaintext 'Meta' object containing record metadata,
Expand Down Expand Up @@ -290,40 +291,43 @@ func (c *Client) Read(ctx context.Context, recordID string) (*Record, error) {
return record, nil
}

// NewRecord creates a new record of the given content type.
func (c *Client) NewRecord(recordType string) *Record {
return &Record{
// Write writes a new encrypted record to the database. Returns the new record (with
// the original, unencrypted data)
func (c *Client) Write(ctx context.Context, recordType string, data *map[string]string, plain *map[string]string) (*Record, error) {
record := &Record{
Meta: Meta{
Type: recordType,
WriterID: c.Options.ClientID,
UserID: c.Options.ClientID, // for now
Plain: *plain,
},
Data: make(map[string]string),
Data: *data,
}
}

// Write writes a new encrypted record to the database, returning the new record's
// unique ID.
func (c *Client) Write(ctx context.Context, record *Record) (string, error) {
encryptedRecord, err := c.encryptRecord(ctx, record)
if err != nil {
return "", err
return nil, err
}

buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(encryptedRecord)
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/storage/records", c.apiURL()), buf)
if err != nil {
return "", err
return nil, err
}

resp, err := c.rawCall(ctx, req, encryptedRecord)
if err != nil {
return "", err
return nil, err
}

defer closeResp(resp)
return encryptedRecord.Meta.RecordID, nil

record.Meta.Created = encryptedRecord.Meta.Created
record.Meta.LastModified = encryptedRecord.Meta.LastModified
record.Meta.Version = encryptedRecord.Meta.Version
record.Meta.RecordID = encryptedRecord.Meta.RecordID
return record, nil
}

// Delete deletes a record given a record ID.
Expand Down
28 changes: 15 additions & 13 deletions cmd/e3db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,19 @@ func cmdWrite(cmd *cli.Cmd) {
recordData = *data
}

record := client.NewRecord(*recordType)
err := json.NewDecoder(strings.NewReader(recordData)).Decode(&record.Data)
jsonData := make(map[string]string)

err := json.NewDecoder(strings.NewReader(recordData)).Decode(&jsonData)
if err != nil {
dieErr(err)
}

id, err := client.Write(context.Background(), record)
record, err := client.Write(context.Background(), *recordType, &jsonData, nil)
if err != nil {
dieErr(err)
}

fmt.Println(id)
fmt.Println(record.Meta.RecordID)
}
}

Expand All @@ -238,7 +239,7 @@ func cmdWriteFile(cmd *cli.Cmd) {

cmd.Action = func() {
client := options.getClient()
record := client.NewRecord(*recordType)
data := make(map[string]string)

f, err := os.Open(*filename)
if err != nil {
Expand All @@ -260,16 +261,16 @@ func cmdWriteFile(cmd *cli.Cmd) {
buf := new(bytes.Buffer)
buf.ReadFrom(f)

record.Data["filename"] = fi.Name()
record.Data["contents"] = base64.RawURLEncoding.EncodeToString(buf.Bytes())
record.Data["size"] = strconv.FormatInt(fi.Size(), 10)
data["filename"] = fi.Name()
data["contents"] = base64.RawURLEncoding.EncodeToString(buf.Bytes())
data["size"] = strconv.FormatInt(fi.Size(), 10)

id, err := client.Write(context.Background(), record)
record, err := client.Write(context.Background(), *recordType, &data, nil)
if err != nil {
dieErr(err)
}

fmt.Println(id)
fmt.Println(record.Meta.RecordID)
}
}

Expand Down Expand Up @@ -561,9 +562,10 @@ func cmdFeedback(cmd *cli.Cmd) {
}

// Write feedback to the database
record := client.NewRecord("feedback")
record.Data["comment"] = text
id, err := client.Write(context.Background(), record)
data := make(map[string]string)
data["comment"] = text

id, err := client.Write(context.Background(), "feedback", &data, nil)
if err != nil {
dieErr(err)
}
Expand Down

0 comments on commit 244d88a

Please sign in to comment.