Skip to content

Commit b2ddbb5

Browse files
committed
Added command infrastructure - not finished
1 parent fdbe0e1 commit b2ddbb5

File tree

24 files changed

+289
-368
lines changed

24 files changed

+289
-368
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package common
2+
3+
type CommandHandler interface {
4+
CanHandle(command any) bool
5+
Handle(command any) any
6+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package client
2+
3+
import (
4+
"github.com/go-clarum/agent/application/command/common"
5+
"github.com/go-clarum/agent/application/command/http/client/commands"
6+
"github.com/go-clarum/agent/application/command/http/client/internal"
7+
"github.com/go-clarum/agent/infrastructure/logging"
8+
"net/http"
9+
)
10+
11+
type handler struct {
12+
endpoints map[string]*internal.Endpoint
13+
logger *logging.Logger
14+
}
15+
16+
func (h *handler) CanHandle(command any) bool {
17+
return true
18+
}
19+
20+
func (h *handler) Handle(command any) any {
21+
return nil
22+
}
23+
24+
func NewHttpClientHandler() common.CommandHandler {
25+
return &handler{
26+
endpoints: make(map[string]*internal.Endpoint),
27+
logger: logging.NewLogger("HttpClientHandler"),
28+
}
29+
}
30+
31+
func (h *handler) InitializeEndpoint(req *commands.InitEndpointCommand) error {
32+
newEndpoint, err := internal.NewEndpoint(req)
33+
34+
if err != nil {
35+
h.logger.Errorf("failed to initialize HTTP client endpoint - %s", err)
36+
return err
37+
}
38+
39+
if oldEndpoint, exists := h.endpoints[newEndpoint.Name]; exists {
40+
h.logger.Infof("HTTP client endpoint [%s] already exists - replacing", oldEndpoint.Name)
41+
}
42+
43+
h.endpoints[newEndpoint.Name] = newEndpoint
44+
logging.Infof("registered HTTP client endpoint [%s]", newEndpoint.Name)
45+
46+
return nil
47+
}
48+
49+
func (h *handler) SendAction(sendAction *commands.SendCommand) error {
50+
endpoint, exists := h.endpoints[sendAction.EndpointName]
51+
if !exists {
52+
h.logger.Errorf("HTTP client endpoint [%s] not found - action [%s] will not be executed",
53+
sendAction.EndpointName, sendAction.Name)
54+
}
55+
56+
return endpoint.Send(sendAction)
57+
}
58+
59+
func (h *handler) ReceiveAction(receiveAction *commands.ReceiveCommand) (*http.Response, error) {
60+
endpoint, exists := h.endpoints[receiveAction.EndpointName]
61+
if !exists {
62+
h.logger.Errorf("HTTP client endpoint [%s] not found - action [%s] will not be executed",
63+
receiveAction.EndpointName, receiveAction.Name)
64+
}
65+
66+
return endpoint.Receive(receiveAction)
67+
}

src/application/services/http/client/actions/actions.go renamed to src/application/command/http/client/commands/commands.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
package actions
1+
package commands
22

33
import (
44
"fmt"
5-
"github.com/go-clarum/agent/application/services/http/common/model"
5+
"github.com/go-clarum/agent/application/command/http/common/model"
66
"strconv"
77
"time"
88
)
99

10-
type InitEndpointAction struct {
10+
type InitEndpointCommand struct {
1111
Name string
1212
BaseUrl string
1313
ContentType string
1414
TimeoutSeconds time.Duration
1515
}
1616

17-
type SendAction struct {
17+
type SendCommand struct {
1818
Name string
1919
Url string
2020
Path []string
@@ -25,7 +25,7 @@ type SendAction struct {
2525
EndpointName string
2626
}
2727

28-
type ReceiveAction struct {
28+
type ReceiveCommand struct {
2929
Name string
3030
PayloadType model.PayloadType
3131
StatusCode int
@@ -34,7 +34,7 @@ type ReceiveAction struct {
3434
EndpointName string
3535
}
3636

37-
func (action *SendAction) ToString() string {
37+
func (action *SendCommand) ToString() string {
3838
return fmt.Sprintf(
3939
"["+
4040
"Method: %s, "+
@@ -48,7 +48,7 @@ func (action *SendAction) ToString() string {
4848
action.Headers, action.QueryParams, action.Payload)
4949
}
5050

51-
func (action *ReceiveAction) ToString() string {
51+
func (action *ReceiveCommand) ToString() string {
5252
statusCodeText := "none"
5353
if action.StatusCode > 0 {
5454
statusCodeText = strconv.Itoa(action.StatusCode)

src/application/services/http/client/internal/endpoint.go renamed to src/application/command/http/client/internal/endpoint.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"github.com/go-clarum/agent/application/command/http/client/commands"
8+
"github.com/go-clarum/agent/application/command/http/common/constants"
9+
"github.com/go-clarum/agent/application/command/http/common/utils"
10+
"github.com/go-clarum/agent/application/command/http/common/validators"
711
"github.com/go-clarum/agent/application/control"
8-
"github.com/go-clarum/agent/application/services/http/client/actions"
9-
"github.com/go-clarum/agent/application/services/http/common/constants"
10-
"github.com/go-clarum/agent/application/services/http/common/utils"
11-
"github.com/go-clarum/agent/application/services/http/common/validators"
1212
"github.com/go-clarum/agent/application/utils/durations"
1313
clarumstrings "github.com/go-clarum/agent/application/validators/strings"
1414
"github.com/go-clarum/agent/infrastructure/config"
@@ -32,7 +32,7 @@ type responsePair struct {
3232
error error
3333
}
3434

35-
func NewEndpoint(ic *actions.InitEndpointAction) (*Endpoint, error) {
35+
func NewEndpoint(ic *commands.InitEndpointCommand) (*Endpoint, error) {
3636
if clarumstrings.IsBlank(ic.Name) {
3737
return nil, errors.New("cannot create HTTP client endpoint - name is empty")
3838
}
@@ -51,7 +51,7 @@ func NewEndpoint(ic *actions.InitEndpointAction) (*Endpoint, error) {
5151
}, nil
5252
}
5353

54-
func (endpoint *Endpoint) Send(action *actions.SendAction) error {
54+
func (endpoint *Endpoint) Send(action *commands.SendCommand) error {
5555
if action == nil {
5656
return endpoint.handleError("send action is nil", nil)
5757
}
@@ -109,7 +109,7 @@ func closeBody(res *http.Response) {
109109
}
110110

111111
// validationOptions pass by value is intentional
112-
func (endpoint *Endpoint) Receive(action *actions.ReceiveAction) (*http.Response, error) {
112+
func (endpoint *Endpoint) Receive(action *commands.ReceiveCommand) (*http.Response, error) {
113113
if action == nil {
114114
return nil, endpoint.handleError("receive action is nil", nil)
115115
}
@@ -135,7 +135,7 @@ func (endpoint *Endpoint) Receive(action *actions.ReceiveAction) (*http.Response
135135
}
136136

137137
// Put missing data into a message to send: baseUrl & ContentType Header
138-
func (endpoint *Endpoint) enrichSendAction(action *actions.SendAction) {
138+
func (endpoint *Endpoint) enrichSendAction(action *commands.SendCommand) {
139139
if clarumstrings.IsBlank(action.Url) {
140140
action.Url = endpoint.baseUrl
141141
}
@@ -151,15 +151,15 @@ func (endpoint *Endpoint) enrichSendAction(action *actions.SendAction) {
151151
}
152152

153153
// Put missing data into message to receive: ContentType Header
154-
func (endpoint *Endpoint) enrichReceiveAction(action *actions.ReceiveAction) {
154+
func (endpoint *Endpoint) enrichReceiveAction(action *commands.ReceiveCommand) {
155155
if clarumstrings.IsNotBlank(endpoint.contentType) {
156156
if _, exists := action.Headers[constants.ContentTypeHeaderName]; !exists {
157157
action.Headers[constants.ContentTypeHeaderName] = endpoint.contentType
158158
}
159159
}
160160
}
161161

162-
func (endpoint *Endpoint) validateMessageToSend(action *actions.SendAction) error {
162+
func (endpoint *Endpoint) validateMessageToSend(action *commands.SendCommand) error {
163163
if clarumstrings.IsBlank(action.Method) {
164164
return endpoint.handleError("send action is invalid - missing HTTP method", nil)
165165
}
@@ -173,7 +173,7 @@ func (endpoint *Endpoint) validateMessageToSend(action *actions.SendAction) erro
173173
return nil
174174
}
175175

176-
func (endpoint *Endpoint) buildRequest(action *actions.SendAction) (*http.Request, error) {
176+
func (endpoint *Endpoint) buildRequest(action *commands.SendCommand) (*http.Request, error) {
177177
url := utils.BuildPath(action.Url, action.Path...)
178178

179179
req, err := http.NewRequest(action.Method, url, bytes.NewBufferString(action.Payload))

src/application/services/http/common/validators/validators.go renamed to src/application/command/http/common/validators/validators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package validators
33
import (
44
"errors"
55
"fmt"
6-
"github.com/go-clarum/agent/application/services/http/common/model"
7-
"github.com/go-clarum/agent/application/services/http/common/utils"
6+
"github.com/go-clarum/agent/application/command/http/common/model"
7+
"github.com/go-clarum/agent/application/command/http/common/utils"
88
"github.com/go-clarum/agent/application/utils/arrays"
99
clarumstrings "github.com/go-clarum/agent/application/validators/strings"
1010
"github.com/go-clarum/agent/infrastructure/logging"

src/application/services/http/server/actions/actions.go renamed to src/application/command/http/server/commands/commands.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package actions
1+
package commands
22

33
import (
44
"fmt"
5-
"github.com/go-clarum/agent/application/services/http/common/model"
5+
"github.com/go-clarum/agent/application/command/http/common/model"
66
"time"
77
)
88

9-
type InitEndpointAction struct {
9+
type InitEndpointCommand struct {
1010
Name string
1111
Port uint
1212
ContentType string
1313
TimeoutSeconds time.Duration
1414
}
1515

16-
type SendAction struct {
16+
type SendCommand struct {
1717
Name string
1818
PayloadType model.PayloadType
1919
StatusCode int
@@ -22,7 +22,7 @@ type SendAction struct {
2222
EndpointName string
2323
}
2424

25-
type ReceiveAction struct {
25+
type ReceiveCommand struct {
2626
Name string
2727
Url string
2828
Path []string
@@ -34,7 +34,7 @@ type ReceiveAction struct {
3434
EndpointName string
3535
}
3636

37-
func (action *ReceiveAction) ToString() string {
37+
func (action *ReceiveCommand) ToString() string {
3838
return fmt.Sprintf(
3939
"["+
4040
"Method: %s, "+

0 commit comments

Comments
 (0)