-
Notifications
You must be signed in to change notification settings - Fork 317
/
warehouse.go
143 lines (121 loc) · 3.81 KB
/
warehouse.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/rudderlabs/rudder-server/utils/httputil"
)
const defaultTimeout = 10 * time.Second
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// StagingFile contains the require metadata to process a staging file.
type StagingFile struct {
WorkspaceID string
SourceID string
DestinationID string
Location string
Schema map[string]map[string]interface{}
FirstEventAt string
LastEventAt string
TotalEvents int
TotalBytes int
UseRudderStorage bool
DestinationRevisionID string
// cloud sources specific info
SourceBatchID string
SourceTaskID string
SourceTaskRunID string
SourceJobID string
SourceJobRunID string
TimeWindow time.Time
}
// legacyPayload is used to maintain backwards compatibility with the /v1 endpoint.
type legacyPayload struct {
WorkspaceID string
Schema map[string]map[string]interface{}
BatchDestination stagingFileBatchDestination
Location string
FirstEventAt string
LastEventAt string
TotalEvents int
TotalBytes int
UseRudderStorage bool
DestinationRevisionID string
// cloud sources specific info
SourceBatchID string
SourceTaskID string
SourceTaskRunID string
SourceJobID string
SourceJobRunID string
TimeWindow time.Time
}
type stagingFileBatchDestination struct {
Source struct{ ID string }
Destination struct{ ID string }
}
type Warehouse struct {
baseURL string
client *http.Client
}
type WarehouseOpts func(*Warehouse)
func WithTimeout(timeout time.Duration) WarehouseOpts {
return func(warehouse *Warehouse) {
warehouse.client.Timeout = timeout
}
}
func NewWarehouse(baseURL string, opts ...WarehouseOpts) *Warehouse {
warehouse := &Warehouse{
baseURL: baseURL,
client: &http.Client{Timeout: defaultTimeout},
}
for _, opt := range opts {
opt(warehouse)
}
return warehouse
}
func (warehouse *Warehouse) Process(ctx context.Context, stagingFile StagingFile) error {
legacy := legacyPayload{
WorkspaceID: stagingFile.WorkspaceID,
Schema: stagingFile.Schema,
BatchDestination: stagingFileBatchDestination{
Source: struct{ ID string }{ID: stagingFile.SourceID},
Destination: struct{ ID string }{ID: stagingFile.DestinationID},
},
Location: stagingFile.Location,
FirstEventAt: stagingFile.FirstEventAt,
LastEventAt: stagingFile.LastEventAt,
TotalEvents: stagingFile.TotalEvents,
TotalBytes: stagingFile.TotalBytes,
UseRudderStorage: stagingFile.UseRudderStorage,
DestinationRevisionID: stagingFile.DestinationRevisionID,
SourceBatchID: stagingFile.SourceBatchID,
SourceTaskID: stagingFile.SourceTaskID,
SourceTaskRunID: stagingFile.SourceTaskRunID,
SourceJobID: stagingFile.SourceJobID,
SourceJobRunID: stagingFile.SourceJobRunID,
TimeWindow: stagingFile.TimeWindow,
}
jsonPayload, err := json.Marshal(legacy)
if err != nil {
return fmt.Errorf("marshaling staging file: %w", err)
}
uri := fmt.Sprintf(`%s/v1/process`, warehouse.baseURL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, bytes.NewBuffer(jsonPayload))
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := warehouse.client.Do(req)
if err != nil {
return fmt.Errorf("http request to %q: %w", warehouse.baseURL, err)
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code %q on %s: %v", resp.Status, warehouse.baseURL, string(body))
}
return nil
}