Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add response headers to trigger. #85

Merged
merged 8 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions trigger/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ flogo install github.com/project-flogo/contrib/trigger/rest
| content | any | The content of the request

### Reply:
| Name | Type | Description
|:--- | :--- | :---
| code | int | The http code to reply with
| data | any | The data to reply with
| Name | Type | Description
|:--- | :--- | :---
| code | int | The http code to reply with
| data | any | The data to reply with
|headers| params | The HTTP response headers


## Example Configurations
Expand Down
7 changes: 6 additions & 1 deletion trigger/rest/descriptor.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flogo-rest",
"type": "flogo:trigger",
"version": "0.9.0",
"version": "0.9.1",
"title": "Receive HTTP Message",
"description": "Simple REST Trigger",
"homepage": "https://github.com/project-flogo/contrib/tree/master/trigger/rest",
Expand Down Expand Up @@ -65,6 +65,11 @@
"name": "data",
"type": "any",
"description": "The data to reply with"
},
{
"name": "headers",
"type": "params",
"description": "The HTTP response headers"
}
],
"handler": {
Expand Down
2 changes: 2 additions & 0 deletions trigger/rest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/project-flogo/contrib/trigger/rest

require (
github.com/julienschmidt/httprouter v1.2.0
github.com/pkg/errors v0.8.1 // indirect
github.com/project-flogo/core v0.9.2
github.com/stretchr/testify v1.3.0
)

3 changes: 1 addition & 2 deletions trigger/rest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-flogo/core v0.9.0 h1:/iR4m5L0zj5SuqLtDDZIRyvrvG8TxwxdM0n8ZURo1I4=
github.com/project-flogo/core v0.9.0/go.mod h1:QGWi7TDLlhGUaYH3n/16ImCuulbEHGADYEXyrcHhX7U=
github.com/project-flogo/core v0.9.2 h1:1j7d8e6ivwnhyRudeTRIZlr2XCNa+SERrmcrzKswr0Y=
github.com/project-flogo/core v0.9.2/go.mod h1:QGWi7TDLlhGUaYH3n/16ImCuulbEHGADYEXyrcHhX7U=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
15 changes: 10 additions & 5 deletions trigger/rest/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type Output struct {
Headers map[string]string `md:"headers"` // The HTTP header parameters
Content interface{} `md:"content"` // The content of the request
Method string `md:"method"` // The HTTP method used for the request

}

type Reply struct {
Code int `md:"code"` // The http code to reply with
Data interface{} `md:"data"` // The data to reply with
Code int `md:"code"` // The http code to reply with
Data interface{} `md:"data"` // The data to reply with
Headers map[string]string `md:"headers"` // The HTTP response headers
}

func (o *Output) ToMap() map[string]interface{} {
Expand Down Expand Up @@ -66,8 +66,9 @@ func (o *Output) FromMap(values map[string]interface{}) error {

func (r *Reply) ToMap() map[string]interface{} {
return map[string]interface{}{
"code": r.Code,
"data": r.Data,
"code": r.Code,
"data": r.Data,
"headers": r.Headers,
}
}

Expand All @@ -79,6 +80,10 @@ func (r *Reply) FromMap(values map[string]interface{}) error {
return err
}
r.Data, _ = values["data"]
r.Headers, err = coerce.ToParams(values["headers"])
if err != nil {
return err
}

return nil
}
7 changes: 7 additions & 0 deletions trigger/rest/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ func newActionHandler(rt *Trigger, method string, handler trigger.Handler) httpr
return
}

// add response headers
if len(reply.Headers) > 0 {
for key, value := range reply.Headers {
w.Header().Add(key, value)
}
}

if reply.Data != nil {

if reply.Code == 0 {
Expand Down