From d1df52da9ce64deb40018c1086ac4308e28960b5 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 22 Nov 2025 15:25:00 -0600 Subject: [PATCH] create encode processors for http request/response --- internal/processing/http-request-create.go | 79 +++++++++++++++++++++ internal/processing/http-response-encode.go | 41 +++++++++++ 2 files changed, 120 insertions(+) create mode 100644 internal/processing/http-request-create.go create mode 100644 internal/processing/http-response-encode.go diff --git a/internal/processing/http-request-create.go b/internal/processing/http-request-create.go new file mode 100644 index 0000000..bc99cf9 --- /dev/null +++ b/internal/processing/http-request-create.go @@ -0,0 +1,79 @@ +package processing + +import ( + "bytes" + "context" + "fmt" + "net/http" + "text/template" +) + +type HTTPRequestCreate struct { + config ProcessorConfig + Method string + URL *template.Template +} + +func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, error) { + + var urlBuffer bytes.Buffer + err := hre.URL.Execute(&urlBuffer, payload) + + if err != nil { + return nil, err + } + + urlString := urlBuffer.String() + + request, err := http.NewRequest(hre.Method, urlString, bytes.NewBuffer([]byte{})) + + if err != nil { + return nil, err + } + + return request, nil +} + +func (hre *HTTPRequestCreate) Type() string { + return hre.config.Type +} + +func init() { + RegisterProcessor(ProcessorRegistration{ + Type: "http.request.create", + New: func(config ProcessorConfig) (Processor, error) { + params := config.Params + + method, ok := params["method"] + + if !ok { + return nil, fmt.Errorf("http.request.create requires an method parameter") + } + + methodString, ok := method.(string) + + if !ok { + return nil, fmt.Errorf("http.request.create url must be a string") + } + + url, ok := params["url"] + + if !ok { + return nil, fmt.Errorf("http.request.create requires an url parameter") + } + + urlString, ok := url.(string) + + if !ok { + return nil, fmt.Errorf("http.request.create url must be a string") + } + + urlTemplate, err := template.New("url").Parse(urlString) + + if err != nil { + return nil, err + } + return &HTTPRequestCreate{config: config, URL: urlTemplate, Method: methodString}, nil + }, + }) +} diff --git a/internal/processing/http-response-encode.go b/internal/processing/http-response-encode.go new file mode 100644 index 0000000..da0d587 --- /dev/null +++ b/internal/processing/http-response-encode.go @@ -0,0 +1,41 @@ +package processing + +import ( + "context" + "fmt" + "io" + "net/http" +) + +type HTTPResponseEncode struct { + config ProcessorConfig +} + +func (hre *HTTPResponseEncode) Process(ctx context.Context, payload any) (any, error) { + payloadResponse, ok := payload.(*http.Response) + defer payloadResponse.Body.Close() + + if !ok { + return nil, fmt.Errorf("http.response.encode processor only accepts an http.Response") + } + + bytes, err := io.ReadAll(payloadResponse.Body) + if err != nil { + return nil, err + } + + return bytes, nil +} + +func (hre *HTTPResponseEncode) Type() string { + return hre.config.Type +} + +func init() { + RegisterProcessor(ProcessorRegistration{ + Type: "http.response.encode", + New: func(config ProcessorConfig) (Processor, error) { + return &HTTPResponseEncode{config: config}, nil + }, + }) +}