-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.go
More file actions
73 lines (65 loc) · 2.1 KB
/
export.go
File metadata and controls
73 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package d1http
import (
"context"
"net/http"
"time"
)
type DumpOptions struct {
NoData bool `json:"no_data,omitempty"`
NoSchema bool `json:"no_schema,omitempty"`
Tables []string `json:"tables,omitempty"`
}
type ExportRequest struct {
OutputFormat string `json:"output_format"`
CurrentBookmark string `json:"current_bookmark,omitempty"`
DumpOptions *DumpOptions `json:"dump_options,omitempty"`
}
type ExportResponse struct {
AtBookmark string `json:"at_bookmark,omitempty"`
Error string `json:"error,omitempty"`
Messages []string `json:"messages,omitempty"`
Result *ExportResult `json:"result,omitempty"`
Status string `json:"status,omitempty"`
Success bool `json:"success,omitempty"`
Type string `json:"type,omitempty"`
}
type ExportResult struct {
Filename string `json:"filename,omitempty"`
SignedURL string `json:"signed_url,omitempty"`
}
func (c *Client) Export(ctx context.Context, databaseID string, req ExportRequest) (*ExportResponse, error) {
id, err := c.databaseID(databaseID)
if err != nil {
return nil, err
}
if req.OutputFormat == "" {
req.OutputFormat = "polling"
}
var env envelope[ExportResponse]
err = c.do(ctx, http.MethodPost, apiPath("accounts", c.cfg.AccountID, "d1/database", id, "export"), nil, req, &env)
return &env.Result, err
}
func (c *Client) PollExport(ctx context.Context, databaseID, bookmark string) (*ExportResponse, error) {
return c.Export(ctx, databaseID, ExportRequest{OutputFormat: "polling", CurrentBookmark: bookmark})
}
func (c *Client) ExportUntilComplete(ctx context.Context, databaseID string, req ExportRequest, interval time.Duration) (*ExportResponse, error) {
if interval <= 0 {
interval = 2 * time.Second
}
res, err := c.Export(ctx, databaseID, req)
if err != nil {
return nil, err
}
for res.Status != "complete" && res.Status != "error" {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(interval):
}
res, err = c.PollExport(ctx, databaseID, res.AtBookmark)
if err != nil {
return nil, err
}
}
return res, nil
}