54 lines
1,018 B
Go
54 lines
1,018 B
Go
package k2v
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type BatchInsertItem struct {
|
|
PartitionKey string `json:"pk"`
|
|
SortKey string `json:"sk"`
|
|
CausalityToken *string `json:"ct"`
|
|
Value Item `json:"v"`
|
|
}
|
|
|
|
func (c *Client) InsertBatch(ctx context.Context, b Bucket, items []BatchInsertItem) error {
|
|
u, err := url.Parse(c.endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u.Path = string(b)
|
|
|
|
reqBody, err := json.Marshal(items)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(reqBody))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := c.executeRequest(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
return fmt.Errorf("http status code %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|