-
Notifications
You must be signed in to change notification settings - Fork 31
/
import.go
110 lines (91 loc) · 3.11 KB
/
import.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package stream_chat
import (
"context"
"net/http"
"net/url"
"strconv"
"time"
)
type ImportTaskHistory struct {
CreatedAt time.Time `json:"created_at"`
NextState string `json:"next_state"`
PrevState string `json:"prev_state"`
}
type ImportMode string
const (
InsertMode ImportMode = "insert"
UpsertMode ImportMode = "upsert"
)
type ImportTask struct {
CreatedAt time.Time `json:"created_at"`
Path string `json:"path"`
Mode ImportMode `json:"mode"`
History []*ImportTaskHistory `json:"history"`
ID string `json:"id"`
State string `json:"state"`
UpdatedAt time.Time `json:"updated_at"`
Result interface{} `json:"result"`
Size *int `json:"size"`
}
type ListImportsOptions struct {
Limit int
Offset int
}
type CreateImportResponse struct {
ImportTask *ImportTask `json:"import_task"`
Response
}
type CreateImportURLResponse struct {
Path string `json:"path"`
UploadURL string `json:"upload_url"`
Response
}
type GetImportResponse struct {
ImportTask *ImportTask `json:"import_task"`
Response
}
type ListImportsResponse struct {
ImportTasks []*ImportTask `json:"import_tasks"`
Response
}
// CreateImportURL creates a new import URL.
// Note: Do not use this.
// It is present for internal usage only.
// This function can, and will, break and/or be removed at any point in time.
func (c *Client) CreateImportURL(ctx context.Context, filename string) (*CreateImportURLResponse, error) {
var resp CreateImportURLResponse
err := c.makeRequest(ctx, http.MethodPost, "import_urls", nil, map[string]string{"filename": filename}, &resp)
return &resp, err
}
// CreateImport creates a new import task.
// Note: Do not use this.
// It is present for internal usage only.
// This function can, and will, break and/or be removed at any point in time.
func (c *Client) CreateImport(ctx context.Context, filePath string, mode ImportMode) (*CreateImportResponse, error) {
var resp CreateImportResponse
err := c.makeRequest(ctx, http.MethodPost, "imports", nil, map[string]string{"path": filePath, "mode": string(mode)}, &resp)
return &resp, err
}
// GetImport returns an import task.
// Note: Do not use this.
// It is present for internal usage only.
// This function can, and will, break and/or be removed at any point in time.
func (c *Client) GetImport(ctx context.Context, id string) (*GetImportResponse, error) {
var resp GetImportResponse
err := c.makeRequest(ctx, http.MethodGet, "imports/"+id, nil, nil, &resp)
return &resp, err
}
// ListImports returns all import tasks.
// Note: Do not use this.
// It is present for internal usage only.
// This function can, and will, break and/or be removed at any point in time.
func (c *Client) ListImports(ctx context.Context, opts *ListImportsOptions) (*ListImportsResponse, error) {
params := url.Values{}
if opts != nil {
params.Set("limit", strconv.Itoa(opts.Limit))
params.Set("offset", strconv.Itoa(opts.Offset))
}
var resp ListImportsResponse
err := c.makeRequest(ctx, http.MethodGet, "imports", params, nil, &resp)
return &resp, err
}