Skip to content

Commit f0b4fa0

Browse files
cpanatoalexellis
authored andcommitted
controller: add content type option to send in the headers when invoking a function
Signed-off-by: Carlos Panato <[email protected]>
1 parent 89f0ffc commit f0b4fa0

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ Set the following in `ControllerConfig`:
7373
}
7474
```
7575

76+
If you need to use the `Content-Type` header to validate or to check when it invoke the function you can set the `Content-Type`
77+
in `ControllerConfig`:
78+
79+
```go
80+
config := &types.ControllerConfig{
81+
...
82+
ContentType: "application/json",
83+
}
84+
```
85+
7686
View the code: [cmd/tester/main.go](cmd/tester/main.go)
7787

7888
## License

types/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type ControllerConfig struct {
3838

3939
// PrintSync indicates whether the sync should be logged.
4040
PrintSync bool
41+
42+
// ContentType defines which content type will be set in the header to inkoke the function. i.e "application/json".
43+
// Optional, if not set the Content-Type header will not be set.
44+
ContentType string
4145
}
4246

4347
// Controller is used to invoke functions on a per-topic basis and to subscribe to responses returned by said functions.
@@ -79,6 +83,7 @@ func NewController(credentials *auth.BasicAuthCredentials, config *ControllerCon
7983

8084
invoker := NewInvoker(gatewayFunctionPath,
8185
MakeClient(config.UpstreamTimeout),
86+
config.ContentType,
8287
config.PrintResponse)
8388

8489
subs := []ResponseSubscriber{}

types/invoker.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Invoker struct {
2121
PrintResponse bool
2222
Client *http.Client
2323
GatewayURL string
24+
ContentType string
2425
Responses chan InvokerResponse
2526
}
2627

@@ -37,11 +38,12 @@ type InvokerResponse struct {
3738
}
3839

3940
// NewInvoker constructs an Invoker instance
40-
func NewInvoker(gatewayURL string, client *http.Client, printResponse bool) *Invoker {
41+
func NewInvoker(gatewayURL string, client *http.Client, contentType string, printResponse bool) *Invoker {
4142
return &Invoker{
4243
PrintResponse: printResponse,
4344
Client: client,
4445
GatewayURL: gatewayURL,
46+
ContentType: contentType,
4547
Responses: make(chan InvokerResponse),
4648
}
4749
}
@@ -67,7 +69,7 @@ func (i *Invoker) InvokeWithContext(ctx context.Context, topicMap *TopicMap, top
6769
gwURL := fmt.Sprintf("%s/%s", i.GatewayURL, matchedFunction)
6870
reader := bytes.NewReader(*message)
6971

70-
body, statusCode, header, doErr := invokefunction(ctx, i.Client, gwURL, reader)
72+
body, statusCode, header, doErr := invokefunction(ctx, i.Client, gwURL, i.ContentType, reader)
7173

7274
if doErr != nil {
7375
i.Responses <- InvokerResponse{
@@ -88,7 +90,7 @@ func (i *Invoker) InvokeWithContext(ctx context.Context, topicMap *TopicMap, top
8890
}
8991
}
9092

91-
func invokefunction(ctx context.Context, c *http.Client, gwURL string, reader io.Reader) (*[]byte, int, *http.Header, error) {
93+
func invokefunction(ctx context.Context, c *http.Client, gwURL, contentType string, reader io.Reader) (*[]byte, int, *http.Header, error) {
9294

9395
httpReq, err := http.NewRequest(http.MethodPost, gwURL, reader)
9496
if err != nil {
@@ -100,6 +102,10 @@ func invokefunction(ctx context.Context, c *http.Client, gwURL string, reader io
100102
defer httpReq.Body.Close()
101103
}
102104

105+
if contentType != "" {
106+
httpReq.Header.Set("Content-Type", contentType)
107+
}
108+
103109
var body *[]byte
104110

105111
res, doErr := c.Do(httpReq)

0 commit comments

Comments
 (0)