mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
create encode processors for http request/response
This commit is contained in:
79
internal/processing/http-request-create.go
Normal file
79
internal/processing/http-request-create.go
Normal file
@@ -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
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
41
internal/processing/http-response-encode.go
Normal file
41
internal/processing/http-response-encode.go
Normal file
@@ -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
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user