-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_step_request_parse.go
More file actions
163 lines (144 loc) · 4.38 KB
/
pipeline_step_request_parse.go
File metadata and controls
163 lines (144 loc) · 4.38 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package module
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/CrisisTextLine/modular"
)
// RequestParseStep extracts path parameters, query parameters, request body,
// and optionally request headers from the HTTP request stored in pipeline metadata.
type RequestParseStep struct {
name string
pathParams []string
queryParams []string
parseBody bool
parseHeaders []string
}
// NewRequestParseStepFactory returns a StepFactory that creates RequestParseStep instances.
func NewRequestParseStepFactory() StepFactory {
return func(name string, config map[string]any, _ modular.Application) (PipelineStep, error) {
var pathParams []string
if pp, ok := config["path_params"]; ok {
if list, ok := pp.([]any); ok {
for _, item := range list {
if s, ok := item.(string); ok {
pathParams = append(pathParams, s)
}
}
}
}
var queryParams []string
if qp, ok := config["query_params"]; ok {
if list, ok := qp.([]any); ok {
for _, item := range list {
if s, ok := item.(string); ok {
queryParams = append(queryParams, s)
}
}
}
}
parseBody, _ := config["parse_body"].(bool)
var parseHeaders []string
if ph, ok := config["parse_headers"]; ok {
if list, ok := ph.([]any); ok {
for _, item := range list {
if s, ok := item.(string); ok {
parseHeaders = append(parseHeaders, s)
}
}
}
}
return &RequestParseStep{
name: name,
pathParams: pathParams,
queryParams: queryParams,
parseBody: parseBody,
parseHeaders: parseHeaders,
}, nil
}
}
// Name returns the step name.
func (s *RequestParseStep) Name() string { return s.name }
// Execute extracts path parameters, query parameters, and/or request body
// from the HTTP request stored in pipeline context metadata.
func (s *RequestParseStep) Execute(_ context.Context, pc *PipelineContext) (*StepResult, error) {
output := make(map[string]any)
// Extract path parameters using _route_pattern and actual request path
if len(s.pathParams) > 0 {
pathParamValues := make(map[string]any)
routePattern, _ := pc.Metadata["_route_pattern"].(string)
req, _ := pc.Metadata["_http_request"].(*http.Request)
if routePattern != "" && req != nil {
actualPath := req.URL.Path
patternParts := strings.Split(strings.Trim(routePattern, "/"), "/")
actualParts := strings.Split(strings.Trim(actualPath, "/"), "/")
// Build a map of param name -> value by matching {param} segments
paramMap := make(map[string]string)
for i, pp := range patternParts {
if strings.HasPrefix(pp, "{") && strings.HasSuffix(pp, "}") {
paramName := pp[1 : len(pp)-1]
if i < len(actualParts) {
paramMap[paramName] = actualParts[i]
}
}
}
// Extract only requested path params
for _, name := range s.pathParams {
if val, ok := paramMap[name]; ok {
pathParamValues[name] = val
}
}
}
output["path_params"] = pathParamValues
}
// Extract query parameters
if len(s.queryParams) > 0 {
queryValues := make(map[string]any)
req, _ := pc.Metadata["_http_request"].(*http.Request)
if req != nil {
q := req.URL.Query()
for _, name := range s.queryParams {
if val := q.Get(name); val != "" {
queryValues[name] = val
}
}
}
output["query"] = queryValues
}
// Extract request headers (always includes the key, empty string if header absent)
if len(s.parseHeaders) > 0 {
headerValues := make(map[string]any)
req, _ := pc.Metadata["_http_request"].(*http.Request)
for _, name := range s.parseHeaders {
if req != nil {
headerValues[name] = req.Header.Get(name)
} else {
headerValues[name] = ""
}
}
output["headers"] = headerValues
}
// Parse request body — first try trigger data (command handler pre-parses body),
// then fall back to reading from the HTTP request directly
if s.parseBody {
if body, ok := pc.TriggerData["body"].(map[string]any); ok {
output["body"] = body
} else if body, ok := pc.Current["body"].(map[string]any); ok {
output["body"] = body
} else {
req, _ := pc.Metadata["_http_request"].(*http.Request)
if req != nil && req.Body != nil {
bodyBytes, err := io.ReadAll(req.Body)
if err == nil && len(bodyBytes) > 0 {
var bodyData map[string]any
if json.Unmarshal(bodyBytes, &bodyData) == nil {
output["body"] = bodyData
}
}
}
}
}
return &StepResult{Output: output}, nil
}