-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_update.go
More file actions
53 lines (44 loc) · 1.21 KB
/
source_update.go
File metadata and controls
53 lines (44 loc) · 1.21 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
package unstructured
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// UpdateSourceRequest represents the request to update a source connector.
type UpdateSourceRequest struct {
ID string
Config SourceConfig
}
// UpdateSource updates the configuration of an existing source connector.
// It returns the updated source connector.
func (c *Client) UpdateSource(ctx context.Context, in UpdateSourceRequest) (*Source, error) {
config, err := json.Marshal(in.Config)
if err != nil {
return nil, fmt.Errorf("failed to marshal config: %w", err)
}
wrapper := struct {
Config json.RawMessage `json:"config"`
}{
Config: json.RawMessage(config),
}
body, err := json.Marshal(wrapper)
if err != nil {
return nil, fmt.Errorf("failed to marshal update request: %w", err)
}
req, err := http.NewRequestWithContext(ctx,
http.MethodPut,
c.endpoint.JoinPath("sources", in.ID).String(),
bytes.NewReader(body),
)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
var source Source
if err := c.do(req, &source); err != nil {
return nil, fmt.Errorf("failed to update source: %w", err)
}
return &source, nil
}