mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
package module
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/jwetzell/showbridge-go/internal/config"
|
|
"github.com/jwetzell/showbridge-go/internal/route"
|
|
)
|
|
|
|
type HTTPClient struct {
|
|
config config.ModuleConfig
|
|
ctx context.Context
|
|
client *http.Client
|
|
router route.RouteIO
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func init() {
|
|
RegisterModule(ModuleRegistration{
|
|
Type: "http.client",
|
|
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
|
|
|
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
|
|
|
if !ok {
|
|
return nil, errors.New("http.client unable to get router from context")
|
|
}
|
|
|
|
return &HTTPClient{config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
|
},
|
|
})
|
|
}
|
|
|
|
func (hc *HTTPClient) Id() string {
|
|
return hc.config.Id
|
|
}
|
|
|
|
func (hc *HTTPClient) Type() string {
|
|
return hc.config.Type
|
|
}
|
|
|
|
func (hc *HTTPClient) Run() error {
|
|
|
|
hc.client = &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
<-hc.ctx.Done()
|
|
hc.logger.Debug("done")
|
|
return nil
|
|
}
|
|
|
|
func (hc *HTTPClient) Output(ctx context.Context, payload any) error {
|
|
|
|
payloadRequest, ok := payload.(*http.Request)
|
|
|
|
if !ok {
|
|
return errors.New("http.client is only able to output an http.Request")
|
|
}
|
|
|
|
if hc.client == nil {
|
|
return errors.New("http.client client is nil")
|
|
}
|
|
|
|
response, err := hc.client.Do(payloadRequest)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if hc.router != nil {
|
|
hc.router.HandleInput(hc.Id(), response)
|
|
}
|
|
|
|
return nil
|
|
}
|