-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_backend_config.go
56 lines (49 loc) · 1.63 KB
/
transformer_backend_config.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
package transformer
import (
"fmt"
"net/http"
kithttptest "github.com/rudderlabs/rudder-go-kit/testhelper/httptest"
"github.com/google/uuid"
)
const (
getByVersionIdEndPoint = "/transformation/getByVersionId"
versionIDKey = "versionId"
)
func newTestBackendConfigServer(transformations map[string]string) *kithttptest.Server {
return kithttptest.NewServer(NewTransformerBackendConfigHandler(transformations))
}
// NewTransformerBackendConfigHandler returns http request handler to handle all backend config requests by transformer
func NewTransformerBackendConfigHandler(transformations map[string]string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc(getByVersionIdEndPoint, getByVersionIdHandler(transformations))
return mux
}
func getByVersionIdHandler(transformations map[string]string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
transformationVersionId := r.URL.Query().Get(versionIDKey)
transformationCode, ok := transformations[transformationVersionId]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
_, err := w.Write([]byte(fmt.Sprintf(`{
"id": %q,
"createdAt": "2023-03-27T11:40:00.894Z",
"updatedAt": "2023-03-27T11:40:00.894Z",
"versionId": %q,
"name": "Add Transformed field",
"description": "",
"code": %q,
"secretsVersion": null,
"codeVersion": "1",
"language": "javascript",
"imports": [],
"secrets": {}
}`, uuid.NewString(), transformationVersionId, transformationCode)))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_ = fmt.Errorf("error writing response: %v", err)
return
}
}
}